"""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 COMMISSIONS_CREATE, COMMISSIONS_VIEW
from app.permissions.definitions import STAFF_CREATE, STAFF_VIEW
from app.schemas.staff import CommissionRuleCreate, CommissionRuleRead
from app.schemas.staff import StaffProfileCreate, StaffProfileRead
from app.services.staff import CommissionRuleService
from app.services.staff import StaffProfileService


staffProfile_router = APIRouter()

@staffProfile_router.post("", response_model=StaffProfileRead, status_code=status.HTTP_201_CREATED)
async def create_staffProfile(body: StaffProfileCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(STAFF_CREATE))):
    return await StaffProfileService(db).create(tenant_id, body, actor=user)

@staffProfile_router.get("", response_model=list[StaffProfileRead])
async def list_staffProfile(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(STAFF_VIEW))):
    return await StaffProfileService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@staffProfile_router.get("/{entity_id}", response_model=StaffProfileRead)
async def get_staffProfile(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(STAFF_VIEW))):
    return await StaffProfileService(db).get(tenant_id, entity_id)


commissionRule_router = APIRouter()

@commissionRule_router.post("", response_model=CommissionRuleRead, status_code=status.HTTP_201_CREATED)
async def create_commissionRule(body: CommissionRuleCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(COMMISSIONS_CREATE))):
    return await CommissionRuleService(db).create(tenant_id, body, actor=user)

@commissionRule_router.get("", response_model=list[CommissionRuleRead])
async def list_commissionRule(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(COMMISSIONS_VIEW))):
    return await CommissionRuleService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@commissionRule_router.get("/{entity_id}", response_model=CommissionRuleRead)
async def get_commissionRule(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(COMMISSIONS_VIEW))):
    return await CommissionRuleService(db).get(tenant_id, entity_id)
