from fastapi import APIRouter, Depends

from core.security.authentication import get_user, require_role
from schema import GenericResponse
from schema.time_slot import TimeSlotCreateSchema, TimeSlotUpdateSchema
from services.time_slot import TimeSlotService

time_slot_router = APIRouter(prefix="/time_slot")


@time_slot_router.post("/", response_model=GenericResponse, dependencies=[Depends(require_role('admin'))])
async def add_new_time_slot(request: TimeSlotCreateSchema, user=Depends(get_user),
                            service: TimeSlotService = Depends(TimeSlotService)):
    new_slot = service.create_new_time_slot(request)
    return {"message": "Time slot added"}


@time_slot_router.get("/", response_model=GenericResponse)
async def list_all_time_slot(user=Depends(get_user), service=Depends(TimeSlotService)):
    return {"message": "Time slot populated successfully.", 'data': service.fetch_all_time_slot()}


@time_slot_router.get("/{time_slot_id}", response_model=GenericResponse)
async def time_slot_details(time_slot_id: int, user=Depends(get_user), service=Depends(TimeSlotService)):
    return {"message": "Time slot  populated successfully.", 'data': service.fetch_time_slot_details(time_slot_id)}


@time_slot_router.put("/{time_slot_id}", response_model=GenericResponse, dependencies=[Depends(require_role('admin'))])
async def update_time_slot(time_slot_id: int, request: TimeSlotUpdateSchema, user=Depends(get_user),
                           service=Depends(TimeSlotService)):
    service.update_time_slot(time_slot_id, request)
    return {"message": "Time slot  updated successfully."}


@time_slot_router.delete("/{time_slot_id}", response_model=GenericResponse,
                         dependencies=[Depends(require_role('admin'))])
async def delete_time_slot(time_slot_id: int, user=Depends(get_user), service=Depends(TimeSlotService)):
    service.delete_time_slot(time_slot_id)
    return {"message": "Time slot deleted successfully."}
