package com.reading.platform.controller.teacher;

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 = "Teacher - Growth Record", description = "Growth Record APIs for Teacher")
@RestController
@RequestMapping("/api/teacher/growth-records")
@RequiredArgsConstructor
public class TeacherGrowthController {

    private final GrowthRecordService growthRecordService;

    @Operation(summary = "Create growth record")
    @PostMapping
    public Result<GrowthRecord> createGrowthRecord(@Valid @RequestBody GrowthRecordCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Long userId = SecurityUtils.getCurrentUserId();
        return Result.success(growthRecordService.createGrowthRecord(tenantId, userId, "teacher", request));
    }

    @Operation(summary = "Update growth record")
    @PutMapping("/{id}")
    public Result<GrowthRecord> updateGrowthRecord(@PathVariable Long id, @RequestBody GrowthRecordUpdateRequest request) {
        return Result.success(growthRecordService.updateGrowthRecord(id, request));
    }

    @Operation(summary = "Get growth record by ID")
    @GetMapping("/{id}")
    public Result<GrowthRecord> getGrowthRecord(@PathVariable Long id) {
        return Result.success(growthRecordService.getGrowthRecordById(id));
    }

    @Operation(summary = "Get growth record page")
    @GetMapping
    public Result<PageResult<GrowthRecord>> getGrowthRecordPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) 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);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete growth record")
    @DeleteMapping("/{id}")
    public Result<Void> deleteGrowthRecord(@PathVariable Long id) {
        growthRecordService.deleteGrowthRecord(id);
        return Result.success();
    }

}
