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.Clazz;
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 = "School - Class", description = "Class Management APIs for School")
@RestController
@RequestMapping("/api/school/classes")
@RequiredArgsConstructor
public class SchoolClassController {

    private final ClassService classService;

    @Operation(summary = "Create class")
    @PostMapping
    public Result<Clazz> createClass(@Valid @RequestBody ClassCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(classService.createClass(tenantId, request));
    }

    @Operation(summary = "Update class")
    @PutMapping("/{id}")
    public Result<Clazz> updateClass(@PathVariable Long id, @RequestBody ClassUpdateRequest request) {
        return Result.success(classService.updateClass(id, request));
    }

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

    @Operation(summary = "Get class page")
    @GetMapping
    public Result<PageResult<Clazz>> getClassPage(
            @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<Clazz> page = classService.getClassPage(tenantId, pageNum, pageSize, keyword, grade, status);
        return Result.success(PageResult.of(page));
    }

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

    @Operation(summary = "Assign teachers to class")
    @PostMapping("/{id}/teachers")
    public Result<Void> assignTeachers(@PathVariable Long id, @RequestBody List<Long> teacherIds) {
        classService.assignTeachers(id, teacherIds);
        return Result.success();
    }

    @Operation(summary = "Assign students to class")
    @PostMapping("/{id}/students")
    public Result<Void> assignStudents(@PathVariable Long id, @RequestBody List<Long> studentIds) {
        classService.assignStudents(id, studentIds);
        return Result.success();
    }

}
