<?php

namespace Core\App\Models;

use PDO;

class ThemeSections
{
    protected PDO $db;

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

    /* =========================
       GETTERS
    ========================= */

    public function getAll(): array
    {
        $stmt = $this->db->query("
            SELECT *
            FROM theme_sections
            ORDER BY section_order ASC, id ASC
        ");

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

    public function getById(int $id): ?array
    {
        if ($id <= 0) {
            return null;
        }

        $stmt = $this->db->prepare("
            SELECT *
            FROM theme_sections
            WHERE id = :id
            LIMIT 1
        ");

        $stmt->execute([':id' => $id]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        return $row ?: null;
    }

    /* =========================
       CREATE
    ========================= */

    public function add(
        string $sectionKey,
        string $sectionType,
        int $order = 0,
        array $settings = [],
        int $isEnabled = 1
    ): bool {
        $stmt = $this->db->prepare("
            INSERT INTO theme_sections
                (section_key, section_type, section_order, settings, is_enabled)
            VALUES
                (:section_key, :section_type, :section_order, :settings, :is_enabled)
        ");

        return $stmt->execute([
            ':section_key'   => $sectionKey,
            ':section_type'  => $sectionType,
            ':section_order' => $order,
            ':settings'      => json_encode($settings, JSON_UNESCAPED_UNICODE),
            ':is_enabled'    => $isEnabled,
        ]);
    }

    /* =========================
       UPDATE
    ========================= */

    public function update(int $id, array $data): bool
    {
        if ($id <= 0) {
            return false;
        }

        $fields = [];
        $params = [':id' => $id];

        if (isset($data['section_order'])) {
            $fields[] = 'section_order = :section_order';
            $params[':section_order'] = (int)$data['section_order'];
        }

        if (isset($data['is_enabled'])) {
            $fields[] = 'is_enabled = :is_enabled';
            $params[':is_enabled'] = (int)$data['is_enabled'];
        }

        if (isset($data['settings'])) {
            $fields[] = 'settings = :settings';
            $params[':settings'] = json_encode(
                $data['settings'],
                JSON_UNESCAPED_UNICODE
            );
        }

        if (empty($fields)) {
            return false;
        }

        $sql = "
            UPDATE theme_sections
            SET " . implode(', ', $fields) . "
            WHERE id = :id
            LIMIT 1
        ";

        $stmt = $this->db->prepare($sql);
        return $stmt->execute($params);
    }

    /* =========================
       DELETE  (FIXED – SAFE)
    ========================= */

    public function delete(int $id): bool
    {
        if ($id <= 0) {
            return false;
        }

        $stmt = $this->db->prepare("
            DELETE FROM theme_sections
            WHERE id = :id
            LIMIT 1
        ");

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