<?php

use App\Models\SettingModel;

if (! function_exists('app_setting')) {
    function app_setting(): array
    {
        static $setting = null;

        if ($setting !== null) {
            return $setting;
        }

        $defaultSetting = [
            'logo'            => null,
            'favicon'         => null,
            'nama_aplikasi'   => 'Aplikasi Admin',
            'judul_aplikasi'  => 'Aplikasi Admin',
            'tagline'         => 'Fondasi aplikasi yang siap dikembangkan',
            'nama_perusahaan' => 'Perusahaan Anda',
            'pajak_persen'    => 11,
            'nota_penjualan_ukuran' => 'thermal_58',
        ];

        try {
            $settingModel = new SettingModel();
            $settingData  = $settingModel->find(1);

            if (! $settingData) {
                $settingModel->insert(array_merge(
                    ['setting_id' => 1],
                    $defaultSetting
                ));

                $settingData = $settingModel->find(1);
            }

            $setting = array_merge($defaultSetting, $settingData ?? []);
        } catch (Throwable $exception) {
            $setting = $defaultSetting;
        }

        return $setting;
    }
}
