<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Exam;
use App\Models\Question;
use App\Imports\QuestionsImport;
use Maatwebsite\Excel\Facades\Excel;

class QuestionController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @param  \App\Models\Exam  $exam
     * @return \Inertia\Response
     */
    public function create(Exam $exam)
    {
        return inertia('Admin/Questions/Create', [
            'exam' => $exam,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Exam  $exam
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(\App\Http\Requests\StoreQuestionRequest $request, Exam $exam)
    {

        $imageName = null;
        if ($request->hasFile('image')) {
            $imageName = basename($request->file('image')->store('questions', 'public'));
        }

        Question::create([
            'exam_id' => $exam->id,
            'question' => $request->question,
            'image' => $imageName,
            'option_1' => $request->option_1,
            'option_2' => $request->option_2,
            'option_3' => $request->option_3,
            'option_4' => $request->option_4,
            'option_5' => $request->option_5,
            'answer' => $request->answer,
        ]);

        return redirect()->route('admin.exams.show', $exam->id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Exam  $exam
     * @param  \App\Models\Question  $question
     * @return \Inertia\Response
     */
    public function edit(Exam $exam, Question $question)
    {
        return inertia('Admin/Questions/Edit', [
            'exam' => $exam,
            'question' => $question,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Exam  $exam
     * @param  \App\Models\Question  $question
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(\App\Http\Requests\UpdateQuestionRequest $request, Exam $exam, Question $question)
    {

        $imageName = $question->image;
        if ($request->hasFile('image')) {
            $oldImageName = basename($question->image);
            if ($oldImageName && \Illuminate\Support\Facades\Storage::disk('public')->exists('questions/' . $oldImageName)) {
                \Illuminate\Support\Facades\Storage::disk('public')->delete('questions/' . $oldImageName);
            }
            $imageName = basename($request->file('image')->store('questions', 'public'));
        }

        if ($request->remove_image) {
            $oldImageName = basename($question->image);
            if ($oldImageName && \Illuminate\Support\Facades\Storage::disk('public')->exists('questions/' . $oldImageName)) {
                \Illuminate\Support\Facades\Storage::disk('public')->delete('questions/' . $oldImageName);
            }
            $imageName = null;
        }

        $question->update([
            'question' => $request->question,
            'image' => $imageName,
            'option_1' => $request->option_1,
            'option_2' => $request->option_2,
            'option_3' => $request->option_3,
            'option_4' => $request->option_4,
            'option_5' => $request->option_5,
            'answer' => $request->answer,
        ]);

        return redirect()->route('admin.exams.show', $exam->id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Exam  $exam
     * @param  \App\Models\Question  $question
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Exam $exam, Question $question)
    {
        $oldImageName = basename($question->image);
        if ($oldImageName && \Illuminate\Support\Facades\Storage::disk('public')->exists('questions/' . $oldImageName)) {
            \Illuminate\Support\Facades\Storage::disk('public')->delete('questions/' . $oldImageName);
        }

        $question->delete();
        return redirect()->route('admin.exams.show', $exam->id);
    }

    /**
     * Show import form for questions.
     *
     * @param  \App\Models\Exam  $exam
     * @return \Inertia\Response
     */
    public function import(Exam $exam)
    {
        return inertia('Admin/Questions/Import', [
            'exam' => $exam
        ]);
    }

    /**
     * Handle question import.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Exam  $exam
     * @return \Illuminate\Http\RedirectResponse
     */
    public function storeImport(Request $request, Exam $exam)
    {
        $request->validate([
            'file' => 'required|mimes:csv,xls,xlsx'
        ]);

        Excel::import(new QuestionsImport($exam->id), $request->file('file'));

        return redirect()->route('admin.exams.show', $exam->id);
    }

    /**
     * Download format import excel.
     *
     * @param  \App\Models\Exam  $exam
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function exportFormat(Exam $exam)
    {
        return Excel::download(new \App\Exports\QuestionFormatExport($exam->id), 'format-import-soal.xlsx');
    }
}
