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.GrowthRecordCreateRequest;
import com.reading.platform.dto.request.GrowthRecordUpdateRequest;
import com.reading.platform.entity.GrowthRecord;
import com.reading.platform.service.GrowthRecordService;
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 = "学校 - 成长档案", description = "成长档案管理接口（学校管理员专用）")
@RestController
@RequestMapping("/api/v1/school/growth-records")
@RequiredArgsConstructor
public class SchoolGrowthController {

    private final GrowthRecordService growthRecordService;

    @Operation(summary = "创建成长档案")
    @PostMapping
    public Result<GrowthRecord> createGrowthRecord(@Valid @RequestBody GrowthRecordCreateRequest request) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        String userId = SecurityUtils.getCurrentUserId();
        String role = SecurityUtils.getCurrentRole();
        return Result.success(growthRecordService.createGrowthRecord(tenantId, userId, role, request));
    }

    @Operation(summary = "更新成长档案")
    @PutMapping("/{id}")
    public Result<GrowthRecord> updateGrowthRecord(@PathVariable String id, @RequestBody GrowthRecordUpdateRequest request) {
        return Result.success(growthRecordService.updateGrowthRecord(id, request));
    }

    @Operation(summary = "根据ID获取成长档案")
    @GetMapping("/{id}")
    public Result<GrowthRecord> getGrowthRecord(@PathVariable String id) {
        return Result.success(growthRecordService.getGrowthRecordById(id));
    }

    @Operation(summary = "获取成长档案分页")
    @GetMapping
    public Result<PageResult<GrowthRecord>> getGrowthRecordPage(
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String studentId,
            @RequestParam(required = false) String type) {
        String tenantId = SecurityUtils.getCurrentTenantId();
        Page<GrowthRecord> page = growthRecordService.getGrowthRecordPage(tenantId, pageNum, pageSize, studentId, type);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "删除成长档案")
    @DeleteMapping("/{id}")
    public Result<Void> deleteGrowthRecord(@PathVariable String id) {
        growthRecordService.deleteGrowthRecord(id);
        return Result.success();
    }

}
