<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Settings_m extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function table_ready()
    {
        return $this->db->table_exists('app_settings');
    }

    public function ensure_table()
    {
        if ($this->table_ready()) {
            return true;
        }
        $sql = "CREATE TABLE IF NOT EXISTS `app_settings` (
            `setting_key` varchar(80) NOT NULL,
            `setting_value` text NOT NULL,
            `updated_at` datetime DEFAULT NULL,
            PRIMARY KEY (`setting_key`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
        return (bool) $this->db->query($sql);
    }

    public function get($key, $default = null)
    {
        if (!$this->table_ready()) {
            return $default;
        }
        $row = $this->db->where('setting_key', $key)->get('app_settings')->row();
        if (!$row) {
            return $default;
        }
        return $row->setting_value;
    }

    public function set($key, $value)
    {
        $this->ensure_table();
        $exists = $this->db->where('setting_key', $key)->get('app_settings')->row();
        $data = array(
            'setting_key' => $key,
            'setting_value' => (string) $value,
            'updated_at' => date('Y-m-d H:i:s'),
        );
        if ($exists) {
            $ok = $this->db->where('setting_key', $key)->update('app_settings', $data);
        } else {
            $ok = $this->db->insert('app_settings', $data);
        }

        if ($key === 'ui_font_family') {
            if (!function_exists('app_font_write_cache')) {
                $CI =& get_instance();
                $CI->load->helper('settings');
            }
            app_font_write_cache($value);
        }

        return $ok;
    }

    /**
     * Available UI/export fonts.
     * pdf = Dompdf-safe family (DejaVu / core PDF fonts only).
     * office = Word/Excel CSS font-family (uses real installed OS fonts).
     * @return array key => config
     */
    public function font_options()
    {
        return array(
            'ibm_plex' => array(
                'label' => 'IBM Plex Sans',
                'css' => '"IBM Plex Sans", "Segoe UI", system-ui, sans-serif',
                'google' => 'IBM+Plex+Sans:wght@400;500;600;700',
                'pdf' => 'DejaVu Sans',
                'office' => '"IBM Plex Sans", Calibri, Arial, sans-serif',
            ),
            'segoe' => array(
                'label' => 'Segoe UI',
                'css' => '"Segoe UI", system-ui, sans-serif',
                'google' => '',
                'pdf' => 'Helvetica',
                'office' => '"Segoe UI", Arial, sans-serif',
            ),
            'arial' => array(
                'label' => 'Arial',
                'css' => 'Arial, Helvetica, sans-serif',
                'google' => '',
                'pdf' => 'Helvetica',
                'office' => 'Arial, Helvetica, sans-serif',
            ),
            'calibri' => array(
                'label' => 'Calibri',
                'css' => 'Calibri, "Segoe UI", Arial, sans-serif',
                'google' => '',
                'pdf' => 'DejaVu Sans',
                'office' => 'Calibri, Arial, sans-serif',
            ),
            'roboto' => array(
                'label' => 'Roboto',
                'css' => 'Roboto, "Segoe UI", Arial, sans-serif',
                'google' => 'Roboto:wght@400;500;700',
                'pdf' => 'DejaVu Sans',
                'office' => 'Roboto, Arial, Helvetica, sans-serif',
            ),
            'open_sans' => array(
                'label' => 'Open Sans',
                'css' => '"Open Sans", "Segoe UI", Arial, sans-serif',
                'google' => 'Open+Sans:wght@400;600;700',
                'pdf' => 'DejaVu Sans',
                'office' => '"Open Sans", Arial, Helvetica, sans-serif',
            ),
            'lato' => array(
                'label' => 'Lato',
                'css' => 'Lato, "Segoe UI", Arial, sans-serif',
                'google' => 'Lato:wght@400;700',
                'pdf' => 'DejaVu Sans',
                'office' => 'Lato, Arial, Helvetica, sans-serif',
            ),
            'times' => array(
                'label' => 'Times New Roman',
                'css' => '"Times New Roman", Times, serif',
                'google' => '',
                'pdf' => 'Times-Roman',
                'office' => '"Times New Roman", Times, serif',
            ),
            'georgia' => array(
                'label' => 'Georgia',
                'css' => 'Georgia, "Times New Roman", serif',
                'google' => '',
                'pdf' => 'DejaVu Serif',
                'office' => 'Georgia, "Times New Roman", serif',
            ),
            'courier' => array(
                'label' => 'Courier New',
                'css' => '"Courier New", Courier, monospace',
                'google' => '',
                'pdf' => 'Courier',
                'office' => '"Courier New", Courier, monospace',
            ),
        );
    }

    public function resolve_font($key = null)
    {
        $options = $this->font_options();
        if ($key === null || $key === '') {
            $key = $this->get('ui_font_family', 'ibm_plex');
        }
        if (!isset($options[$key])) {
            $key = 'ibm_plex';
        }
        $cfg = $options[$key];
        $cfg['key'] = $key;
        return $cfg;
    }
}
