<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\StudentProgressService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class StudentProgressController extends Controller
{
    public function __construct(
        private StudentProgressService $service
    ) {}

    /**
     * 获取单个学生的学习进度
     * GET /api/students/{studentId}/learning-progress
     */
    public function show(string $studentId): JsonResponse
    {
        try {
            $result = $this->service->getProgress($studentId);

            if (!$result['success']) {
                return response()->json($result, 400);
            }

            return response()->json($result);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '计算学习进度失败: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * 批量获取学生学习进度
     * POST /api/students/learning-progress/batch
     */
    public function batch(Request $request): JsonResponse
    {
        try {
            $studentIds = $request->input('student_ids', []);
            $result = $this->service->getBatchProgress($studentIds);

            if (!$result['success']) {
                return response()->json($result, 400);
            }

            return response()->json($result);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '批量计算学习进度失败: ' . $e->getMessage()
            ], 500);
        }
    }
}
