<?php

class Project {
    private $conn;
    private $table_name = "projects";

    public $id_project;
    public $project_name;
    public $description;
    public $start_date;
    public $end_date;
    public $status;
    public $created_by_user_id;
    public $created_at;
    public $updated_at;

    public function __construct($db) {
        $this->conn = $db;
    }

    // Create a new project
    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                  SET
                    project_name=:project_name,
                    description=:description,
                    start_date=:start_date,
                    end_date=:end_date,
                    status=:status,
                    created_by_user_id=:created_by_user_id";

        $stmt = $this->conn->prepare($query);

        // Sanitize
        $this->project_name = htmlspecialchars(strip_tags($this->project_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = !empty($this->end_date) ? htmlspecialchars(strip_tags($this->end_date)) : null;
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->created_by_user_id = htmlspecialchars(strip_tags($this->created_by_user_id));

        // Bind values
        $stmt->bindParam(":project_name", $this->project_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":created_by_user_id", $this->created_by_user_id);

        if ($stmt->execute()) {
            $this->id_project = $this->conn->lastInsertId();
            return true;
        }
        return false;
    }

    // Read all projects
    public function readAll() {
        $query = "SELECT
                    p.id_project, p.project_name, p.description, p.start_date, p.end_date, p.status,
                    p.created_at, u.nama_lengkap as created_by_name
                  FROM
                    " . $this->table_name . " p
                  LEFT JOIN
                    users u ON p.created_by_user_id = u.id_user
                  ORDER BY
                    p.start_date DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // Read a single project by ID
    public function readOne() {
        $query = "SELECT
                    p.id_project, p.project_name, p.description, p.start_date, p.end_date, p.status,
                    p.created_by_user_id, p.created_at, u.nama_lengkap as created_by_name
                  FROM
                    " . $this->table_name . " p
                  LEFT JOIN
                    users u ON p.created_by_user_id = u.id_user
                  WHERE
                    p.id_project = ?
                  LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id_project);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row) {
            $this->project_name = $row['project_name'];
            $this->description = $row['description'];
            $this->start_date = $row['start_date'];
            $this->end_date = $row['end_date'];
            $this->status = $row['status'];
            $this->created_by_user_id = $row['created_by_user_id'];
            $this->created_at = $row['created_at'];
            return true;
        }
        return false;
    }

    // Update a project
    public function update() {
        $query = "UPDATE " . $this->table_name . "
                  SET
                    project_name = :project_name,
                    description = :description,
                    start_date = :start_date,
                    end_date = :end_date,
                    status = :status
                  WHERE
                    id_project = :id_project";

        $stmt = $this->conn->prepare($query);

        // Sanitize
        $this->project_name = htmlspecialchars(strip_tags($this->project_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = !empty($this->end_date) ? htmlspecialchars(strip_tags($this->end_date)) : null;
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->id_project = htmlspecialchars(strip_tags($this->id_project));

        // Bind values
        $stmt->bindParam(":project_name", $this->project_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":id_project", $this->id_project);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    // Delete a project
    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id_project = ?";
        $stmt = $this->conn->prepare($query);

        $this->id_project = htmlspecialchars(strip_tags($this->id_project));
        $stmt->bindParam(1, $this->id_project);

        if ($stmt->execute()) {
            return true;
        }
        return false;
      }
    // Update only the project's status
    public function updateStatus($new_status, $id_project) {
        $query = "UPDATE " . $this->table_name . " SET status = :status WHERE id_project = :id_project";
        $stmt = $this->conn->prepare($query);

        // Sanitize
        $status = htmlspecialchars(strip_tags($new_status));
        $id_project = htmlspecialchars(strip_tags($id_project));

        // Bind values
        $stmt->bindParam(":status", $status);
        $stmt->bindParam(":id_project", $id_project);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }
}
