package com.lesingle.edu.controller.parent;

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.response.ChildInfoResponse;
import com.lesingle.edu.dto.response.ChildProfileResponse;
import com.lesingle.edu.dto.response.LessonRecordResponse;
import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.enums.UserRole;
import com.lesingle.edu.service.ParentAuthService;
import com.lesingle.edu.service.ParentChildService;
import com.lesingle.edu.service.ParentLessonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Parent - Child", description = "Child Information APIs for Parent")
@RestController
@RequestMapping("/api/v1/parent/children")
@RequiredArgsConstructor
@RequireRole(UserRole.PARENT)
public class ParentChildController {

    private final ParentChildService parentChildService;
    private final ParentAuthService parentAuthService;
    private final ParentLessonService parentLessonService;

    @Operation(summary = "Get my children")
    @GetMapping
    public Result<List<ChildInfoResponse>> getMyChildren() {
        Long parentId = SecurityUtils.getCurrentUserId();
        List<ChildInfoResponse> children = parentChildService.getChildrenForParent(parentId);
        return Result.success(children);
    }

    @Operation(summary = "Get child by ID")
    @GetMapping("/{id}")
    public Result<ChildProfileResponse> getChild(@PathVariable Long id) {
        Long parentId = SecurityUtils.getCurrentUserId();
        ChildProfileResponse profile = parentChildService.getChildProfile(parentId, id);
        return Result.success(profile);
    }

    @Operation(summary = "Get child lesson records (reading history)")
    @GetMapping("/{id}/lessons")
    public Result<PageResult<LessonRecordResponse>> getChildLessons(
            @PathVariable Long id,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Long parentId = SecurityUtils.getCurrentUserId();
        parentAuthService.validateParentStudentRelation(parentId, id);
        PageResult<LessonRecordResponse> result = parentLessonService.getLessonsByStudentId(id, parentId, pageNum, pageSize);
        return Result.success(result);
    }

    @Operation(summary = "Get child growth records")
    @GetMapping("/{id}/growth")
    public Result<Object> getChildGrowth(@PathVariable Long id) {
        // 已迁移至 ParentGrowthController: GET /parent/growth-records/student/:studentId
        return Result.success(null);
    }

}
