<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class Track extends Model
{
    public function allByConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT t.*, u.first_name, u.last_name
            FROM tracks t
            LEFT JOIN users u ON u.id = t.chair_user_id
            WHERE t.conference_id = :conference_id
            ORDER BY t.id DESC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("
            SELECT t.*, c.title_ar, c.title_en
            FROM tracks t
            INNER JOIN conferences c ON c.id = t.conference_id
            WHERE t.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 tracks (
                conference_id, chair_user_id,
                name_ar, name_en,
                description_ar, description_en,
                max_reviewers, min_reviewers, is_active
            ) VALUES (
                :conference_id, :chair_user_id,
                :name_ar, :name_en,
                :description_ar, :description_en,
                :max_reviewers, :min_reviewers, :is_active
            )
        ");

        $stmt->execute([
            ':conference_id' => $data['conference_id'],
            ':chair_user_id' => $data['chair_user_id'] ?: null,
            ':name_ar' => $data['name_ar'],
            ':name_en' => $data['name_en'],
            ':description_ar' => $data['description_ar'] ?: null,
            ':description_en' => $data['description_en'] ?: null,
            ':max_reviewers' => $data['max_reviewers'] ?: 3,
            ':min_reviewers' => $data['min_reviewers'] ?: 2,
            ':is_active' => $data['is_active'] ?? 1,
        ]);

        return (int) $this->db->lastInsertId();
    }

    public function update(int $id, array $data): void
    {
        $stmt = $this->db->prepare("
            UPDATE tracks SET
                conference_id = :conference_id,
                chair_user_id = :chair_user_id,
                name_ar = :name_ar,
                name_en = :name_en,
                description_ar = :description_ar,
                description_en = :description_en,
                max_reviewers = :max_reviewers,
                min_reviewers = :min_reviewers,
                is_active = :is_active
            WHERE id = :id
        ");

        $stmt->execute([
            ':id' => $id,
            ':conference_id' => $data['conference_id'],
            ':chair_user_id' => $data['chair_user_id'] ?: null,
            ':name_ar' => $data['name_ar'],
            ':name_en' => $data['name_en'],
            ':description_ar' => $data['description_ar'] ?: null,
            ':description_en' => $data['description_en'] ?: null,
            ':max_reviewers' => $data['max_reviewers'] ?: 3,
            ':min_reviewers' => $data['min_reviewers'] ?: 2,
            ':is_active' => $data['is_active'] ?? 1,
        ]);
    }

    public function activeByConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM tracks
            WHERE conference_id = :conference_id AND is_active = 1
            ORDER BY id ASC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }
}
