@extends('layouts.self-paced-dashboard')

@section('title', 'Import Questions — ' . $course->title)
@section('page-title', 'Import Exam Questions')
@section('page-subtitle', 'Upload a CSV file to bulk-import questions for ' . $course->title)

@section('hero-actions')
    <a href="{{ route('self-paced.dashboard.exams.course-questions', $course) }}"
       class="inline-flex items-center gap-2 px-4 py-2 bg-white/10 hover:bg-white/20 text-white text-sm font-medium rounded-lg transition border border-white/30">
        <i class="fas fa-arrow-left"></i> Back to Questions
    </a>
@endsection

@section('content')
<div class="max-w-5xl mx-auto">

    {{-- Breadcrumb --}}
    <nav class="flex items-center gap-2 text-xs text-gray-400 mb-4">
        <a href="{{ route('self-paced.dashboard.index') }}" class="hover:text-indigo-600 transition">Dashboard</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <a href="{{ route('self-paced.dashboard.exams.index') }}" class="hover:text-indigo-600 transition">Exams</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <a href="{{ route('self-paced.dashboard.exams.course-questions', $course) }}" class="hover:text-indigo-600 transition">{{ Str::limit($course->title, 40) }}</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <span class="text-gray-600 font-medium">Import CSV</span>
    </nav>

    {{-- Validation Errors --}}
    @if($errors->any())
    <div class="bg-red-50 border border-red-200 rounded-xl p-4 mb-5">
        <div class="flex items-start gap-3">
            <i class="fas fa-exclamation-circle text-red-500 mt-0.5"></i>
            <div>
                <p class="text-sm font-semibold text-red-700 mb-1">Please fix the following errors:</p>
                <ul class="text-sm text-red-600 space-y-0.5 list-disc list-inside">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
    @endif

    {{-- CSV Format Instructions --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 mb-5">
        <h2 class="text-base font-semibold text-gray-800 mb-4 flex items-center gap-2">
            <i class="fas fa-info-circle text-blue-500"></i> CSV Format Instructions
        </h2>

        <p class="text-sm text-gray-600 mb-4">
            Your CSV file must have a header row followed by one question per row.
            The columns must be in the following order:
        </p>

        <div class="overflow-x-auto rounded-lg border border-gray-200 mb-4">
            <table class="w-full text-xs">
                <thead>
                    <tr>
                        <th class="bg-indigo-50 text-indigo-700 font-semibold text-left px-3 py-2">Column</th>
                        <th class="bg-purple-50 text-purple-700 font-semibold text-left px-3 py-2">Field</th>
                        <th class="bg-blue-50 text-blue-700 font-semibold text-left px-3 py-2">Required</th>
                        <th class="bg-green-50 text-green-700 font-semibold text-left px-3 py-2">Notes</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <tr class="bg-white">
                        <td class="px-3 py-2 font-mono text-gray-700">1</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">question_text</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">The full question text</td>
                    </tr>
                    <tr class="bg-gray-50/50">
                        <td class="px-3 py-2 font-mono text-gray-700">2</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">option_a</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">Text for option A</td>
                    </tr>
                    <tr class="bg-white">
                        <td class="px-3 py-2 font-mono text-gray-700">3</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">option_b</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">Text for option B</td>
                    </tr>
                    <tr class="bg-gray-50/50">
                        <td class="px-3 py-2 font-mono text-gray-700">4</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">option_c</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">Text for option C</td>
                    </tr>
                    <tr class="bg-white">
                        <td class="px-3 py-2 font-mono text-gray-700">5</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">option_d</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">Text for option D</td>
                    </tr>
                    <tr class="bg-gray-50/50">
                        <td class="px-3 py-2 font-mono text-gray-700">6</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">correct_answer</td>
                        <td class="px-3 py-2"><span class="text-red-600 font-semibold">Yes</span></td>
                        <td class="px-3 py-2 text-gray-500">Must be exactly: <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, or <strong>D</strong></td>
                    </tr>
                    <tr class="bg-white">
                        <td class="px-3 py-2 font-mono text-gray-700">7</td>
                        <td class="px-3 py-2 font-mono text-indigo-600">explanation</td>
                        <td class="px-3 py-2"><span class="text-gray-400">Optional</span></td>
                        <td class="px-3 py-2 text-gray-500">Explanation shown after exam (can be blank)</td>
                    </tr>
                </tbody>
            </table>
        </div>

        {{-- Sample CSV preview --}}
        <div class="bg-gray-900 rounded-lg p-4 text-xs font-mono text-green-400 overflow-x-auto mb-4">
            <p class="text-gray-400 mb-1"># Example CSV content:</p>
            <p>question_text,option_a,option_b,option_c,option_d,correct_answer,explanation</p>
            <p>What is the capital of Uganda?,Kampala,Nairobi,Dar es Salaam,Kigali,A,Kampala is the capital city of Uganda.</p>
            <p>Which year was Uganda founded?,1962,1960,1964,1958,A,Uganda gained independence in 1962.</p>
        </div>

        {{-- Download sample template --}}
        <div class="flex items-center gap-3 p-3 bg-green-50 border border-green-100 rounded-lg">
            <i class="fas fa-file-csv text-green-600 text-lg flex-shrink-0"></i>
            <div class="flex-1">
                <p class="text-xs font-semibold text-green-800">Download Sample Template</p>
                <p class="text-xs text-green-600">Use this template to prepare your questions correctly.</p>
            </div>
            <a href="data:text/csv;charset=utf-8,question_text%2Coption_a%2Coption_b%2Coption_c%2Coption_d%2Ccorrect_answer%2Cexplanation%0AWhat+is+the+capital+of+Uganda%3F%2CKampala%2CNairobi%2CDar+es+Salaam%2CKigali%2CA%2CKampala+is+the+capital+city+of+Uganda."
               download="exam_questions_template.csv"
               class="inline-flex items-center gap-1.5 px-3 py-1.5 bg-green-600 hover:bg-green-700 text-white text-xs font-semibold rounded-lg transition flex-shrink-0">
                <i class="fas fa-download"></i> Download
            </a>
        </div>
    </div>

    {{-- Upload Form --}}
    <form action="{{ route('self-paced.dashboard.exams.import.submit', $course) }}"
          method="POST"
          enctype="multipart/form-data"
          x-data="{ fileName: '', dragging: false }"
          @dragover.prevent="dragging = true"
          @dragleave.prevent="dragging = false"
          @drop.prevent="dragging = false; const f = $event.dataTransfer.files[0]; if(f){ fileName = f.name; $refs.csvInput.files = $event.dataTransfer.files; }">
        @csrf

        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">

            <h2 class="text-base font-semibold text-gray-800 mb-5 flex items-center gap-2">
                <i class="fas fa-upload text-indigo-500"></i> Upload CSV File
            </h2>

            {{-- Drop zone --}}
            <div :class="dragging ? 'border-indigo-400 bg-indigo-50' : 'border-gray-200 bg-gray-50'"
                 class="border-2 border-dashed rounded-xl p-8 text-center transition cursor-pointer"
                 @click="$refs.csvInput.click()">

                <i class="fas fa-file-csv text-4xl mb-3 block"
                   :class="dragging ? 'text-indigo-500' : 'text-gray-300'"></i>

                <template x-if="!fileName">
                    <div>
                        <p class="text-sm font-medium text-gray-600">Drop your CSV file here, or <span class="text-indigo-600 underline">browse</span></p>
                        <p class="text-xs text-gray-400 mt-1">Accepted formats: .csv, .txt — Max size: 2MB</p>
                    </div>
                </template>
                <template x-if="fileName">
                    <div>
                        <p class="text-sm font-semibold text-indigo-700" x-text="fileName"></p>
                        <p class="text-xs text-gray-400 mt-1">File selected — click to change</p>
                    </div>
                </template>

                <input type="file"
                       name="csv_file"
                       accept=".csv,.txt"
                       x-ref="csvInput"
                       @change="fileName = $event.target.files[0]?.name || ''"
                       class="hidden">
            </div>

            @error('csv_file')
                <p class="text-red-500 text-xs mt-2">{{ $message }}</p>
            @enderror

            {{-- Import notes --}}
            <div class="mt-4 space-y-2">
                <div class="flex items-start gap-2 text-xs text-gray-500">
                    <i class="fas fa-check-circle text-green-500 mt-0.5 flex-shrink-0"></i>
                    <span>Rows with invalid <code class="bg-gray-100 px-1 rounded">correct_answer</code> values will be skipped and reported.</span>
                </div>
                <div class="flex items-start gap-2 text-xs text-gray-500">
                    <i class="fas fa-check-circle text-green-500 mt-0.5 flex-shrink-0"></i>
                    <span>All imported questions will be set to <strong>active</strong> by default.</span>
                </div>
                <div class="flex items-start gap-2 text-xs text-gray-500">
                    <i class="fas fa-check-circle text-green-500 mt-0.5 flex-shrink-0"></i>
                    <span>The first row (header) is always skipped automatically.</span>
                </div>
                <div class="flex items-start gap-2 text-xs text-gray-500">
                    <i class="fas fa-info-circle text-blue-400 mt-0.5 flex-shrink-0"></i>
                    <span>Importing does not replace existing questions — it adds to the question bank.</span>
                </div>
            </div>

        </div>

        <div class="flex justify-between mt-4">
            <a href="{{ route('self-paced.dashboard.exams.course-questions', $course) }}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-gray-100 hover:bg-gray-200 text-gray-700 text-sm font-semibold rounded-lg transition">
                <i class="fas fa-arrow-left"></i> Cancel
            </a>
            <button type="submit"
                    x-bind:disabled="!fileName"
                    :class="fileName ? 'bg-indigo-600 hover:bg-indigo-700 cursor-pointer' : 'bg-indigo-300 cursor-not-allowed'"
                    class="inline-flex items-center gap-2 px-6 py-2.5 text-white text-sm font-semibold rounded-lg transition shadow">
                <i class="fas fa-file-import"></i> Import Questions
            </button>
        </div>

    </form>
</div>
@endsection
