<?php

namespace App\Http\Controllers\Api;

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

class StudentProgressController extends Controller
{
    protected StudentProgressService $progressService;

    public function __construct(StudentProgressService $progressService)
    {
        $this->progressService = $progressService;
    }

    /**
     * 获取学生学习进度
     *
     * @param string $studentId
     * @return JsonResponse
     */
    public function getLearningProgress(string $studentId): JsonResponse
    {
        try {
            Log::info('获取学生学习进度', ['student_id' => $studentId]);

            $result = $this->progressService->calculateLearningProgress($studentId);

            if ($result['success']) {
                return response()->json([
                    'success' => true,
                    'data' => $result['data'],
                    'message' => '学习进度计算成功'
                ]);
            }

            return response()->json([
                'success' => false,
                'message' => $result['error'] ?? '计算学习进度失败'
            ], 400);

        } catch (\Exception $e) {
            Log::error('获取学生学习进度失败', [
                'student_id' => $studentId,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);

            return response()->json([
                'success' => false,
                'message' => '获取学习进度失败: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * 获取学生知识点掌握度详情
     *
     * @param string $studentId
     * @return JsonResponse
     */
    public function getKnowledgePointDetails(string $studentId): JsonResponse
    {
        try {
            Log::info('获取学生知识点掌握度详情', ['student_id' => $studentId]);

            $details = $this->progressService->getKnowledgePointDetails($studentId);

            return response()->json([
                'success' => true,
                'data' => $details,
                'message' => '获取知识点详情成功'
            ]);

        } catch (\Exception $e) {
            Log::error('获取学生知识点掌握度详情失败', [
                'student_id' => $studentId,
                'error' => $e->getMessage()
            ]);

            return response()->json([
                'success' => false,
                'message' => '获取知识点详情失败: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * 批量获取学生学习进度
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function batchGetLearningProgress(Request $request): JsonResponse
    {
        try {
            $studentIds = $request->input('student_ids', []);
            $studentIds = array_filter(array_map('trim', $studentIds));

            if (empty($studentIds)) {
                return response()->json([
                    'success' => false,
                    'message' => '学生ID列表不能为空'
                ], 400);
            }

            if (count($studentIds) > 100) {
                return response()->json([
                    'success' => false,
                    'message' => '单次最多查询100个学生'
                ], 400);
            }

            Log::info('批量获取学生学习进度', ['student_ids' => $studentIds]);

            $results = $this->progressService->batchCalculateLearningProgress($studentIds);

            return response()->json([
                'success' => true,
                'data' => $results,
                'message' => '批量获取学习进度成功'
            ]);

        } catch (\Exception $e) {
            Log::error('批量获取学生学习进度失败', [
                'error' => $e->getMessage(),
                'student_ids' => $request->input('student_ids', [])
            ]);

            return response()->json([
                'success' => false,
                'message' => '批量获取学习进度失败: ' . $e->getMessage()
            ], 500);
        }
    }
}