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.status import StatusEntity
from app.core.usecases.status_service import StatusesService
from app.infrastructure.database.models.users import User

router = APIRouter()


@router.get("/statuses", response_model=List[StatusEntity])
def read_statuses(db: Session = Depends(get_db),
                  current_user: User = Depends(verify_token)):
    service = StatusesService(db)
    return service.get_all_statuses()


@router.get("/statuses/{status_id}", response_model=StatusEntity)
def read_status(status_id: int, db: Session = Depends(get_db),
                current_user: User = Depends(verify_token)):
    service = StatusesService(db)
    status = service.get_status_by_id(status_id)
    if status is None:
        raise HTTPException(status_code=404, detail="Status not found")
    return status


@router.post("/statuses", response_model=StatusEntity)
def create_status(status: StatusEntity, db: Session = Depends(get_db),
                  current_user: User = Depends(verify_token)):
    service = StatusesService(db)
    return service.create_status(status)


@router.put("/statuses/{status_id}", response_model=StatusEntity)
def update_status(status_id: int, status: StatusEntity, db: Session = Depends(get_db),
                  current_user: User = Depends(verify_token)):
    service = StatusesService(db)
    updated_status = service.update_status(status_id, status)
    if updated_status is None:
        raise HTTPException(status_code=404, detail="Status not found")
    return updated_status


@router.delete("/statuses/{status_id}", response_model=bool)
def delete_status(status_id: int, db: Session = Depends(get_db),
                  current_user: User = Depends(verify_token)):
    service = StatusesService(db)
    success = service.delete_status(status_id)
    if not success:
        raise HTTPException(status_code=404, detail="Status not found")
    return success
