<?php
require_once __DIR__ . '/../../../../helpers/response.php';
require_once __DIR__ . '/../../../Middleware/VendorAuthMiddleware.php';
require_once __DIR__ . '/../../../Models/Database.php';

class MarketingController {
    
    public function packages() {
        $db = Database::getInstance();
        
        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $limit = isset($_GET['limit']) ? max(1, intval($_GET['limit'])) : 10;
        $offset = ($page - 1) * $limit;

        $total = (int) $db->query("SELECT COUNT(*) FROM ad_packages WHERE status = 'active'")->fetchColumn();

        $stmt = $db->prepare("
            SELECT * FROM ad_packages 
            WHERE status = 'active' 
            ORDER BY price ASC
            LIMIT " . intval($limit) . " OFFSET " . intval($offset) . "
        ");
        $stmt->execute();
        $packages = $stmt->fetchAll();

        $pagination = [
            'total' => $total,
            'page' => $page,
            'limit' => $limit,
            'total_pages' => ceil($total / $limit)
        ];

        jsonResponse(true, 'Ad packages loaded', ['packages' => $packages], $pagination);
    }

    public function ads() {
        $payload = VendorAuthMiddleware::authenticate();
        $vendorId = $payload['id'];
        
        $db = Database::getInstance();

        $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $limit = isset($_GET['limit']) ? max(1, intval($_GET['limit'])) : 10;
        $offset = ($page - 1) * $limit;

        $totalStmt = $db->prepare("
            SELECT COUNT(*)
            FROM vendor_ads va
            JOIN maids m ON va.maid_id = m.id
            JOIN ad_packages ap ON va.ad_package_id = ap.id
            WHERE va.vendor_id = ?
        ");
        $totalStmt->execute([$vendorId]);
        $total = (int) $totalStmt->fetchColumn();

        $stmt = $db->prepare("
            SELECT va.*, m.full_name as maid_name, ap.name_en as package_name
            FROM vendor_ads va
            JOIN maids m ON va.maid_id = m.id
            JOIN ad_packages ap ON va.ad_package_id = ap.id
            WHERE va.vendor_id = ?
            ORDER BY va.created_at DESC
            LIMIT " . intval($limit) . " OFFSET " . intval($offset) . "
        ");
        $stmt->execute([$vendorId]);
        $ads = $stmt->fetchAll();
        
        $pagination = [
            'total' => $total,
            'page' => $page,
            'limit' => $limit,
            'total_pages' => ceil($total / $limit)
        ];

        jsonResponse(true, 'Vendor ads loaded', ['ads' => $ads], $pagination);
    }

    public function purchaseAd() {
        $payload = VendorAuthMiddleware::authenticate();
        $vendorId = $payload['id'];
        
        $data = json_decode(file_get_contents('php://input'), true);
        if (!isset($data['maid_id']) || !isset($data['ad_package_id'])) {
            sendError('Maid and Package selection are required', [], 422);
        }
        
        $db = Database::getInstance();
        
        // In a real app, you'd check if the maid belongs to the vendor here
        
        $stmt = $db->prepare("
            INSERT INTO vendor_ads (vendor_id, maid_id, ad_package_id, status)
            VALUES (?, ?, ?, 'pending')
        ");
        $stmt->execute([$vendorId, $data['maid_id'], $data['ad_package_id']]);
        
        sendResponse([], 'Ad request submitted successfully. Awaiting payment and approval.', 201);
    }
}
