<?php

namespace App\Http\Controllers\Api;

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

class SajdahController extends Controller
{
    /**
     * Get all Sajdahs
     */
    public function index()
    {
        $sajdahs = Sajdah::with(['surah', 'ayat'])
            ->orderBy('ayah_number')
            ->get()
            ->map(function ($sajdah) {
                return [
                    'number' => $sajdah->ayah_number,
                    'text' => $sajdah->arabic_text,
                    'surah' => [
                        'number' => $sajdah->surah->surah_no,
                        'name' => $sajdah->surah->surah_name_arabic,
                        'englishName' => $sajdah->surah->surah_name,
                        'englishNameTranslation' => $sajdah->surah->surah_name_translation,
                        'revelationType' => ucfirst(strtolower($sajdah->surah->revelation_place)),
                        'numberOfAyahs' => $sajdah->surah->total_ayah,
                    ],
                    'numberInSurah' => $sajdah->number_in_surah,
                    'juz' => $sajdah->juz,
                    'manzil' => $sajdah->manzil,
                    'page' => $sajdah->page,
                    'ruku' => $sajdah->ruku,
                    'hizbQuarter' => $sajdah->hizb_quarter,
                    'sajda' => [
                        'id' => $sajdah->id,
                        'recommended' => $sajdah->recommended,
                        'obligatory' => $sajdah->obligatory,
                    ],
                ];
            });

        // Return in the same format as the original API
        return response()->json([
            'code' => 200,
            'status' => 'OK',
            'data' => [
                'ayahs' => $sajdahs,
                'edition' => [
                    'identifier' => 'quran-uthmani',
                    'language' => 'ar',
                    'name' => 'القرآن الكريم برسم العثماني (uthmani)',
                    'englishName' => 'Uthmani',
                    'format' => 'text',
                    'type' => 'quran',
                    'direction' => 'rtl',
                ],
            ],
        ]);
    }

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

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $sajdah->id,
                'ayah_number' => $sajdah->ayah_number,
                'number_in_surah' => $sajdah->number_in_surah,
                'arabic_text' => $sajdah->arabic_text,
                'recommended' => $sajdah->recommended,
                'obligatory' => $sajdah->obligatory,
                'surah' => [
                    'id' => $sajdah->surah->id,
                    'surah_no' => $sajdah->surah->surah_no,
                    'surah_name' => $sajdah->surah->surah_name,
                    'surah_name_arabic' => $sajdah->surah->surah_name_arabic,
                ],
                'juz' => $sajdah->juz,
                'manzil' => $sajdah->manzil,
                'page' => $sajdah->page,
                'ruku' => $sajdah->ruku,
                'hizb_quarter' => $sajdah->hizb_quarter,
            ],
        ]);
    }
}
