<?php
require_once __DIR__ . '/BaseModel.php';

class Quotation extends BaseModel {
    protected $table = 'quotations';

    public function paginateWithDetails($page = 1, $perPage = 10) {
        $offset = ($page - 1) * $perPage;
        
        $stmt = $this->db->prepare("
            SELECT q.*, cust.full_name as customer_name, v.agency_name as vendor_name, m.full_name as maid_name
            FROM {$this->table} q
            LEFT JOIN customers cust ON q.customer_id = cust.id
            LEFT JOIN vendors v ON q.vendor_id = v.id
            LEFT JOIN maids m ON q.maid_id = m.id
            ORDER BY q.id DESC LIMIT ? OFFSET ?
        ");
        $stmt->bindValue(1, $perPage, PDO::PARAM_INT);
        $stmt->bindValue(2, $offset, PDO::PARAM_INT);
        $stmt->execute();
        $data = $stmt->fetchAll();
        
        // Fetch inclusions for each quotation
        foreach ($data as &$row) {
            $incStmt = $this->db->prepare("SELECT title, is_included FROM quotation_inclusions WHERE quotation_id = ?");
            $incStmt->execute([$row['id']]);
            $row['inclusions'] = $incStmt->fetchAll(PDO::FETCH_ASSOC);
        }
        
        $countStmt = $this->db->query("SELECT COUNT(*) FROM {$this->table}");
        $total = $countStmt->fetchColumn();
        
        return [
            'data' => $data,
            'total' => $total,
            'page' => $page,
            'per_page' => $perPage
        ];
    }
}
