<?php

class Faq {
    private $pdo;

    public function __construct(PDO $pdo) {
        $this->pdo = $pdo;
    }

    /**
     * Adds a new FAQ entry to the database.
     *
     * @param string $title_en The English question for the FAQ.
     * @param string $description The English answer for the FAQ.
     * @param int $reg_by The ID of the user who registered this entry (from ist_tbl_users).
     * @param int $fkisp_id_of The ID of the person associated with this entry (from ist_tbl_people).
     * @return bool True on success.
     * @throws Exception If a database error occurs.
     */
    public function addFaq(string $title_en, string $description, int $reg_by, int $fkisp_id_of): bool {
        $sql = "INSERT INTO dsps_tbl_dspsfaq (dspsfaq_title_en, dspsfaq_description, dspsfaq_reg_by, fkisp_id_of)
                VALUES (:title_en, :description, :reg_by, :fkisp_id_of)";
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->bindParam(':title_en', $title_en);
            $stmt->bindParam(':description', $description);
            $stmt->bindParam(':reg_by', $reg_by);
            $stmt->bindParam(':fkisp_id_of', $fkisp_id_of);
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Error adding FAQ entry: " . $e->getMessage());
            throw new Exception("Could not add FAQ entry. Please try again later.");
        }
    }

    /**
     * Updates an existing FAQ entry.
     *
     * @param int $id The ID of the FAQ entry to update.
     * @param string $title_en The new English question.
     * @param string $description The new English answer.
     * @param int $mod_by The ID of the user who modified this entry.
     * @param int $fkisp_id_of The ID of the person associated with this entry.
     * @return bool True on success.
     * @throws Exception If a database error occurs.
     */
    public function updateFaq(int $id, string $title_en, string $description, int $mod_by, int $fkisp_id_of): bool {
        $sql = "UPDATE dsps_tbl_dspsfaq
                SET dspsfaq_title_en = :title_en, dspsfaq_description = :description,
                    dspsfaq_mod_datetime = CURRENT_TIMESTAMP, dspsfaq_reg_by = :mod_by, fkisp_id_of = :fkisp_id_of
                WHERE pkdspsfaq_id = :id";
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->bindParam(':title_en', $title_en);
            $stmt->bindParam(':description', $description);
            $stmt->bindParam(':mod_by', $mod_by);
            $stmt->bindParam(':fkisp_id_of', $fkisp_id_of);
            $stmt->bindParam(':id', $id);
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Error updating FAQ entry (ID: $id): " . $e->getMessage());
            throw new Exception("Could not update FAQ entry. Please try again later.");
        }
    }

    /**
     * Deletes an FAQ entry.
     *
     * @param int $id The ID of the FAQ entry to delete.
     * @return bool True on success.
     * @throws Exception If a database error occurs.
     */
    public function deleteFaq(int $id): bool {
        $sql = "DELETE FROM dsps_tbl_dspsfaq WHERE pkdspsfaq_id = :id";
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->bindParam(':id', $id);
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Error deleting FAQ entry (ID: $id): " . $e->getMessage());
            throw new Exception("Could not delete FAQ entry. Please try again later.");
        }
    }

    /**
     * Retrieves a single FAQ entry by its ID.
     *
     * @param int $id The ID of the FAQ entry.
     * @return array|false The entry data as an associative array, or false if not found.
     * @throws Exception If a database error occurs.
     */
    public function getFaqById(int $id) {
        $sql = "SELECT * FROM dsps_tbl_dspsfaq WHERE pkdspsfaq_id = :id";
        try {
            $stmt = $this->pdo->prepare($sql);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
            return $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Error fetching FAQ entry by ID ($id): " . $e->getMessage());
            throw new Exception("Could not retrieve FAQ entry. Please try again later.");
        }
    }

    /**
     * Retrieves all FAQ entries.
     *
     * @return array An array of FAQ entry data.
     * @throws Exception If a database error occurs.
     */
    public function getAllFaqs(): array {
        $sql = "SELECT * FROM dsps_tbl_dspsfaq ORDER BY dspsfaq_reg_datetime ASC";
        try {
            $stmt = $this->pdo->query($sql);
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Error fetching all FAQ entries: " . $e->getMessage());
            throw new Exception("Could not retrieve FAQ entries. Please try again later.");
        }
    }

    /**
     * Gets the total count of FAQ entries.
     *
     * @return int The total number of FAQ entries.
     * @throws Exception If a database error occurs.
     */
    public function getTotalFaqs(): int {
        $sql = "SELECT COUNT(*) FROM dsps_tbl_dspsfaq";
        try {
            $stmt = $this->pdo->query($sql);
            return $stmt->fetchColumn();
        } catch (PDOException $e) {
            error_log("Error getting total FAQ count: " . $e->getMessage());
            throw new Exception("Could not retrieve FAQ count. Please try again later.");
        }
    }
}
