package com.lesingle.creation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.schoolclass.CreateClassDTO;
import com.lesingle.creation.dto.schoolclass.UpdateClassDTO;
import com.lesingle.creation.service.SchoolClassService;
import com.lesingle.creation.vo.schoolclass.ClassVO;
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.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * 班级管理控制器
 */
@Tag(name = "班级管理", description = "班级 CRUD 接口")
@RestController
@RequestMapping("/api/classes")
@RequiredArgsConstructor
public class SchoolClassController {

    private final SchoolClassService schoolClassService;

    @PostMapping
    @Operation(summary = "创建班级")
    @PreAuthorize("hasAuthority('class:create')")
    public Result<ClassVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Valid CreateClassDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        ClassVO result = schoolClassService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "分页查询班级列表")
    @PreAuthorize("hasAuthority('class:read')")
    public Result<Page<ClassVO>> pageQuery(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long gradeId,
            @RequestParam(required = false) Integer type) {
        Long tenantId = userPrincipal.getTenantId();
        Page<ClassVO> result = schoolClassService.pageQuery(pageNum, pageSize, tenantId, gradeId, type);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取班级详情")
    @PreAuthorize("hasAuthority('class:read')")
    public Result<ClassVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ClassVO result = schoolClassService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新班级")
    @PreAuthorize("hasAuthority('class:update')")
    public Result<ClassVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Valid UpdateClassDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long modifierId = userPrincipal.getUserId();
        ClassVO result = schoolClassService.update(id, dto, tenantId, modifierId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除班级")
    @PreAuthorize("hasAuthority('class:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        schoolClassService.delete(id, tenantId);
        return Result.success(null);
    }
}
