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

class FaqController {
    public function index() {
        $db = Database::getInstance();
        
        // Fetch active FAQ categories
        $categoriesStmt = $db->query("
            SELECT id, name_en, name_ar, description_en, description_ar, icon, sort_order 
            FROM faq_categories 
            WHERE status = 'active' 
            ORDER BY sort_order ASC
        ");
        $categories = $categoriesStmt->fetchAll();
        
        // Fetch active FAQs
        $faqsStmt = $db->query("
            SELECT id, category_id, question_en, question_ar, answer_en, answer_ar, sort_order 
            FROM faqs 
            WHERE status = 'active' 
            ORDER BY sort_order ASC
        ");
        $faqs = $faqsStmt->fetchAll();
        
        // Group FAQs by category_id
        $groupedFaqs = [];
        foreach ($faqs as $faq) {
            $catId = $faq['category_id'];
            if (!isset($groupedFaqs[$catId])) {
                $groupedFaqs[$catId] = [];
            }
            $groupedFaqs[$catId][] = $faq;
        }
        
        // Attach FAQs to categories
        foreach ($categories as &$category) {
            $catId = $category['id'];
            $category['faqs'] = $groupedFaqs[$catId] ?? [];
        }
        
        $data = [
            'categories' => $categories
        ];
        
        // Check for any uncategorized FAQs (category_id is NULL or 0)
        $uncategorizedFaqs = [];
        foreach ($groupedFaqs as $catId => $list) {
            if (empty($catId) || $catId == 0) {
                $uncategorizedFaqs = array_merge($uncategorizedFaqs, $list);
            }
        }
        if (!empty($uncategorizedFaqs)) {
            $data['uncategorized'] = $uncategorizedFaqs;
        }
        
        sendResponse($data, 'FAQs loaded successfully');
    }

    public function categories() {
        $db = Database::getInstance();
        $categoriesStmt = $db->query("
            SELECT id, name_en, name_ar, description_en, description_ar, icon, sort_order 
            FROM faq_categories 
            WHERE status = 'active' 
            ORDER BY sort_order ASC
        ");
        $categories = $categoriesStmt->fetchAll();
        sendResponse(['categories' => $categories], 'FAQ categories loaded successfully');
    }

    public function categoryFaqs($categoryId) {
        $db = Database::getInstance();
        
        $catStmt = $db->prepare("
            SELECT id, name_en, name_ar, description_en, description_ar, icon, sort_order 
            FROM faq_categories 
            WHERE id = ? AND status = 'active'
        ");
        $catStmt->execute([$categoryId]);
        $category = $catStmt->fetch();
        
        if (!$category) {
            sendError('FAQ Category not found', [], 404);
        }
        
        $faqsStmt = $db->prepare("
            SELECT id, category_id, question_en, question_ar, answer_en, answer_ar, sort_order 
            FROM faqs 
            WHERE category_id = ? AND status = 'active' 
            ORDER BY sort_order ASC
        ");
        $faqsStmt->execute([$categoryId]);
        $faqs = $faqsStmt->fetchAll();
        
        $category['faqs'] = $faqs;
        
        sendResponse(['category' => $category], 'Category FAQs loaded successfully');
    }
}
