from typing import Dict, Any from api.v1.common.tenant_crud_routes import TenantCRUDRoutes from core.database import get_core_db from core.security import get_current_user, validate_access_to_resource from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from . import dto, services # Create main router router = APIRouter() # Create CRUD routes for CustomsBroker using TenantCRUDRoutes customs_broker_crud = TenantCRUDRoutes( service=services.CustomsBrokerService, create_schema=dto.CustomsBrokerCreateDTO, update_schema=dto.CustomsBrokerUpdateDTO, response_schema=dto.CustomsBrokerResponseDTO, prefix="/customs-brokers", # No prefix since it's already in the parent router tags=[], resource_name="Customs Broker", id_name="broker_key", id_type=str, enable_list=True, # Enable list endpoint with pagination ) # Include the CRUD routes router.include_router(customs_broker_crud.router) # Additional routes for child resources (CustomsBrokerVU and CustomsBrokerPersonnel) # These remain as manual routes since they have different patterns @router.put( "/customs-broker-vu/{broker_key}", response_model=dto.CustomsBrokerVUCreateDTO, ) def update_customs_broker_vu( broker_key: str, vu_data: dto.CustomsBrokerVUCreateDTO, company_id: int = Query(..., description="Company ID"), db: Session = Depends(get_core_db), current_user: Dict[str, Any] = Depends(get_current_user), ): tenant_id = validate_access_to_resource(db, company_id, current_user) # Verify the broker exists and belongs to the tenant/company broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id) if not broker: raise HTTPException(status_code=404, detail="Customs Broker not found") updated_vu = services.CustomsBrokerVUService.update_vu(db, broker_key, vu_data) if not updated_vu: raise HTTPException(status_code=404, detail="Customs Broker VU not found") return updated_vu @router.put( "/customs-broker-personnel/{broker_key}/{line}", response_model=dto.CustomsBrokerPersonnelDTO, ) def update_customs_broker_personnel( broker_key: str, line: int, personnel_data: dto.CustomsBrokerPersonnelDTO, company_id: int = Query(..., description="Company ID"), db: Session = Depends(get_core_db), current_user: Dict[str, Any] = Depends(get_current_user), ): tenant_id = validate_access_to_resource(db, company_id, current_user) # Verify the broker exists and belongs to the tenant/company broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id) if not broker: raise HTTPException(status_code=404, detail="Customs Broker not found") updated_personnel = services.CustomsBrokerPersonnelService.update_personnel( db, broker_key, line, personnel_data ) if not updated_personnel: raise HTTPException( status_code=404, detail="Customs Broker Personnel not found" ) return updated_personnel