package com.reading.platform.controller.school;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.dto.request.ClassCreateRequest;
import com.reading.platform.dto.request.ClassUpdateRequest;
import com.reading.platform.entity.ClassTeacher;
import com.reading.platform.entity.Clazz;
import com.reading.platform.entity.Student;
import com.reading.platform.service.ClassService;
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/classes")
@RequiredArgsConstructor
public class SchoolClassController {

    private final ClassService classService;

    @Operation(summary = "创建班级")
    @PostMapping
    public Result<Clazz> createClass(@Valid @RequestBody ClassCreateRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(classService.createClass(tenantId, request));
    }

    @Operation(summary = "更新班级")
    @PutMapping("/{id}")
    public Result<Clazz> updateClass(@PathVariable String id, @RequestBody ClassUpdateRequest request) {
        return Result.success(classService.updateClass(id, request));
    }

    @Operation(summary = "Get class by ID")
    @GetMapping("/{id}")
    public Result<Clazz> getClass(@PathVariable String id) {
        return Result.success(classService.getClassById(id));
    }

    @Operation(summary = "获取班级列表（无分页）")
    @GetMapping("/list")
    public Result<List<Clazz>> getClassList() {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(classService.getClassList(tenantId));
    }

    @Operation(summary = "Get class page")
    @GetMapping
    public Result<PageResult<Clazz>> getClassPage(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String grade,
            @RequestParam(required = false) String status) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<Clazz> page = classService.getClassPage(tenantId, pageNum, pageSize, keyword, grade, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除班级")
    @DeleteMapping("/{id}")
    public Result<Void> deleteClass(@PathVariable String id) {
        classService.deleteClass(id);
        return Result.success();
    }

    @Operation(summary = "获取班级学生分页")
    @GetMapping("/{id}/students")
    public Result<PageResult<Student>> getClassStudents(
            @PathVariable String id,
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<Student> page = classService.getClassStudents(id, pageNum, pageSize, keyword);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "获取班级教师列表")
    @GetMapping("/{id}/teachers")
    public Result<List<ClassTeacher>> getClassTeachers(@PathVariable String id) {
        return Result.success(classService.getClassTeachers(id));
    }

    @Operation(summary = "添加班级教师")
    @PostMapping("/{id}/teachers")
    public Result<ClassTeacher> addClassTeacher(
            @PathVariable String id,
            @RequestBody ClassTeacherRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(classService.addClassTeacher(tenantId, id, request.getTeacherId(), request.getRole(), request.getIsPrimary()));
    }

    @Operation(summary = "更新班级教师角色")
    @PutMapping("/{id}/teachers/{teacherId}")
    public Result<ClassTeacher> updateClassTeacher(
            @PathVariable String id,
            @PathVariable String teacherId,
            @RequestBody ClassTeacherRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(classService.updateClassTeacher(tenantId, id, teacherId, request.getRole(), request.getIsPrimary()));
    }

    @Operation(summary = "移除班级教师")
    @DeleteMapping("/{id}/teachers/{teacherId}")
    public Result<Void> removeClassTeacher(@PathVariable String id, @PathVariable String teacherId) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        classService.removeClassTeacher(tenantId, id, teacherId);
        return Result.success();
    }

    @Operation(summary = "分配教师到班级")
    @PostMapping("/{id}/teachers/batch")
    public Result<Void> assignTeachers(@PathVariable String id, @RequestBody List<String> teacherIds) {
        classService.assignTeachers(id, teacherIds);
        return Result.success();
    }

    @Operation(summary = "分配学生到班级")
    @PostMapping("/{id}/students/batch")
    public Result<Void> assignStudents(@PathVariable String id, @RequestBody List<String> studentIds) {
        classService.assignStudents(id, studentIds);
        return Result.success();
    }

    @Operation(summary = "移除班级教师（旧接口）")
    @DeleteMapping("/{id}/teachers/remove/{teacherId}")
    public Result<Void> removeTeacher(@PathVariable String id, @PathVariable String teacherId) {
        classService.removeTeacher(id, teacherId);
        return Result.success();
    }

    @Operation(summary = "移除班级学生（旧接口）")
    @DeleteMapping("/{id}/students/remove/{studentId}")
    public Result<Void> removeStudent(@PathVariable String id, @PathVariable String studentId) {
        classService.removeStudent(id, studentId);
        return Result.success();
    }

    /**
     * 班级教师请求 DTO
     */
    public static class ClassTeacherRequest {
        private String teacherId;
        private String role;
        private Boolean isPrimary;

        public String getTeacherId() {
            return teacherId;
        }

        public void setTeacherId(String teacherId) {
            this.teacherId = teacherId;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public Boolean getIsPrimary() {
            return isPrimary;
        }

        public void setIsPrimary(Boolean isPrimary) {
            this.isPrimary = isPrimary;
        }
    }

}
