<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\Hadees;
use App\Models\HadithCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class HadithController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $hadiths = Hadees::with('category')->orderBy('created_at', 'desc')->get();
        return view('dashboard.hadith.index', compact('hadiths'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $categories = HadithCategory::all();
        return view('dashboard.hadith.create', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'hadith_category_id' => 'required|exists:hadith_categories,id',
            'topic' => 'required|string|max:255',
            'topic_arabic' => 'required|string|max:255',
            'narrator' => 'required|string',
            'arabic_text' => 'required|string',
            'english_translation' => 'required|string',
            'source' => 'required|string|max:255',
            'grade' => 'nullable|string|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        try {
            Hadees::create($request->all());

            return redirect()
                ->route('hadith.index')
                ->with('success', 'Hadith created successfully!');

        } catch (\Exception $e) {
            return redirect()
                ->back()
                ->withInput()
                ->with('error', 'Error creating hadith: '.$e->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $hadith = Hadees::findOrFail($id);
        $categories = HadithCategory::all();
        return view('dashboard.hadith.edit', compact('hadith', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'hadith_category_id' => 'required|exists:hadith_categories,id',
            'topic' => 'required|string|max:255',
            'topic_arabic' => 'required|string|max:255',
            'narrator' => 'required|string',
            'arabic_text' => 'required|string',
            'english_translation' => 'required|string',
            'source' => 'required|string|max:255',
            'grade' => 'nullable|string|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        try {
            $hadith = Hadees::findOrFail($id);
            $hadith->update($request->all());

            return redirect()
                ->route('hadith.index')
                ->with('success', 'Hadith updated successfully!');

        } catch (\Exception $e) {
            return redirect()
                ->back()
                ->withInput()
                ->with('error', 'Error updating hadith: '.$e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $hadith = Hadees::findOrFail($id);
            $hadith->delete();

            return redirect()
                ->route('hadith.index')
                ->with('success', 'Hadith deleted successfully!');

        } catch (\Exception $e) {
            return redirect()
                ->route('hadith.index')
                ->with('error', 'Error deleting hadith: '.$e->getMessage());
        }
    }
}
