from fastapi import Depends, APIRouter

from core.security.authentication import require_role, get_user
from schema import GenericResponse
from schema.course_allocation import CourseAllocationCreateSchema, CourseAllocationUpdateSchema
from services.course_allocation import CourseAllocationService

course_allocation_router = APIRouter(prefix="/course_allocation")


@course_allocation_router.post("/", dependencies=[Depends(require_role('admin'))], response_model=GenericResponse)
async def create_course_allocation(request: CourseAllocationCreateSchema, user=Depends(get_user),
                                   service=Depends(CourseAllocationService)):
    service.save(request)
    return {"message": "Course allocation created successfully."}


@course_allocation_router.get("/", response_model=GenericResponse)
async def list_all_course_allocation(user=Depends(get_user), service=Depends(CourseAllocationService)):
    return {"message": "Course allocation populated successfully.", 'data': service.fetch_all_course_allocation()}


@course_allocation_router.get("/{course_allocation_id}", response_model=GenericResponse)
async def course_allocation_detail(course_allocation_id: int, user=Depends(get_user),
                                   service=Depends(CourseAllocationService)):
    return {"message": "Course allocation populated successfully.",
            'data': service.course_allocation_detail(course_allocation_id)}


@course_allocation_router.put("/{course_allocation_id}", response_model=GenericResponse,
                              dependencies=[Depends(require_role('admin'))])
async def update_course_allocation(course_allocation_id: int, request: CourseAllocationUpdateSchema,
                                   user=Depends(get_user), service=Depends(CourseAllocationService)):
    service.update_course_allocation(course_allocation_id, request)
    return {"message": "Course allocation updated successfully."}


@course_allocation_router.delete("/{course_allocation_id}", response_model=GenericResponse,
                                 dependencies=[Depends(require_role('admin'))])
async def delete_course_allocation(course_allocation_id: int, user=Depends(get_user),
                                   service=Depends(CourseAllocationService)):
    service.delete_course_allocation(course_allocation_id)
    return {"message": "Course allocation deleted successfully."}


@course_allocation_router.get("/course_wise/{course_id}")
async def get_allocation_details_by_course(course_id: int, user=Depends(get_user),
                                           service=Depends(CourseAllocationService)):
    return {"message": "Course allocation populated successfully.",
            'data': service.filter_allocation_by_course_id(course_id)}
