<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

/**
 * Faq Model
 *
 * Handles frequently asked questions data retrieval.
 */
class Faq extends Model
{
    /**
     * The database table associated with the model.
     */
    protected string $table = 'faqs';

    /**
     * Get FAQs for the home page (show_on_home = 1 and active = 1).
     *
     * @param int $limit Maximum number of FAQs to return
     * @return array<int, array<string, mixed>>
     */
    public function getForHome(int $limit = 6): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `show_on_home` = :show_on_home AND `active` = :active ORDER BY `sort_order` ASC LIMIT :limit',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':show_on_home', 1, PDO::PARAM_INT);
        $stmt->bindValue(':active', 1, PDO::PARAM_INT);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }

    /**
     * Get FAQs filtered by category.
     *
     * @param string $category The FAQ category to filter by
     * @return array<int, array<string, mixed>>
     */
    public function getByCategory(string $category): array
    {
        $sql = sprintf(
            'SELECT * FROM `%s` WHERE `category` = :category AND `active` = :active ORDER BY `sort_order` ASC',
            $this->table
        );

        $stmt = $this->db->prepare($sql);
        $stmt->execute([':category' => $category, ':active' => 1]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }
}
