package com.reading.platform.controller.school;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.common.security.SecurityUtils;
import com.reading.platform.entity.SchoolCourse;
import com.reading.platform.service.SchoolCourseService;
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.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学校 - 校本课程", description = "校本课程管理接口（学校管理员专用）")
@RestController
@RequestMapping("/api/v1/school")
@RequiredArgsConstructor
@RequireRole(UserRole.SCHOOL)
public class SchoolCourseController {

    private final SchoolCourseService schoolCourseService;

    @Operation(summary = "获取课程列表（无分页）")
    @GetMapping("/courses")
    public Result<List<SchoolCourse>> getCoursesList() {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<SchoolCourse> page = schoolCourseService.getCourses(1, 100, tenantId, null);
        return Result.success(page.getRecords());
    }

    @Operation(summary = "获取校本课程分页")
    @GetMapping("/school-courses")
    public Result<PageResult<SchoolCourse>> getSchoolCoursesPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String keyword) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<SchoolCourse> page = schoolCourseService.getCourses(pageNum, pageSize, tenantId, keyword);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "根据 ID 获取校本课程（兼容旧路径 /school-courses/{id}）")
    @GetMapping("/school-courses/{id}")
    public Result<SchoolCourse> getSchoolCourseById(@PathVariable String id) {
        return Result.success(schoolCourseService.getCourseById(id));
    }

    @Operation(summary = "根据 ID 获取校本课程")
    @GetMapping("/courses/{id}")
    public Result<SchoolCourse> getCourseById(@PathVariable String id) {
        return Result.success(schoolCourseService.getCourseById(id));
    }

    @Operation(summary = "创建校本课程（兼容旧路径 /school-courses）")
    @PostMapping("/school-courses")
    public Result<SchoolCourse> createSchoolCourse(@Valid @RequestBody SchoolCourse course) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        String userId = SecurityUtils.getCurrentUserId();
        return Result.success(schoolCourseService.createCourse(tenantId, userId, course));
    }

    @Operation(summary = "创建校本课程")
    @PostMapping("/courses")
    public Result<SchoolCourse> createCourse(@Valid @RequestBody SchoolCourse course) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        String userId = SecurityUtils.getCurrentUserId();
        return Result.success(schoolCourseService.createCourse(tenantId, userId, course));
    }

    @Operation(summary = "更新校本课程（兼容旧路径 /school-courses/{id}）")
    @PutMapping("/school-courses/{id}")
    public Result<SchoolCourse> updateSchoolCourse(@PathVariable String id, @Valid @RequestBody SchoolCourse course) {
        return Result.success(schoolCourseService.updateCourse(id, course));
    }

    @Operation(summary = "更新校本课程")
    @PutMapping("/courses/{id}")
    public Result<SchoolCourse> updateCourse(@PathVariable String id, @Valid @RequestBody SchoolCourse course) {
        return Result.success(schoolCourseService.updateCourse(id, course));
    }

    @Operation(summary = "删除校本课程（兼容旧路径 /school-courses/{id}）")
    @DeleteMapping("/school-courses/{id}")
    public Result<Void> deleteSchoolCourse(@PathVariable String id) {
        schoolCourseService.deleteCourse(id);
        return Result.success();
    }

    @Operation(summary = "删除校本课程")
    @DeleteMapping("/courses/{id}")
    public Result<Void> deleteCourse(@PathVariable String id) {
        schoolCourseService.deleteCourse(id);
        return Result.success();
    }
}
