<?php

namespace Core\App\Models;

use PDO;

class ProductSections
{
    protected PDO $db;

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

    /* =====================================================
     * Get sections for a specific product
     * ===================================================== */
    public function getByProduct(int $productId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM product_sections
            WHERE product_id = ?
            ORDER BY section_order ASC
        ");

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

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

        return $rows;
    }

    /* =====================================================
     * Get sections for a page (e.g. homepage)
     * ===================================================== */
    public function getByPage(string $page): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM product_sections
            WHERE page = ?
            ORDER BY section_order ASC
        ");

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

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

        return $rows;
    }

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

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

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

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

    /* =====================================================
     * Delete all sections of a product (used on update/save)
     * ===================================================== */
    public function deleteByProduct(int $productId): bool
    {
        $stmt = $this->db->prepare("
            DELETE FROM product_sections
            WHERE product_id = ?
        ");

        return $stmt->execute([$productId]);
    }

    /* =====================================================
     * Delete all sections of a page
     * ===================================================== */
    public function deleteByPage(string $page): bool
    {
        $stmt = $this->db->prepare("
            DELETE FROM product_sections
            WHERE page = ?
        ");

        return $stmt->execute([$page]);
    }
}
