<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class HomepageBlockModel extends Model
{
    public function all(): array
    {
        return $this->db->query("
            SELECT *
            FROM homepage_blocks
            ORDER BY sort_order ASC, id DESC
        ")->fetchAll();
    }

    public function active(): array
    {
        return $this->db->query("
            SELECT *
            FROM homepage_blocks
            WHERE is_active = 1
            ORDER BY sort_order ASC, id DESC
        ")->fetchAll();
    }

    public function latest(int $limit = 6): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM homepage_blocks
            WHERE is_active = 1
            ORDER BY created_at DESC, id DESC
            LIMIT :lim
        ");
        $stmt->bindValue(':lim', $limit, \PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function byType(string $type): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM homepage_blocks
            WHERE block_type = :block_type
              AND is_active = 1
            ORDER BY sort_order ASC, id DESC
        ");
        $stmt->execute([':block_type' => $type]);
        return $stmt->fetchAll();
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM homepage_blocks WHERE id = :id LIMIT 1");
        $stmt->execute([':id' => $id]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    public function create(array $data): int
    {
        $stmt = $this->db->prepare("
            INSERT INTO homepage_blocks (
                block_type, title_en, title_ar, body_en, body_ar,
                image_path, link_url, sort_order, is_active, created_by
            ) VALUES (
                :block_type, :title_en, :title_ar, :body_en, :body_ar,
                :image_path, :link_url, :sort_order, :is_active, :created_by
            )
        ");
        $stmt->execute([
            ':block_type' => $data['block_type'],
            ':title_en' => $data['title_en'] ?: null,
            ':title_ar' => $data['title_ar'] ?: null,
            ':body_en' => $data['body_en'] ?: null,
            ':body_ar' => $data['body_ar'] ?: null,
            ':image_path' => $data['image_path'] ?: null,
            ':link_url' => $data['link_url'] ?: null,
            ':sort_order' => $data['sort_order'] ?? 0,
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
            ':created_by' => $data['created_by'] ?? null,
        ]);
        return (int) $this->db->lastInsertId();
    }

    public function update(int $id, array $data): void
    {
        $stmt = $this->db->prepare("
            UPDATE homepage_blocks
            SET block_type = :block_type,
                title_en = :title_en,
                title_ar = :title_ar,
                body_en = :body_en,
                body_ar = :body_ar,
                image_path = :image_path,
                link_url = :link_url,
                sort_order = :sort_order,
                is_active = :is_active
            WHERE id = :id
        ");
        $stmt->execute([
            ':id' => $id,
            ':block_type' => $data['block_type'],
            ':title_en' => $data['title_en'] ?: null,
            ':title_ar' => $data['title_ar'] ?: null,
            ':body_en' => $data['body_en'] ?: null,
            ':body_ar' => $data['body_ar'] ?: null,
            ':image_path' => $data['image_path'] ?: null,
            ':link_url' => $data['link_url'] ?: null,
            ':sort_order' => $data['sort_order'] ?? 0,
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
        ]);
    }
}
