<?php

use App\Models\SchoolSettingModel;

function getLogo($type = 'sekolah')
{
    $model = new SchoolSettingModel();
    $setting = $model->getSettings();
    
    $logoField = ($type == 'kemenag') ? 'logo_kemenag' : 'logo_sekolah';
    $logo = $setting[$logoField] ?? null;
    
    if ($logo && file_exists(FCPATH . 'uploads/logos/' . $logo)) {
        return base_url('uploads/logos/' . $logo);
    }
    
    // Default logo jika belum upload
    if ($type == 'kemenag') {
        return base_url('assets/img/logo-kemenag-default.png');
    }
    
    return base_url('assets/img/logo-sekolah-default.png');
}

if (!function_exists('format_person_name')) {
    /**
     * Format nama orang (Guru/Kepala/Siswa) ke Proper Case
     * dengan tetap menjaga penulisan gelar akademik agar benar (cth: S.Pd., M.Pd., S.Ag.)
     */
    function format_person_name($name)
    {
        if (empty($name)) {
            return '';
        }

        // Ubah seluruh teks ke Proper Case (Capitalize Each Word)
        $proper = ucwords(strtolower($name));

        // Perbaiki gelar-gelar akademik yang singkatan hurufnya harus kapital di tengah/akhir
        $degreeReplacements = [
            // Gelar Sarjana (S.) & Magister (M.) umum
            'S.pd'   => 'S.Pd',
            'M.pd'   => 'M.Pd',
            'S.ag'   => 'S.Ag',
            'M.ag'   => 'M.Ag',
            'S.kom'  => 'S.Kom',
            'M.kom'  => 'M.Kom',
            'S.si'   => 'S.Si',
            'M.si'   => 'M.Si',
            'S.sos'  => 'S.Sos',
            'M.sos'  => 'M.Sos',
            'S.t'    => 'S.T',
            'M.t'    => 'M.T',
            'S.psi'  => 'S.Psi',
            'M.psi'  => 'M.Psi',
            'S.e'    => 'S.E',
            'M.m'    => 'M.M',
            'S.h'    => 'S.H',
            'M.h'    => 'M.H',
            'S.ak'   => 'S.Ak',
            'M.ak'   => 'M.Ak',
            'S.kes'  => 'S.Kes',
            'M.kes'  => 'M.Kes',
            'S.hum'  => 'S.Hum',
            'M.hum'  => 'M.Hum',
            'S.fil'  => 'S.Fil',
            'M.fil'  => 'M.Fil',
            'S.ip'   => 'S.IP',
            'M.ip'   => 'M.IP',
            'S.ap'   => 'S.AP',
            'M.ap'   => 'M.AP',
            
            // Gelar keagamaan spesifik
            'S.pd.i' => 'S.Pd.I',
            'S.pd.i.'=> 'S.Pd.I.',
            'M.pd.i' => 'M.Pd.I',
            'M.pd.i.'=> 'M.Pd.I.',
            'S.s.i'  => 'S.S.I',
            'S.s.i.' => 'S.S.I.',
            'M.s.i'  => 'M.S.I',
            'M.s.i.' => 'M.S.I.',
            'Msi'    => 'M.S.I',
            'Msi.'   => 'M.S.I.',
            'S.or'   => 'S.Or',
            'S.or.'  => 'S.Or.',
            'M.or'   => 'M.Or',
            'M.or.'  => 'M.Or.',
            
            // Lain-lain / Internasional
            'Ph.d'   => 'Ph.D',
            'M.b.a'  => 'M.B.A',
            'B.b.a'  => 'B.B.A',
        ];

        foreach ($degreeReplacements as $incorrect => $correct) {
            $proper = str_ireplace($incorrect, $correct, $proper);
        }

        return $proper;
    }
}

if (!function_exists('get_app_version')) {
    /**
     * Get the current application version dynamically from version.json
     */
    function get_app_version()
    {
        $versionFile = WRITEPATH . 'version.json';
        if (file_exists($versionFile)) {
            $data = json_decode(file_get_contents($versionFile), true);
            if ($data && isset($data['version'])) {
                return $data['version'];
            }
        }
        return '1.0.0';
    }
}