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.StudentCreateRequest;
import com.reading.platform.dto.request.StudentUpdateRequest;
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.*;

@Tag(name = "School - Student", description = "Student Management APIs for School")
@RestController
@RequestMapping("/api/school/students")
@RequiredArgsConstructor
public class SchoolStudentController {

    private final StudentService studentService;

    @Operation(summary = "Create student")
    @PostMapping
    public Result<Student> createStudent(@Valid @RequestBody StudentCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(studentService.createStudent(tenantId, request));
    }

    @Operation(summary = "Update student")
    @PutMapping("/{id}")
    public Result<Student> updateStudent(@PathVariable Long id, @RequestBody StudentUpdateRequest request) {
        return Result.success(studentService.updateStudent(id, request));
    }

    @Operation(summary = "Get student by ID")
    @GetMapping("/{id}")
    public Result<Student> getStudent(@PathVariable Long id) {
        return Result.success(studentService.getStudentById(id));
    }

    @Operation(summary = "Get student page")
    @GetMapping
    public Result<PageResult<Student>> getStudentPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String grade,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Page<Student> page = studentService.getStudentPage(tenantId, pageNum, pageSize, keyword, grade, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete student")
    @DeleteMapping("/{id}")
    public Result<Void> deleteStudent(@PathVariable Long id) {
        studentService.deleteStudent(id);
        return Result.success();
    }

}
