<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\Surah;
use App\Models\Ayat;
use App\Models\Juz;
use App\Models\Sajdah;
use Illuminate\Http\Request;

class QuranController extends Controller
{
    /**
     * Display Quran dashboard overview
     */
    public function index()
    {
        $stats = [
            'surahs' => Surah::count(),
            'ayats' => Ayat::count(),
            'juzs' => Juz::count(),
            'sajdahs' => Sajdah::count(),
        ];

        return view('dashboard.quran.index', compact('stats'));
    }

    /**
     * Display all Surahs
     */
    public function surahs()
    {
        $surahs = Surah::orderBy('surah_no')->paginate(20);
        return view('dashboard.quran.surahs', compact('surahs'));
    }

    /**
     * Display a specific Surah with its Ayats
     */
    public function showSurah($id)
    {
        $surah = Surah::with('ayats')->where('surah_no', $id)->firstOrFail();
        $ayats = $surah->ayats()->orderBy('ayah_no')->get();
        return view('dashboard.quran.surah-detail', compact('surah', 'ayats'));
    }

    /**
     * Display all Ayats
     */
    public function ayats(Request $request)
    {
        $query = Ayat::with('surah');
        
        if ($request->has('surah_id')) {
            $query->where('surah_id', $request->surah_id);
        }
        
        $ayats = $query->orderBy('surah_id')->orderBy('ayah_no')->paginate(50);
        $surahs = Surah::orderBy('surah_no')->get();
        
        return view('dashboard.quran.ayats', compact('ayats', 'surahs'));
    }

    /**
     * Display all Juzs
     */
    public function juzs()
    {
        $juzs = Juz::orderBy('juz_number')->get();
        return view('dashboard.quran.juzs', compact('juzs'));
    }

    /**
     * Display a specific Juz
     */
    public function showJuz($id)
    {
        $juz = Juz::where('juz_number', $id)->firstOrFail();
        return view('dashboard.quran.juz-detail', compact('juz'));
    }

    /**
     * Display all Sajdahs
     */
    public function sajdahs()
    {
        $sajdahs = Sajdah::with(['surah', 'ayat'])->orderBy('ayah_number')->paginate(20);
        return view('dashboard.quran.sajdahs', compact('sajdahs'));
    }
}
