"""API routes."""
from __future__ import annotations
from uuid import UUID
from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_db, get_pagination, require_tenant
from app.api.permissions import require_permissions
from shared.pagination import PaginationParams
from shared.security import CurrentUser

from app.permissions.definitions import BRANCH_POLICIES_CREATE, BRANCH_POLICIES_VIEW
from app.permissions.definitions import ROOMS_CREATE, ROOMS_VIEW
from app.permissions.definitions import STATIONS_CREATE, STATIONS_VIEW
from app.schemas.salon import BranchPolicyCreate, BranchPolicyRead
from app.schemas.salon import StationCreate, StationRead
from app.schemas.salon import TreatmentRoomCreate, TreatmentRoomRead
from app.services.salon import BranchPolicyService
from app.services.salon import StationService
from app.services.salon import TreatmentRoomService


treatmentRoom_router = APIRouter()

@treatmentRoom_router.post("", response_model=TreatmentRoomRead, status_code=status.HTTP_201_CREATED)
async def create_treatmentRoom(body: TreatmentRoomCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(ROOMS_CREATE))):
    return await TreatmentRoomService(db).create(tenant_id, body, actor=user)

@treatmentRoom_router.get("", response_model=list[TreatmentRoomRead])
async def list_treatmentRoom(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(ROOMS_VIEW))):
    return await TreatmentRoomService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@treatmentRoom_router.get("/{entity_id}", response_model=TreatmentRoomRead)
async def get_treatmentRoom(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(ROOMS_VIEW))):
    return await TreatmentRoomService(db).get(tenant_id, entity_id)


station_router = APIRouter()

@station_router.post("", response_model=StationRead, status_code=status.HTTP_201_CREATED)
async def create_station(body: StationCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(STATIONS_CREATE))):
    return await StationService(db).create(tenant_id, body, actor=user)

@station_router.get("", response_model=list[StationRead])
async def list_station(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(STATIONS_VIEW))):
    return await StationService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@station_router.get("/{entity_id}", response_model=StationRead)
async def get_station(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(STATIONS_VIEW))):
    return await StationService(db).get(tenant_id, entity_id)


branchPolicy_router = APIRouter()

@branchPolicy_router.post("", response_model=BranchPolicyRead, status_code=status.HTTP_201_CREATED)
async def create_branchPolicy(body: BranchPolicyCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(BRANCH_POLICIES_CREATE))):
    return await BranchPolicyService(db).create(tenant_id, body, actor=user)

@branchPolicy_router.get("", response_model=list[BranchPolicyRead])
async def list_branchPolicy(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(BRANCH_POLICIES_VIEW))):
    return await BranchPolicyService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@branchPolicy_router.get("/{entity_id}", response_model=BranchPolicyRead)
async def get_branchPolicy(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(BRANCH_POLICIES_VIEW))):
    return await BranchPolicyService(db).get(tenant_id, entity_id)
