<?php

class ProjectTask {
    private $conn;
    private $table_name = "project_tasks";

    public $id_task;
    public $id_project;
    public $task_name;
    public $description;
    public $assignee_user_id;
    public $start_date;
    public $due_date;
    public $status;
    public $created_at;
    public $updated_at;

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

    // Create a new task
    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                  SET
                    id_project=:id_project,
                    task_name=:task_name,
                    description=:description,
                    assignee_user_id=:assignee_user_id,
                    start_date=:start_date,
                    due_date=:due_date,
                    status=:status";

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

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

        // Bind values
        $stmt->bindParam(":id_project", $this->id_project);
        $stmt->bindParam(":task_name", $this->task_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":assignee_user_id", $this->assignee_user_id);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":due_date", $this->due_date);
        $stmt->bindParam(":status", $this->status);

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

    // Read all tasks for a specific project
    public function readByProjectId($id_project) {
        $query = "SELECT
                    pt.id_task, pt.task_name, pt.description, pt.start_date, pt.due_date, pt.status,
                    pt.assignee_user_id, u.nama_lengkap as assignee_name
                  FROM
                    " . $this->table_name . " pt
                  LEFT JOIN
                    users u ON pt.assignee_user_id = u.id_user
                  WHERE
                    pt.id_project = ?
                  ORDER BY
                    pt.start_date ASC, pt.due_date ASC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $id_project);
        $stmt->execute();
        return $stmt;
    }
    
    // Read a single task by ID
    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id_task = ? LIMIT 0,1";

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

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

        if($row) {
            $this->id_project = $row['id_project'];
            $this->task_name = $row['task_name'];
            $this->description = $row['description'];
            $this->assignee_user_id = $row['assignee_user_id'];
            $this->start_date = $row['start_date'];
            $this->due_date = $row['due_date'];
            $this->status = $row['status'];
            return true;
        }
        return false;
    }

    // Update a task
    public function update() {
        $query = "UPDATE " . $this->table_name . "
                  SET
                    task_name = :task_name,
                    description = :description,
                    assignee_user_id = :assignee_user_id,
                    start_date = :start_date,
                    due_date = :due_date,
                    status = :status
                  WHERE
                    id_task = :id_task";

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

        // Sanitize
        $this->task_name = htmlspecialchars(strip_tags($this->task_name));
        $this->description = !empty($this->description) ? htmlspecialchars(strip_tags($this->description)) : null;
        $this->assignee_user_id = !empty($this->assignee_user_id) ? htmlspecialchars(strip_tags($this->assignee_user_id)) : null;
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->due_date = !empty($this->due_date) ? htmlspecialchars(strip_tags($this->due_date)) : null;
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->id_task = htmlspecialchars(strip_tags($this->id_task));

        // Bind values
        $stmt->bindParam(":task_name", $this->task_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":assignee_user_id", $this->assignee_user_id);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":due_date", $this->due_date);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":id_task", $this->id_task);

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

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

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

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

        // Sanitize
        $status = htmlspecialchars(strip_tags($new_status));
        $id_task = htmlspecialchars(strip_tags($this->id_task));

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

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