from typing import List

from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session

from app.core.usecases.auth_service import verify_token
from app.infrastructure.database.dependencies import get_db
from app.core.entities.federal_district import FederalDistrictEntity
from app.core.usecases.federal_district_service import FederalDistrictsService
from app.infrastructure.database.models.users import User

router = APIRouter()


@router.get("/federal-districts", response_model=List[FederalDistrictEntity])
def read_federal_districts(db: Session = Depends(get_db),
                           current_user: User = Depends(verify_token)):
    service = FederalDistrictsService(db)
    return service.get_all_federal_districts()


@router.get("/federal-districts/{federal_district_id}", response_model=FederalDistrictEntity)
def read_federal_district(federal_district_id: int, db: Session = Depends(get_db),
                          current_user: User = Depends(verify_token)):
    service = FederalDistrictsService(db)
    federal_district = service.get_federal_district_by_id(federal_district_id)
    if federal_district is None:
        raise HTTPException(status_code=404, detail="Federal district not found")
    return federal_district


@router.post("/federal-districts", response_model=FederalDistrictEntity)
def create_federal_district(federal_district: FederalDistrictEntity, db: Session = Depends(get_db),
                            current_user: User = Depends(verify_token)):
    service = FederalDistrictsService(db)
    return service.create_federal_district(federal_district)


@router.put("/federal-districts/{federal_district_id}", response_model=FederalDistrictEntity)
def update_federal_district(federal_district_id: int, federal_district: FederalDistrictEntity,
                            db: Session = Depends(get_db),
                            current_user: User = Depends(verify_token)):
    service = FederalDistrictsService(db)
    updated_federal_district = service.update_federal_district(federal_district_id, federal_district)
    if updated_federal_district is None:
        raise HTTPException(status_code=404, detail="Federal district not found")
    return updated_federal_district


@router.delete("/federal-districts/{federal_district_id}", response_model=bool)
def delete_federal_district(federal_district_id: int, db: Session = Depends(get_db),
                            current_user: User = Depends(verify_token)):
    service = FederalDistrictsService(db)
    success = service.delete_federal_district(federal_district_id)
    if not success:
        raise HTTPException(status_code=404, detail="Federal district not found")
    return success
