package com.reading.platform.controller.school;

import cn.hutool.core.util.PageUtil;
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.common.util.PageUtils;
import com.reading.platform.dto.request.StudentCreateRequest;
import com.reading.platform.dto.request.StudentUpdateRequest;
import com.reading.platform.dto.response.ImportTemplateResponse;
import com.reading.platform.dto.response.MessageResponse;
import com.reading.platform.dto.response.StudentTransferHistoryResponse;
import com.reading.platform.entity.Student;
import com.reading.platform.service.StudentService;
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/students")
@RequiredArgsConstructor
public class SchoolStudentController {

    private final StudentService studentService;

    @Operation(summary = "创建学生")
    @PostMapping
    public Result<Student> createStudent(@Valid @RequestBody StudentCreateRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(studentService.createStudent(tenantId, request));
    }

    @Operation(summary = "更新学生")
    @PutMapping("/{id}")
    public Result<Student> updateStudent(@PathVariable String id, @RequestBody StudentUpdateRequest request) {
        return Result.success(studentService.updateStudent(id, request));
    }

    @Operation(summary = "根据ID获取学生")
    @GetMapping("/{id}")
    public Result<Student> getStudent(@PathVariable String id) {
        return Result.success(studentService.getStudentById(id));
    }

    @Operation(summary = "获取学生分页")
    @GetMapping
    public Result<PageResult<Student>> getStudentPage(
            @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<Student> page = studentService.getStudentPage(tenantId, pageNum, pageSize, keyword, grade, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除学生")
    @DeleteMapping("/{id}")
    public Result<Void> deleteStudent(@PathVariable String id) {
        studentService.deleteStudent(id);
        return Result.success();
    }

    @Operation(summary = "批量导入学生")
    @PostMapping("/import")
    public Result<List<Student>> importStudents(@RequestBody List<StudentCreateRequest> requests) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(studentService.importStudents(tenantId, requests));
    }

    @Operation(summary = "获取导入模板")
    @GetMapping("/import/template")
    public Result<ImportTemplateResponse> getImportTemplate() {
        return Result.success(ImportTemplateResponse.studentTemplate());
    }

    @Operation(summary = "学生调班")
    @PostMapping("/{id}/transfer")
    public Result<MessageResponse> transferStudent(
            @PathVariable String id,
            @RequestBody TransferStudentRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        studentService.transferStudent(tenantId, id, request.getToClassId(), request.getReason());
        return Result.success(MessageResponse.of("调班成功"));
    }

    @Operation(summary = "获取学生调班历史")
    @GetMapping("/{id}/history")
    public Result<List<StudentTransferHistoryResponse>> getStudentHistory(@PathVariable String id) {
        List<StudentTransferHistoryResponse> history = studentService.getStudentClassHistory(id);

        return Result.success(history);
    }

    /**
     * 调班请求 DTO
     */
    public static class TransferStudentRequest {
        private String toClassId;
        private String reason;

        public String getToClassId() { return toClassId; }
        public void setToClassId(String toClassId) { this.toClassId = toClassId; }
        public String getReason() { return reason; }
        public void setReason(String reason) { this.reason = reason; }
    }

}
