"""Tracking command handlers — Phase 10.7."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy.ext.asyncio import AsyncSession

from app.schemas.tracking import (
    CustomerTrackingTokenCreate,
    ProofOfDeliveryCreate,
    ProofOfDeliveryUpdate,
    TrackingPointCreate,
    TrackingSessionCreate,
    TrackingSessionUpdate,
)
from app.services.tracking import (
    CustomerTrackingTokenService,
    ProofOfDeliveryService,
    TrackingSessionService,
)
from shared.security import CurrentUser


class TrackingCommands:
    def __init__(self, session: AsyncSession) -> None:
        self.sessions = TrackingSessionService(session)
        self.pod = ProofOfDeliveryService(session)
        self.tokens = CustomerTrackingTokenService(session)

    async def start_session(
        self, tenant_id: UUID, body: TrackingSessionCreate, *, actor: CurrentUser | None
    ):
        return await self.sessions.start(tenant_id, body, actor=actor)

    async def update_session(
        self,
        tenant_id: UUID,
        session_id: UUID,
        body: TrackingSessionUpdate,
        *,
        actor: CurrentUser | None,
    ):
        return await self.sessions.update(tenant_id, session_id, body, actor=actor)

    async def record_point(
        self,
        tenant_id: UUID,
        session_id: UUID,
        body: TrackingPointCreate,
        *,
        actor: CurrentUser | None,
    ):
        return await self.sessions.record_point(
            tenant_id, session_id, body, actor=actor
        )

    async def capture_pod(
        self, tenant_id: UUID, body: ProofOfDeliveryCreate, *, actor: CurrentUser | None
    ):
        return await self.pod.capture(tenant_id, body, actor=actor)

    async def update_pod(
        self,
        tenant_id: UUID,
        pod_id: UUID,
        body: ProofOfDeliveryUpdate,
        *,
        actor: CurrentUser | None,
    ):
        return await self.pod.update(tenant_id, pod_id, body, actor=actor)

    async def create_token(
        self,
        tenant_id: UUID,
        body: CustomerTrackingTokenCreate,
        *,
        actor: CurrentUser | None,
    ):
        return await self.tokens.create(tenant_id, body, actor=actor)

    async def revoke_token(
        self, tenant_id: UUID, token_id: UUID, *, actor: CurrentUser | None
    ):
        return await self.tokens.revoke(tenant_id, token_id, actor=actor)
