@props(['questions'])

@if(isset($questions) && !empty($questions))
<div class="bg-white rounded-lg shadow-sm border border-gray-200">
    <div class="p-4 border-b border-gray-200">
        <h2 class="text-lg font-semibold text-gray-900">📋 题目详情分析</h2>
    </div>
    <div class="divide-y divide-gray-200">
        @foreach($questions as $index => $question)
        <div class="p-4">
            <div class="flex items-center justify-between mb-3">
                <div class="flex items-center space-x-3">
                    <h4 class="font-medium text-gray-900">第 {{ $question['question_number'] ?? 'N/A' }} 题</h4>
                    @if(!empty($question['kp_code']))
                    <span class="px-2 py-1 bg-blue-100 text-blue-800 text-xs rounded">{{ $question['kp_code'] }}</span>
                    @endif
                </div>
                <div class="flex items-center space-x-2">
                    @if(isset($question['score_total']) && $question['score_total'] !== null)
                    <span class="text-sm text-gray-600">得分: {{ $question['score_obtained'] ?? 0 }} / {{ $question['score_total'] }}</span>
                    @endif
                    @if(($question['is_correct'] ?? false))
                        <span class="px-2 py-1 bg-green-100 text-green-800 text-xs rounded">✓ 正确</span>
                    @elseif(!empty($question['student_answer']) && $question['student_answer'] !== '未作答')
                        <span class="px-2 py-1 bg-red-100 text-red-800 text-xs rounded">✗ 错误</span>
                    @else
                        <span class="px-2 py-1 bg-gray-100 text-gray-600 text-xs rounded">未作答</span>
                    @endif
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-3 mb-3">
                <div>
                    <h5 class="text-sm font-medium text-gray-700 mb-2">📝 题目内容</h5>
                    <div class="bg-gray-50 rounded p-3">
                        <p class="text-sm text-gray-600">{{ Str::limit($question['question_text'] ?? 'N/A', 200) }}</p>
                    </div>
                </div>
                <div>
                    <h5 class="text-sm font-medium text-gray-700 mb-2">✏️ 老师评分</h5>
                    <div class="bg-blue-50 rounded p-3">
                        <div class="space-y-2">
                            <div>
                                <p class="text-xs text-gray-500">评分结果:</p>
                                <p class="text-sm text-gray-700">
                                    得分: <span class="font-semibold text-blue-600">{{ $question['score_obtained'] ?? 0 }}</span>
                                    / {{ $question['score_total'] ?? 5 }}
                                    @if(($question['is_correct'] ?? false))
                                        <span class="ml-2 px-2 py-0.5 bg-green-100 text-green-800 text-xs rounded">✓ 正确</span>
                                    @else
                                        <span class="ml-2 px-2 py-0.5 bg-red-100 text-red-800 text-xs rounded">✗ 错误</span>
                                    @endif
                                </p>
                            </div>
                            @if(isset($question['answer_comparison']))
                            <div class="border-t pt-2">
                                <p class="text-xs text-gray-500">正确答案（老师校准）:</p>
                                <p class="text-sm text-green-700 font-medium">{{ $question['answer_comparison']['correct'] }}</p>
                            </div>
                            @endif
                            @if(isset($question['reference_answer']))
                            <div class="border-t pt-2">
                                <p class="text-xs text-gray-500">参考答案:</p>
                                <p class="text-sm text-gray-600">{{ $question['reference_answer'] }}</p>
                            </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>

            @if(isset($question['ai_analysis']) && !empty($question['ai_analysis']))
            <div class="bg-purple-50 border border-purple-200 rounded-lg p-3">
                <h5 class="text-sm font-medium text-gray-700 mb-2">🤖 AI 分析</h5>

                @if($question['ai_analysis']['analysis'])
                    <p class="text-sm text-gray-700 mb-2">{{ $question['ai_analysis']['analysis'] }}</p>
                @endif

                @if($question['is_correct'])
                    {{-- 正确答题的AI反馈 --}}
                    @if(isset($question['ai_analysis']['suggestions']) && !empty($question['ai_analysis']['suggestions']))
                    <div>
                        <p class="text-xs font-medium text-green-700 mb-1">
                            @if(isset($question['answer_comparison']))
                                校准判断正确:
                            @else
                                继续加油:
                            @endif
                        </p>
                        <ul class="text-xs text-green-600 space-y-1">
                            @foreach($question['ai_analysis']['suggestions'] as $suggestion)
                            @if($suggestion)
                            <li class="flex items-start">
                                <span class="mr-1">✓</span>
                                <span>{{ $suggestion }}</span>
                            </li>
                            @endif
                            @endforeach
                        </ul>
                    </div>
                    @endif
                @else
                    {{-- 错误答题的AI反馈（基于老师评分） --}}
                    @if($question['ai_analysis']['mistake_type'] || $question['ai_analysis']['mistake_category'])
                    <div class="grid grid-cols-2 gap-2 mb-2">
                        @if($question['ai_analysis']['mistake_type'])
                            <div class="text-xs">
                                <span class="font-medium text-red-700">错误类型: </span>
                                <span class="text-gray-600">{{ $question['ai_analysis']['mistake_type'] }}</span>
                            </div>
                        @endif
                        @if($question['ai_analysis']['mistake_category'])
                            <div class="text-xs">
                                <span class="font-medium text-red-700">错误类别: </span>
                                <span class="text-gray-600">{{ $question['ai_analysis']['mistake_category'] }}</span>
                            </div>
                        @endif
                    </div>
                    @endif

                    @if($question['ai_analysis']['correct_solution'] || isset($question['solution']))
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-2 mb-2">
                        @if(isset($question['solution']))
                        <div>
                            <p class="text-xs font-medium text-blue-700 mb-1">解题步骤:</p>
                            <p class="text-xs text-blue-600 bg-blue-50 rounded p-2">{{ $question['solution'] }}</p>
                        </div>
                        @endif
                        @if($question['ai_analysis']['correct_solution'])
                        <div>
                            <p class="text-xs font-medium text-green-700 mb-1">正确答案:</p>
                            <p class="text-xs text-green-600 bg-green-100 rounded p-2">{{ $question['ai_analysis']['correct_solution'] }}</p>
                        </div>
                        @endif
                    </div>
                    @endif

                    @if(isset($question['ai_analysis']['suggestions']) && !empty($question['ai_analysis']['suggestions']))
                    <div>
                        <p class="text-xs font-medium text-purple-700 mb-1">
                            @if(isset($question['answer_comparison']))
                                校准后学习建议:
                            @else
                                学习建议:
                            @endif
                        </p>
                        <ul class="text-xs text-purple-600 space-y-1">
                            @foreach($question['ai_analysis']['suggestions'] as $suggestion)
                            @if($suggestion)
                            <li class="flex items-start">
                                <span class="mr-1">•</span>
                                <span>{{ $suggestion }}</span>
                            </li>
                            @endif
                            @endforeach
                        </ul>
                    </div>
                    @endif
                @endif
            </div>
            @endif
        </div>
        @endforeach
    </div>
</div>
@endif
