<?php
class Project {
    private $conn;
    private $table_name = "projects";

    public $id;
    public $customer_id;
    public $project_name;
    public $description;
    public $status;
    public $start_date;
    public $end_date;
    public $budget;
    public $created_at;
    public $updated_at;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET customer_id=:customer_id, project_name=:project_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, created_at=:created_at, updated_at=:updated_at";

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

        $this->customer_id = htmlspecialchars(strip_tags($this->customer_id));
        $this->project_name = htmlspecialchars(strip_tags($this->project_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->budget = htmlspecialchars(strip_tags($this->budget));
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":customer_id", $this->customer_id);
        $stmt->bindParam(":project_name", $this->project_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":budget", $this->budget);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);

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

        return false;
    }

    public function read() {
        $query = "SELECT p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id ORDER BY p.start_date DESC, p.created_at DESC";

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

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id WHERE p.id = ? LIMIT 0,1";

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

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

        $this->customer_id = $row['customer_id'];
        $this->project_name = $row['project_name'];
        $this->description = $row['description'];
        $this->status = $row['status'];
        $this->start_date = $row['start_date'];
        $this->end_date = $row['end_date'];
        $this->budget = $row['budget'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET customer_id=:customer_id, project_name=:project_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, updated_at=:updated_at WHERE id=:id";

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

        $this->customer_id = htmlspecialchars(strip_tags($this->customer_id));
        $this->project_name = htmlspecialchars(strip_tags($this->project_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->budget = htmlspecialchars(strip_tags($this->budget));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":customer_id", $this->customer_id);
        $stmt->bindParam(":project_name", $this->project_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":budget", $this->budget);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);

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

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

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

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

        return false;
    }

    public function search($search_term) {
        $query = "SELECT p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id WHERE 
                  p.project_name LIKE ? OR 
                  p.description LIKE ? OR 
                  c.first_name LIKE ? OR 
                  c.last_name LIKE ? OR 
                  c.company_name LIKE ?
                  ORDER BY p.start_date DESC, p.created_at DESC";

        $stmt = $this->conn->prepare($query);
        $search_term = "%{$search_term}%";
        $stmt->bindParam(1, $search_term);
        $stmt->bindParam(2, $search_term);
        $stmt->bindParam(3, $search_term);
        $stmt->bindParam(4, $search_term);
        $stmt->bindParam(5, $search_term);
        $stmt->execute();

        return $stmt;
    }

    public function getSubprojects($project_id) {
        $query = "SELECT * FROM subprojects WHERE project_id = ? ORDER BY created_at DESC";

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

        return $stmt;
    }

    public function getStatusBadge() {
        $badges = [
            'planning' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Planning</span>',
            'in_progress' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">In Progress</span>',
            'completed' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Completed</span>',
            'on_hold' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">On Hold</span>',
            'cancelled' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cancelled</span>'
        ];
        
        return $badges[$this->status] ?? $this->status;
    }

    public function getProgress() {
        if ($this->start_date && $this->end_date) {
            $start = new DateTime($this->start_date);
            $end = new DateTime($this->end_date);
            $now = new DateTime();
            
            if ($now < $start) {
                return 0; // Not started yet
            } elseif ($now > $end) {
                return 100; // Completed
            } else {
                $total = $end->diff($start)->days;
                $elapsed = $now->diff($start)->days;
                return min(100, round(($elapsed / $total) * 100));
            }
        }
        return 0;
    }

    public function getCustomerName() {
        $query = "SELECT CONCAT(first_name, ' ', last_name) as customer_name, company_name FROM clients WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->customer_id);
        $stmt->execute();
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($row['company_name']) {
            return $row['company_name'];
        }
        return $row['customer_name'];
    }
}
?>
