"""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 MEMBERSHIPS_CREATE, MEMBERSHIPS_VIEW
from app.permissions.definitions import PACKAGES_CREATE, PACKAGES_VIEW
from app.schemas.packages import MembershipPlanCreate, MembershipPlanRead
from app.schemas.packages import PackageDefinitionCreate, PackageDefinitionRead
from app.services.packages import MembershipPlanService
from app.services.packages import PackageDefinitionService


packageDefinition_router = APIRouter()

@packageDefinition_router.post("", response_model=PackageDefinitionRead, status_code=status.HTTP_201_CREATED)
async def create_packageDefinition(body: PackageDefinitionCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(PACKAGES_CREATE))):
    return await PackageDefinitionService(db).create(tenant_id, body, actor=user)

@packageDefinition_router.get("", response_model=list[PackageDefinitionRead])
async def list_packageDefinition(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(PACKAGES_VIEW))):
    return await PackageDefinitionService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@packageDefinition_router.get("/{entity_id}", response_model=PackageDefinitionRead)
async def get_packageDefinition(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(PACKAGES_VIEW))):
    return await PackageDefinitionService(db).get(tenant_id, entity_id)


membershipPlan_router = APIRouter()

@membershipPlan_router.post("", response_model=MembershipPlanRead, status_code=status.HTTP_201_CREATED)
async def create_membershipPlan(body: MembershipPlanCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(require_permissions(MEMBERSHIPS_CREATE))):
    return await MembershipPlanService(db).create(tenant_id, body, actor=user)

@membershipPlan_router.get("", response_model=list[MembershipPlanRead])
async def list_membershipPlan(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(MEMBERSHIPS_VIEW))):
    return await MembershipPlanService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)

@membershipPlan_router.get("/{entity_id}", response_model=MembershipPlanRead)
async def get_membershipPlan(entity_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(require_permissions(MEMBERSHIPS_VIEW))):
    return await MembershipPlanService(db).get(tenant_id, entity_id)
