<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class ConferenceBlockModel extends Model
{
    public function byConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM conference_blocks
            WHERE conference_id = :conference_id
            ORDER BY block_type ASC, sort_order ASC, id ASC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }

    public function activeByConference(int $conferenceId, ?string $type = null): array
    {
        $sql = "
            SELECT *
            FROM conference_blocks
            WHERE conference_id = :conference_id
              AND is_active = 1
        ";
        $params = [':conference_id' => $conferenceId];

        if ($type !== null && $type !== '') {
            $sql .= " AND block_type = :block_type";
            $params[':block_type'] = $type;
        }

        $sql .= " ORDER BY sort_order ASC, id ASC";

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

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM conference_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 conference_blocks (
                conference_id, block_type, title_en, title_ar, body_en, body_ar,
                image_path, link_url, sort_order, is_active
            ) VALUES (
                :conference_id, :block_type, :title_en, :title_ar, :body_en, :body_ar,
                :image_path, :link_url, :sort_order, :is_active
            )
        ");
        $stmt->execute([
            ':conference_id' => $data['conference_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,
        ]);
        return (int) $this->db->lastInsertId();
    }

    public function update(int $id, array $data): void
    {
        $stmt = $this->db->prepare("
            UPDATE conference_blocks
            SET conference_id = :conference_id,
                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,
            ':conference_id' => $data['conference_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,
        ]);
    }
}
