<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\InterviewCategory;
use App\Models\InterviewFlashcard;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;

class InterviewFlashcardController extends Controller
{
    public function index(Request $request): View
    {
        $query = InterviewFlashcard::with('category');

        if ($request->filled('category_id')) {
            $query->where('category_id', $request->category_id);
        }
        if ($request->filled('search')) {
            $query->where('title', 'like', '%' . $request->search . '%');
        }

        $flashcards = $query->orderBy('order')->paginate(20)->withQueryString();
        $categories = InterviewCategory::orderBy('name')->get();

        return view('admin.interview-flashcards.index', compact('flashcards', 'categories'));
    }

    public function create(): View
    {
        $categories = InterviewCategory::orderBy('name')->get();
        return view('admin.interview-flashcards.create', compact('categories'));
    }

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'category_id'  => 'required|exists:interview_categories,id',
            'title'        => 'required|string|max:255',
            'content'      => 'required|string',
            'back_content' => 'required|string',
            'order'        => 'nullable|integer|min:0',
        ]);

        $validated['order'] = $validated['order'] ?? 0;

        InterviewFlashcard::create($validated);

        return redirect()->route('admin.interview-flashcards.index', ['category_id' => $validated['category_id']])
            ->with('success', 'Flashcard başarıyla oluşturuldu.');
    }

    public function edit(InterviewFlashcard $flashcard): View
    {
        $categories = InterviewCategory::orderBy('name')->get();
        return view('admin.interview-flashcards.edit', compact('flashcard', 'categories'));
    }

    public function update(Request $request, InterviewFlashcard $flashcard): RedirectResponse
    {
        $validated = $request->validate([
            'category_id'  => 'required|exists:interview_categories,id',
            'title'        => 'required|string|max:255',
            'content'      => 'required|string',
            'back_content' => 'required|string',
            'order'        => 'nullable|integer|min:0',
        ]);

        $validated['order'] = $validated['order'] ?? 0;

        $flashcard->update($validated);

        return redirect()->route('admin.interview-flashcards.index', ['category_id' => $validated['category_id']])
            ->with('success', 'Flashcard başarıyla güncellendi.');
    }

    public function destroy(InterviewFlashcard $flashcard): RedirectResponse
    {
        $flashcard->delete();

        return redirect()->route('admin.interview-flashcards.index')
            ->with('success', 'Flashcard başarıyla silindi.');
    }
}
