package com.reading.platform.controller.parent;

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.*;

import java.util.List;

@Tag(name = "Parent - Growth Record", description = "Growth Record APIs for Parent")
@RestController
@RequestMapping("/api/parent/growth-records")
@RequiredArgsConstructor
public class ParentGrowthController {

    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, "parent", 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 records by student ID")
    @GetMapping("/student/{studentId}")
    public Result<PageResult<GrowthRecord>> getGrowthRecordsByStudent(
            @PathVariable Long studentId,
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String type) {
        Page<GrowthRecord> page = growthRecordService.getGrowthRecordsByStudentId(studentId, pageNum, pageSize, type);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Get recent growth records")
    @GetMapping("/student/{studentId}/recent")
    public Result<List<GrowthRecord>> getRecentGrowthRecords(
            @PathVariable Long studentId,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return Result.success(growthRecordService.getRecentGrowthRecords(studentId, limit));
    }

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

}
