<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class ConferenceSessionModel extends Model
{
    public function byConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT s.*, d.program_date, r.room_name_en, r.room_name_ar
            FROM conference_sessions s
            INNER JOIN conference_program_days d ON d.id = s.program_day_id
            LEFT JOIN conference_rooms r ON r.id = s.room_id
            WHERE s.conference_id = :conference_id
            ORDER BY d.program_date ASC, s.start_time ASC, s.sort_order ASC, s.id ASC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }

    public function byConferenceGrouped(int $conferenceId): array
    {
        $rows = $this->byConference($conferenceId);
        $out = [];
        foreach ($rows as $row) {
            $date = (string) $row['program_date'];
            $out[$date][] = $row;
        }
        return $out;
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM conference_sessions 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_sessions (
                conference_id, program_day_id, room_id, session_type, title_en, title_ar,
                description_en, description_ar, chair_name_en, chair_name_ar,
                start_time, end_time, sort_order, is_active
            ) VALUES (
                :conference_id, :program_day_id, :room_id, :session_type, :title_en, :title_ar,
                :description_en, :description_ar, :chair_name_en, :chair_name_ar,
                :start_time, :end_time, :sort_order, :is_active
            )
        ");
        $stmt->execute([
            ':conference_id' => $data['conference_id'],
            ':program_day_id' => $data['program_day_id'],
            ':room_id' => $data['room_id'] ?: null,
            ':session_type' => $data['session_type'],
            ':title_en' => $data['title_en'],
            ':title_ar' => $data['title_ar'] ?: null,
            ':description_en' => $data['description_en'] ?: null,
            ':description_ar' => $data['description_ar'] ?: null,
            ':chair_name_en' => $data['chair_name_en'] ?: null,
            ':chair_name_ar' => $data['chair_name_ar'] ?: null,
            ':start_time' => $data['start_time'],
            ':end_time' => $data['end_time'],
            ':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_sessions
            SET conference_id = :conference_id,
                program_day_id = :program_day_id,
                room_id = :room_id,
                session_type = :session_type,
                title_en = :title_en,
                title_ar = :title_ar,
                description_en = :description_en,
                description_ar = :description_ar,
                chair_name_en = :chair_name_en,
                chair_name_ar = :chair_name_ar,
                start_time = :start_time,
                end_time = :end_time,
                sort_order = :sort_order,
                is_active = :is_active
            WHERE id = :id
        ");
        $stmt->execute([
            ':id' => $id,
            ':conference_id' => $data['conference_id'],
            ':program_day_id' => $data['program_day_id'],
            ':room_id' => $data['room_id'] ?: null,
            ':session_type' => $data['session_type'],
            ':title_en' => $data['title_en'],
            ':title_ar' => $data['title_ar'] ?: null,
            ':description_en' => $data['description_en'] ?: null,
            ':description_ar' => $data['description_ar'] ?: null,
            ':chair_name_en' => $data['chair_name_en'] ?: null,
            ':chair_name_ar' => $data['chair_name_ar'] ?: null,
            ':start_time' => $data['start_time'],
            ':end_time' => $data['end_time'],
            ':sort_order' => $data['sort_order'] ?? 0,
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
        ]);
    }
}
