<?php

namespace App\Http\Controllers\Api;

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

class SurahController extends Controller
{
    /**
     * Get all Surahs (list)
     */
    public function index()
    {
        // Only select needed columns for better performance
        $surahs = Surah::select([
            'surah_no',
            'surah_name',
            'surah_name_arabic',
            'surah_name_arabic_long',
            'surah_name_translation',
            'revelation_place',
            'total_ayah'
        ])
        ->orderBy('surah_no')
        ->get()
        ->map(function ($surah) {
            return [
                'surahName' => $surah->surah_name,
                'surahNameArabic' => $surah->surah_name_arabic,
                'surahNameArabicLong' => $surah->surah_name_arabic_long,
                'surahNameTranslation' => $surah->surah_name_translation,
                'revelationPlace' => $surah->revelation_place,
                'totalAyah' => $surah->total_ayah,
            ];
        });

        return response()->json($surahs);
    }

    /**
     * Get a specific Surah with all details
     */
    public function show($id)
    {
        // Load surah with only needed columns
        $surah = Surah::select([
            'id',
            'surah_no',
            'surah_name',
            'surah_name_arabic',
            'surah_name_arabic_long',
            'surah_name_translation',
            'revelation_place',
            'total_ayah',
            'audio'
        ])
        ->where('surah_no', $id)
        ->first();
        
        if (!$surah) {
            return response()->json([
                'success' => false,
                'message' => 'Surah not found',
            ], 404);
        }

        // Eager load only needed ayat columns, ordered by ayah_no
        // This is much faster than loading all columns and then filtering
        $ayats = $surah->ayats()
            ->select([
                'ayah_no',
                'arabic_text',
                'arabic_text_simple',
                'english_translation',
                'transliteration'
            ])
            ->orderBy('ayah_no')
            ->get();
        
        // Single-pass data transformation for better performance
        $english = [];
        $arabic1 = [];
        $arabic2 = [];
        $transliterationArray = [];
        
        foreach ($ayats as $ayat) {
            $english[] = $ayat->english_translation;
            $arabic1[] = $ayat->arabic_text;
            $arabic2[] = $ayat->arabic_text_simple;
            
            if ($ayat->transliteration) {
                $transliterationArray[$ayat->ayah_no] = $ayat->transliteration;
            }
        }
        
        $response = [
            'surahName' => $surah->surah_name,
            'surahNameArabic' => $surah->surah_name_arabic,
            'surahNameArabicLong' => $surah->surah_name_arabic_long,
            'surahNameTranslation' => $surah->surah_name_translation,
            'revelationPlace' => $surah->revelation_place,
            'totalAyah' => $surah->total_ayah,
            'surahNo' => $surah->surah_no,
            'audio' => $surah->audio,
            'english' => $english,
            'arabic1' => $arabic1,
            'arabic2' => $arabic2,
        ];

        // Add transliteration if available
        if (!empty($transliterationArray)) {
            $response['transliteration'] = $transliterationArray;
        }

        return response()->json($response);
    }
}
