"""POS Lite repositories — Phase 12.4."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.pos_lite import PosRegister, PosShift, PosTicket, PosTicketLine
from app.repositories.base import TenantBaseRepository


class PosRegisterRepository(TenantBaseRepository[PosRegister]):
    model = PosRegister

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


class PosShiftRepository(TenantBaseRepository[PosShift]):
    model = PosShift

    async def list_open_for_register(self, tenant_id: UUID, register_id: UUID):
        from app.models.types import PosShiftStatus

        stmt = select(PosShift).where(
            PosShift.tenant_id == tenant_id,
            PosShift.register_id == register_id,
            PosShift.status == PosShiftStatus.OPEN,
            PosShift.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class PosTicketRepository(TenantBaseRepository[PosTicket]):
    model = PosTicket

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


class PosTicketLineRepository(TenantBaseRepository[PosTicketLine]):
    model = PosTicketLine
