package com.magistr.app.controller;

import com.magistr.app.config.auth.RequireRoles;
import com.magistr.app.dto.ScheduleOverrideDto;
import com.magistr.app.dto.ScheduleOverrideAvailabilityDto;
import com.magistr.app.model.Role;
import com.magistr.app.service.ScheduleOverrideService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.time.LocalDate;

@RestController
@RequestMapping("/api/edu-office/schedule/overrides")
@RequireRoles({Role.ADMIN, Role.EDUCATION_OFFICE})
public class ScheduleOverrideController {

    private final ScheduleOverrideService scheduleOverrideService;

    public ScheduleOverrideController(ScheduleOverrideService scheduleOverrideService) {
        this.scheduleOverrideService = scheduleOverrideService;
    }

    @GetMapping
    public List<ScheduleOverrideDto> getAll(
            @RequestParam(required = false) LocalDate startDate,
            @RequestParam(required = false) LocalDate endDate
    ) {
        return scheduleOverrideService.getAll(startDate, endDate);
    }

    @GetMapping("/availability")
    public ScheduleOverrideAvailabilityDto getAvailability(
            @RequestParam Long baseRuleSlotId,
            @RequestParam LocalDate lessonDate
    ) {
        return scheduleOverrideService.getAvailability(baseRuleSlotId, lessonDate);
    }

    @PostMapping
    public ResponseEntity<?> create(@RequestBody ScheduleOverrideDto request) {
        return ResponseEntity.ok(scheduleOverrideService.create(request));
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Long id, @RequestBody ScheduleOverrideDto request) {
        return ResponseEntity.ok(scheduleOverrideService.update(id, request));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        scheduleOverrideService.delete(id);
        return ResponseEntity.ok(Map.of("message", "Точечное изменение расписания удалено"));
    }
}
