<?php
declare(strict_types=1);

// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126

class SupportModel {
    private Database $db;

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

    // Save a new ticket to the database
    public function createTicket(array $data): bool {
        $this->db->query('INSERT INTO support_tickets (user_id, ticket_number, ticket_type, subject, description, status) VALUES (:user_id, :ticket_number, :ticket_type, :subject, :description, :status)');
        
        $this->db->bind(':user_id', $data['user_id']);
        $this->db->bind(':ticket_number', $data['ticket_number']);
        $this->db->bind(':ticket_type', $data['ticket_type']);
        $this->db->bind(':subject', $data['subject']);
        $this->db->bind(':description', $data['description']);
        $this->db->bind(':status', 'Open');

        return $this->db->execute();
    }

    // Get all tickets for a specific user
    public function getTicketsByUser(int $user_id): array {
        $this->db->query('SELECT * FROM support_tickets WHERE user_id = :user_id ORDER BY created_at DESC');
        $this->db->bind(':user_id', $user_id);
        return $this->db->resultSet();
    }

    // Mark a ticket as Resolved (Verifying the user actually owns it)
    public function markResolved(int $id, int $user_id): bool {
        $this->db->query('UPDATE support_tickets SET status = :status WHERE id = :id AND user_id = :user_id');
        $this->db->bind(':status', 'Resolved');
        $this->db->bind(':id', $id);
        $this->db->bind(':user_id', $user_id);
        
        return $this->db->execute();
    }
}