<?php
// app/Models/Faq.php - FAQ Model

class Faq {
    private $db;
    
    public function __construct() {
        $this->db = Database::getInstance();
    }
    
    /**
     * Create new FAQ
     */
    public function create($data) {
        $sql = "INSERT INTO faqs (question, answer, category, display_order, is_active, is_featured, created_at) 
                VALUES (?, ?, ?, ?, ?, ?, NOW())";
        
        $result = $this->db->execute($sql, [
            $data['question'],
            $data['answer'],
            $data['category'] ?? 'general',
            $data['display_order'] ?? 0,
            $data['is_active'] ?? 1,
            $data['is_featured'] ?? 0
        ]);
        
        return $result ? $this->db->lastInsertId() : false;
    }
    
    /**
     * Find FAQ by ID
     */
    public function find($id) {
        $sql = "SELECT * FROM faqs WHERE id = ? LIMIT 1";
        return $this->db->fetch($sql, [$id]);
    }
    
    /**
     * Get all FAQs with optional filtering
     */
    public function getAll($filters = []) {
        $sql = "SELECT * FROM faqs WHERE 1=1";
        $params = [];
        
        if (!empty($filters['category'])) {
            $sql .= " AND category = ?";
            $params[] = $filters['category'];
        }
        
        if (isset($filters['is_active'])) {
            $sql .= " AND is_active = ?";
            $params[] = $filters['is_active'];
        }
        
        if (isset($filters['is_featured'])) {
            $sql .= " AND is_featured = ?";
            $params[] = $filters['is_featured'];
        }
        
        $sql .= " ORDER BY display_order ASC, created_at DESC";
        
        return $this->db->fetchAll($sql, $params);
    }
    
    /**
     * Get featured FAQs (for homepage)
     */
    public function getFeatured($limit = 3) {
        $sql = "SELECT * FROM faqs WHERE is_active = 1 AND is_featured = 1 
                ORDER BY display_order ASC, created_at DESC LIMIT ?";
        return $this->db->fetchAll($sql, [$limit]);
    }
    
    /**
     * Get FAQs by category
     */
    public function getByCategory($category) {
        $sql = "SELECT * FROM faqs WHERE category = ? AND is_active = 1 
                ORDER BY display_order ASC, created_at DESC";
        return $this->db->fetchAll($sql, [$category]);
    }
    
    /**
     * Get all categories
     */
    public function getCategories() {
        $sql = "SELECT category, COUNT(*) as count FROM faqs WHERE is_active = 1 
                GROUP BY category ORDER BY category";
        return $this->db->fetchAll($sql);
    }
    
    /**
     * Search FAQs
     */
    public function search($keyword) {
        $sql = "SELECT * FROM faqs WHERE is_active = 1 AND 
                (question LIKE ? OR answer LIKE ?) 
                ORDER BY display_order ASC, created_at DESC";
        
        $search = '%' . $keyword . '%';
        return $this->db->fetchAll($sql, [$search, $search]);
    }
    
    /**
     * Update FAQ
     */
    public function update($id, $data) {
        $fields = [];
        $params = [];
        
        foreach ($data as $key => $value) {
            $fields[] = "{$key} = ?";
            $params[] = $value;
        }
        
        $params[] = $id;
        
        $sql = "UPDATE faqs SET " . implode(', ', $fields) . ", updated_at = NOW() WHERE id = ?";
        return $this->db->execute($sql, $params);
    }
    
    /**
     * Delete FAQ
     */
    public function delete($id) {
        $sql = "DELETE FROM faqs WHERE id = ?";
        return $this->db->execute($sql, [$id]);
    }
    
    /**
     * Toggle active status
     */
    public function toggleActive($id) {
        $faq = $this->find($id);
        if (!$faq) return false;
        
        $newStatus = $faq['is_active'] ? 0 : 1;
        return $this->update($id, ['is_active' => $newStatus]);
    }
    
    /**
     * Toggle featured status
     */
    public function toggleFeatured($id) {
        $faq = $this->find($id);
        if (!$faq) return false;
        
        $newStatus = $faq['is_featured'] ? 0 : 1;
        return $this->update($id, ['is_featured' => $newStatus]);
    }
    
    /**
     * Get FAQ statistics
     */
    public function getStats() {
        $sql = "SELECT 
                COUNT(*) as total_faqs,
                SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active_faqs,
                SUM(CASE WHEN is_featured = 1 THEN 1 ELSE 0 END) as featured_faqs,
                COUNT(DISTINCT category) as total_categories
                FROM faqs";
        
        return $this->db->fetch($sql);
    }
}