<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

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

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

    public function findByConferenceAndEvent(int $conferenceId, string $eventKey): ?array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM conference_email_templates
            WHERE conference_id = :conference_id
              AND event_key = :event_key
              AND is_active = 1
            LIMIT 1
        ");
        $stmt->execute([
            ':conference_id' => $conferenceId,
            ':event_key' => $eventKey,
        ]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    public function create(array $data): int
    {
        $stmt = $this->db->prepare("
            INSERT INTO conference_email_templates (
                conference_id, event_key, subject_en, subject_ar, body_en, body_ar, is_active
            ) VALUES (
                :conference_id, :event_key, :subject_en, :subject_ar, :body_en, :body_ar, :is_active
            )
        ");
        $stmt->execute([
            ':conference_id' => $data['conference_id'],
            ':event_key' => $data['event_key'],
            ':subject_en' => $data['subject_en'],
            ':subject_ar' => $data['subject_ar'],
            ':body_en' => $data['body_en'],
            ':body_ar' => $data['body_ar'],
            ':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_email_templates
            SET conference_id = :conference_id,
                event_key = :event_key,
                subject_en = :subject_en,
                subject_ar = :subject_ar,
                body_en = :body_en,
                body_ar = :body_ar,
                is_active = :is_active
            WHERE id = :id
        ");
        $stmt->execute([
            ':id' => $id,
            ':conference_id' => $data['conference_id'],
            ':event_key' => $data['event_key'],
            ':subject_en' => $data['subject_en'],
            ':subject_ar' => $data['subject_ar'],
            ':body_en' => $data['body_en'],
            ':body_ar' => $data['body_ar'],
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
        ]);
    }
}
