<?php

namespace App\Models;

use CodeIgniter\Model;

class QuranSurahModel extends Model
{
    protected $table            = 'quran_surahs';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'number', 'name_arabic', 'name_latin', 'name_id', 'total_ayat', 'juz_start', 'revelation'
    ];

    protected $useTimestamps = false;

    /**
     * Ambil semua surah dikelompokkan per juz.
     * Return: [juz_number => [surah, surah, ...], ...]
     */
    public function getAllGroupedByJuz(): array
    {
        $surahs = $this->orderBy('number', 'ASC')->findAll();
        $grouped = [];
        foreach ($surahs as $s) {
            $grouped[$s['juz_start']][] = $s;
        }
        return $grouped;
    }

    /**
     * Ambil lookup: id => data surah (untuk validasi ayat)
     */
    public function getLookup(): array
    {
        $surahs = $this->findAll();
        $lookup = [];
        foreach ($surahs as $s) {
            $lookup[$s['id']] = $s;
        }
        return $lookup;
    }
}
