"""Movie ticket booking: shows, seat holds with expiry, confirmation, no double booking."""
from __future__ import annotations

import threading
from dataclasses import dataclass
from typing import Dict, List, Optional


@dataclass
class SeatState:
    status: str = "available"  # available | held | booked
    holder: Optional[str] = None
    hold_expires: float = 0.0


class Show:
    HOLD_SECONDS = 600

    def __init__(self, show_id: str, rows: int, cols: int):
        self.show_id = show_id
        self.seats: Dict[str, SeatState] = {f"{chr(65 + r)}{c + 1}": SeatState() for r in range(rows) for c in range(cols)}
        self._lock = threading.Lock()  # one lock per show keeps hold-or-fail atomic

    def _expire(self, now: float) -> None:
        for seat in self.seats.values():
            if seat.status == "held" and seat.hold_expires <= now:
                seat.status, seat.holder = "available", None

    def hold(self, user: str, seat_ids: List[str], now: float) -> bool:
        """All-or-nothing hold of several seats."""
        with self._lock:
            self._expire(now)
            if any(self.seats[s].status != "available" for s in seat_ids):
                return False
            for s in seat_ids:
                self.seats[s] = SeatState("held", user, now + self.HOLD_SECONDS)
            return True

    def confirm(self, user: str, seat_ids: List[str], now: float) -> bool:
        with self._lock:
            self._expire(now)
            if any(self.seats[s].status != "held" or self.seats[s].holder != user for s in seat_ids):
                return False
            for s in seat_ids:
                self.seats[s].status = "booked"
            return True

    def available(self, now: float) -> List[str]:
        with self._lock:
            self._expire(now)
            return [s for s, st in self.seats.items() if st.status == "available"]
