"""POS Pro repositories — Phase 12.5."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.pos_pro import PosDiscount, PosFloorPlan, PosPayment, PosStation, PosTaxLine
from app.repositories.base import TenantBaseRepository


class PosPaymentRepository(TenantBaseRepository[PosPayment]):
    model = PosPayment


class PosDiscountRepository(TenantBaseRepository[PosDiscount]):
    model = PosDiscount

    async def get_by_code_for_ticket(
        self, tenant_id: UUID, ticket_id: UUID, code: str
    ) -> PosDiscount | None:
        stmt = select(PosDiscount).where(
            PosDiscount.tenant_id == tenant_id,
            PosDiscount.ticket_id == ticket_id,
            PosDiscount.code == code,
            PosDiscount.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()


class PosTaxLineRepository(TenantBaseRepository[PosTaxLine]):
    model = PosTaxLine


class PosFloorPlanRepository(TenantBaseRepository[PosFloorPlan]):
    model = PosFloorPlan

    async def get_by_code(
        self, tenant_id: UUID, venue_id: UUID, code: str
    ) -> PosFloorPlan | None:
        stmt = select(PosFloorPlan).where(
            PosFloorPlan.tenant_id == tenant_id,
            PosFloorPlan.venue_id == venue_id,
            PosFloorPlan.code == code,
            PosFloorPlan.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()


class PosStationRepository(TenantBaseRepository[PosStation]):
    model = PosStation

    async def get_by_code(
        self, tenant_id: UUID, venue_id: UUID, code: str
    ) -> PosStation | None:
        stmt = select(PosStation).where(
            PosStation.tenant_id == tenant_id,
            PosStation.venue_id == venue_id,
            PosStation.code == code,
            PosStation.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()
