<?php 

namespace Core\App\Models;

use PDO;

class HomeSections
{
    protected PDO $db;

    public function __construct()
    {
        $this->db = \db();
    }

    /* =====================================================
     * Get all homepage sections
     * ===================================================== */
    public function getAll(): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM home_sections
        ");

        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($rows as &$row) {
            $row['settings'] = $row['settings']
                ? json_decode($row['settings'], true)
                : [];
        }

        return $rows;
    }

    /* =====================================================
     * Get single homepage section by ID
     * ===================================================== */
    public function getById(int $id): ?array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM home_sections
            WHERE id = :id
            LIMIT 1
        ");

        $stmt->execute([
            ':id' => $id
        ]);

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$row) {
            return null;
        }

        $row['settings'] = $row['settings']
            ? json_decode($row['settings'], true)
            : [];

        return $row;
    }

    /* =====================================================
     * Add a section to homepage
     * ===================================================== */
    public function add(
        string $type,
        int $order = 0,
        int $enabled = 1,
        array $settings = []
    ): bool {
        $stmt = $this->db->prepare("
            INSERT INTO home_sections
                (section_type, section_order, is_enabled, settings)
            VALUES
                (:section_type, :section_order, :is_enabled, :settings)
        ");

        return $stmt->execute([
            ':section_type'  => $type,
            ':section_order' => $order,
            ':is_enabled'    => $enabled,
            ':settings'      => json_encode($settings),
        ]);
    }

    /* =====================================================
     * Update homepage section
     * ===================================================== */
    public function update(
        int $id,
        int $order = 0,
        int $enabled = 1,
        array $settings = []
    ): bool {
        $stmt = $this->db->prepare("
            UPDATE home_sections
            SET
                section_order = :section_order,
                is_enabled    = :is_enabled,
                settings      = :settings
            WHERE id = :id
        ");

        return $stmt->execute([
            ':section_order' => $order,
            ':is_enabled'    => $enabled,
            ':settings'      => json_encode($settings),
            ':id'            => $id,
        ]);
    }

    /* =====================================================
     * Delete homepage section by ID
     * ===================================================== */
    public function delete(int $id): bool
    {
        $stmt = $this->db->prepare("
            DELETE FROM home_sections
            WHERE id = :id
        ");

        return $stmt->execute([
            ':id' => $id
        ]);
    }

    /* =====================================================
     * Delete all homepage sections
     * ===================================================== */
    public function deleteAll(): bool
    {
        $stmt = $this->db->prepare("
            DELETE FROM home_sections
        ");

        return $stmt->execute();
    }
}
