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.grade.CreateGradeDTO;
import com.lesingle.creation.dto.grade.UpdateGradeDTO;
import com.lesingle.creation.service.GradeService;
import com.lesingle.creation.vo.grade.GradeVO;
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/grades")
@RequiredArgsConstructor
public class GradeController {

    private final GradeService gradeService;

    @PostMapping
    @Operation(summary = "创建年级")
    @PreAuthorize("hasAuthority('grade:create')")
    public Result<GradeVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Valid CreateGradeDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        GradeVO result = gradeService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

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

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

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

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