package com.lesingle.edu.controller.teacher;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.mapper.GrowthRecordMapper;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.request.GrowthRecordCreateRequest;
import com.lesingle.edu.dto.request.GrowthRecordUpdateRequest;
import com.lesingle.edu.dto.response.GrowthRecordResponse;
import com.lesingle.edu.entity.GrowthRecord;
import com.lesingle.edu.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.*;

import java.util.List;

@Tag(name = "教师端 - 成长记录", description = "教师端成长记录 API")
@RestController
@RequestMapping("/api/v1/teacher/growth-records")
@RequiredArgsConstructor
public class TeacherGrowthController {

    private final GrowthRecordService growthRecordService;
    private final GrowthRecordMapper growthRecordMapper;

    @Operation(summary = "创建成长记录")
    @Log(module = LogModule.GROWTH, type = LogOperationType.CREATE, description = "创建成长记录")
    @PostMapping
    public Result<GrowthRecordResponse> createGrowthRecord(@Valid @RequestBody GrowthRecordCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Long userId = SecurityUtils.getCurrentUserId();
        GrowthRecord record = growthRecordService.createGrowthRecord(tenantId, userId, "teacher", request);
        return Result.success(growthRecordMapper.toVO(record));
    }

    @Operation(summary = "更新成长记录")
    @Log(module = LogModule.GROWTH, type = LogOperationType.UPDATE, description = "更新成长记录")
    @PutMapping("/{id}")
    public Result<GrowthRecordResponse> updateGrowthRecord(@PathVariable Long id, @RequestBody GrowthRecordUpdateRequest request) {
        GrowthRecord record = growthRecordService.updateGrowthRecord(id, request);
        return Result.success(growthRecordMapper.toVO(record));
    }

    @Operation(summary = "根据 ID 获取成长记录")
    @GetMapping("/{id}")
    public Result<GrowthRecordResponse> getGrowthRecord(@PathVariable Long id) {
        GrowthRecord record = growthRecordService.getGrowthRecordById(id);
        return Result.success(growthRecordMapper.toVO(record));
    }

    @Operation(summary = "获取成长记录分页列表")
    @GetMapping
    public Result<PageResult<GrowthRecordResponse>> getGrowthRecordPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) String type) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Page<GrowthRecord> page = growthRecordService.getGrowthRecordPage(tenantId, pageNum, pageSize, studentId, type);
        List<GrowthRecordResponse> voList = growthRecordMapper.toVO(page.getRecords());
        return Result.success(PageResult.of(voList, page.getTotal(), page.getCurrent(), page.getSize()));
    }

    @Operation(summary = "删除成长记录")
    @Log(module = LogModule.GROWTH, type = LogOperationType.DELETE, description = "删除成长记录")
    @DeleteMapping("/{id}")
    public Result<Void> deleteGrowthRecord(@PathVariable Long id) {
        growthRecordService.deleteGrowthRecord(id);
        return Result.success();
    }

}
