<?php

namespace App\Http\Controllers\Api;

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

class AyatController extends Controller
{
    /**
     * Get all Ayats (with pagination)
     */
    public function index(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);
        
        return response()->json([
            'success' => true,
            'data' => $ayats,
        ]);
    }

    /**
     * Get a specific Ayat
     */
    public function show($id)
    {
        $ayat = Ayat::with('surah')->find($id);
        
        if (!$ayat) {
            return response()->json([
                'success' => false,
                'message' => 'Ayat not found',
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $ayat->id,
                'surah_id' => $ayat->surah_id,
                'surah' => [
                    'id' => $ayat->surah->id,
                    'surah_no' => $ayat->surah->surah_no,
                    'surah_name' => $ayat->surah->surah_name,
                    'surah_name_arabic' => $ayat->surah->surah_name_arabic,
                ],
                'ayah_no' => $ayat->ayah_no,
                'ayah_number' => $ayat->ayah_number,
                'arabic_text' => $ayat->arabic_text,
                'arabic_text_simple' => $ayat->arabic_text_simple,
                'english_translation' => $ayat->english_translation,
                'transliteration' => $ayat->transliteration,
                'juz' => $ayat->juz,
                'manzil' => $ayat->manzil,
                'page' => $ayat->page,
                'ruku' => $ayat->ruku,
                'hizb_quarter' => $ayat->hizb_quarter,
            ],
        ]);
    }

    /**
     * Get Ayats by Surah
     */
    public function bySurah($surahId)
    {
        $surah = Surah::find($surahId);
        
        if (!$surah) {
            return response()->json([
                'success' => false,
                'message' => 'Surah not found',
            ], 404);
        }

        $ayats = Ayat::where('surah_id', $surahId)
            ->orderBy('ayah_no')
            ->get()
            ->map(function ($ayat) {
                return [
                    'id' => $ayat->id,
                    'ayah_no' => $ayat->ayah_no,
                    'ayah_number' => $ayat->ayah_number,
                    'arabic_text' => $ayat->arabic_text,
                    'arabic_text_simple' => $ayat->arabic_text_simple,
                    'english_translation' => $ayat->english_translation,
                    'transliteration' => $ayat->transliteration,
                    'juz' => $ayat->juz,
                    'manzil' => $ayat->manzil,
                    'page' => $ayat->page,
                    'ruku' => $ayat->ruku,
                    'hizb_quarter' => $ayat->hizb_quarter,
                ];
            });

        return response()->json([
            'success' => true,
            'data' => $ayats,
        ]);
    }
}
