<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

/**
 * Seat Booking Model
 *
 * Handles seat booking for batches.
 */
class SeatBooking extends Model
{
    protected string $table = 'seat_bookings';

    /**
     * Get bookings for a specific batch.
     *
     * @param int $batchId The batch ID
     * @return array<int, array<string, mixed>>
     */
    public function getByBatch(int $batchId): array
    {
        $sql = "SELECT * FROM `{$this->table}` WHERE batch_id = :batch_id ORDER BY created_at DESC";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([':batch_id' => $batchId]);
        return $stmt->fetchAll();
    }

    /**
     * Check if a contact already booked a seat in a batch.
     *
     * @param int $batchId The batch ID
     * @param string $contact The contact number
     * @return bool
     */
    public function hasBooking(int $batchId, string $contact): bool
    {
        $sql = "SELECT COUNT(*) AS total FROM `{$this->table}`
                WHERE batch_id = :batch_id AND contact = :contact AND status != 'cancelled'";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([':batch_id' => $batchId, ':contact' => $contact]);
        return (int) ($stmt->fetch()['total'] ?? 0) > 0;
    }
}
