<?php
require_once __DIR__ . '/../../../Models/Database.php';

class FeaturedController {
    
    public function index() {
        $db = Database::getInstance();
        
        // 1. Fetch currently featured vendors
        $featuredVendors = $db->query("
            SELECT * FROM vendors 
            WHERE is_featured = 1 AND status = 'approved'
            ORDER BY agency_name ASC
        ")->fetchAll();
        
        // 2. Fetch currently recommended maids
        $recommendedMaids = $db->query("
            SELECT m.*, v.agency_name 
            FROM maids m 
            JOIN vendors v ON m.vendor_id = v.id
            WHERE m.is_recommended = 1 AND m.status = 'published' AND m.availability_status = 'available' AND v.status = 'approved'
            ORDER BY m.full_name ASC
        ")->fetchAll();
        
        // 3. Fetch unfeatured approved vendors for the dropdown
        $unfeaturedVendors = $db->query("
            SELECT id, agency_name FROM vendors 
            WHERE is_featured = 0 AND status = 'approved'
            ORDER BY agency_name ASC
        ")->fetchAll();
        
        // 4. Fetch unrecommended active available maids for the dropdown
        $unrecommendedMaids = $db->query("
            SELECT m.id, m.full_name, v.agency_name 
            FROM maids m 
            JOIN vendors v ON m.vendor_id = v.id
            WHERE m.is_recommended = 0 AND m.status = 'published' AND m.availability_status = 'available' AND v.status = 'approved'
            ORDER BY m.full_name ASC
        ")->fetchAll();
        
        require __DIR__ . '/../../../../views/admin/cms/featured.php';
    }
    
    public function addVendor() {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            header("Location: ?page=featured");
            exit;
        }
        
        $vendorId = (int)($_POST['vendor_id'] ?? 0);
        if ($vendorId > 0) {
            $db = Database::getInstance();
            $stmt = $db->prepare("UPDATE vendors SET is_featured = 1 WHERE id = ?");
            $stmt->execute([$vendorId]);
            $this->logAction("Add Featured Vendor", $vendorId);
            header("Location: ?page=featured&success=Vendor added to Featured successfully");
            exit;
        }
        
        header("Location: ?page=featured&error=Invalid Vendor selection");
        exit;
    }
    
    public function removeVendor() {
        $id = (int)($_GET['id'] ?? 0);
        if ($id > 0) {
            $db = Database::getInstance();
            $stmt = $db->prepare("UPDATE vendors SET is_featured = 0 WHERE id = ?");
            $stmt->execute([$id]);
            $this->logAction("Remove Featured Vendor", $id);
            header("Location: ?page=featured&success=Vendor removed from Featured successfully");
            exit;
        }
        
        header("Location: ?page=featured&error=Invalid Vendor ID");
        exit;
    }
    
    public function addMaid() {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            header("Location: ?page=featured");
            exit;
        }
        
        $maidId = (int)($_POST['maid_id'] ?? 0);
        if ($maidId > 0) {
            $db = Database::getInstance();
            $stmt = $db->prepare("UPDATE maids SET is_recommended = 1 WHERE id = ?");
            $stmt->execute([$maidId]);
            $this->logAction("Add Recommended Maid", $maidId);
            header("Location: ?page=featured&success=Maid added to Recommended successfully");
            exit;
        }
        
        header("Location: ?page=featured&error=Invalid Maid selection");
        exit;
    }
    
    public function removeMaid() {
        $id = (int)($_GET['id'] ?? 0);
        if ($id > 0) {
            $db = Database::getInstance();
            $stmt = $db->prepare("UPDATE maids SET is_recommended = 0 WHERE id = ?");
            $stmt->execute([$id]);
            $this->logAction("Remove Recommended Maid", $id);
            header("Location: ?page=featured&success=Maid removed from Recommended successfully");
            exit;
        }
        
        header("Location: ?page=featured&error=Invalid Maid ID");
        exit;
    }
    
    private function logAction($action, $targetId = null, $details = null) {
        require_once __DIR__ . '/../../../Models/AuditLog.php';
        (new AuditLog())->log($action, 'featured_settings', $targetId, $details);
    }
}
