package com.lesingle.edu.controller.teacher;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.enums.UserRole;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.request.SchedulePlanCreateRequest;
import com.lesingle.edu.dto.request.SchedulePlanUpdateRequest;
import com.lesingle.edu.dto.response.SchedulePlanResponse;
import com.lesingle.edu.dto.response.TimetableResponse;
import com.lesingle.edu.entity.SchedulePlan;
import com.lesingle.edu.service.SchoolScheduleService;
import com.lesingle.edu.service.TeacherScheduleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

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

/**
 * 教师端 - 排课管理
 */
@Tag(name = "教师端 - 排课管理", description = "教师端排课 API")
@RestController
@RequestMapping("/api/v1/teacher/schedules")
@RequiredArgsConstructor
@RequireRole(UserRole.TEACHER)
public class TeacherScheduleController {

    private final TeacherScheduleService teacherScheduleService;
    private final SchoolScheduleService schoolScheduleService;

    @GetMapping
    @Operation(summary = "获取教师排课列表")
    public Result<PageResult<SchedulePlanResponse>> getSchedules(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Long teacherId = SecurityUtils.getCurrentUserId();
        Page<SchedulePlan> page = teacherScheduleService.getTeacherSchedules(teacherId, pageNum, pageSize, startDate, endDate);
        List<SchedulePlanResponse> voList = page.getRecords().stream()
                .map(schoolScheduleService::toSchedulePlanResponse)
                .collect(java.util.stream.Collectors.toList());
        return Result.success(PageResult.of(voList, page.getTotal(), page.getCurrent(), page.getSize()));
    }

    @GetMapping("/timetable")
    @Operation(summary = "获取教师课程表")
    public Result<List<TimetableResponse>> getTimetable(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Long teacherId = SecurityUtils.getCurrentUserId();
        List<TimetableResponse> timetable = teacherScheduleService.getTimetable(teacherId, startDate, endDate);
        return Result.success(timetable);
    }

    @GetMapping("/today")
    @Operation(summary = "获取今日排课")
    public Result<List<SchedulePlanResponse>> getTodaySchedules() {
        Long teacherId = SecurityUtils.getCurrentUserId();
        List<SchedulePlan> schedules = teacherScheduleService.getTodaySchedules(teacherId);
        List<SchedulePlanResponse> voList = schedules.stream()
                .map(schoolScheduleService::toSchedulePlanResponse)
                .collect(java.util.stream.Collectors.toList());
        return Result.success(voList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取排课详情")
    public Result<SchedulePlanResponse> getSchedule(@PathVariable Long id) {
        SchedulePlan schedule = teacherScheduleService.getScheduleById(id);
        return Result.success(schoolScheduleService.toSchedulePlanResponse(schedule));
    }

    @PostMapping
    @Operation(summary = "创建排课")
    @Log(module = LogModule.SCHEDULE, type = LogOperationType.CREATE, description = "创建排课")
    public Result<SchedulePlanResponse> createSchedule(@Valid @RequestBody SchedulePlanCreateRequest request) {
        Long teacherId = SecurityUtils.getCurrentUserId();
        Long tenantId = SecurityUtils.getCurrentTenantId();
        SchedulePlan schedule = teacherScheduleService.createSchedule(tenantId, teacherId, request);
        return Result.success(schoolScheduleService.toSchedulePlanResponse(schedule));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新排课")
    @Log(module = LogModule.SCHEDULE, type = LogOperationType.UPDATE, description = "更新排课")
    public Result<SchedulePlanResponse> updateSchedule(
            @PathVariable Long id,
            @RequestBody SchedulePlanUpdateRequest request) {
        SchedulePlan schedule = teacherScheduleService.updateSchedule(id, request);
        return Result.success(schoolScheduleService.toSchedulePlanResponse(schedule));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "取消排课")
    @Log(module = LogModule.SCHEDULE, type = LogOperationType.DELETE, description = "取消排课")
    public Result<Void> cancelSchedule(@PathVariable Long id) {
        teacherScheduleService.cancelSchedule(id);
        return Result.success();
    }
}
