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

class Setting_model extends CI_Model {
    
    protected $table = 'settings';
    
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Get a single setting value
     */
    public function get($key, $default = null) {
        $result = $this->db->get_where($this->table, ['key' => $key])->row();
        return $result ? $result->value : $default;
    }
    
    /**
     * Get multiple settings as key-value array
     */
    public function get_all() {
        $rows = $this->db->get($this->table)->result();
        $settings = [];
        foreach ($rows as $row) {
            $settings[$row->key] = $row->value;
        }
        return $settings;
    }
    
    /**
     * Get settings by keys
     */
    public function get_many($keys) {
        $this->db->where_in('key', $keys);
        $rows = $this->db->get($this->table)->result();
        $settings = [];
        foreach ($rows as $row) {
            $settings[$row->key] = $row->value;
        }
        return $settings;
    }
    
    /**
     * Set a single setting
     */
    public function set($key, $value, $type = 'text', $description = '') {
        $exists = $this->db->get_where($this->table, ['key' => $key])->row();
        
        $data = [
            'key' => $key,
            'value' => $value,
            'type' => $type
        ];
        
        if ($description) {
            $data['description'] = $description;
        }
        
        if ($exists) {
            $this->db->where('key', $key);
            return $this->db->update($this->table, $data);
        } else {
            return $this->db->insert($this->table, $data);
        }
    }
    
    /**
     * Set multiple settings
     */
    public function set_many($settings) {
        foreach ($settings as $key => $value) {
            $this->set($key, $value);
        }
    }
    
    /**
     * Delete a setting
     */
    public function delete($key) {
        return $this->db->delete($this->table, ['key' => $key]);
    }
    
    /**
     * Get app branding settings
     */
    public function get_branding() {
        return $this->get_many([
            'app_name',
            'app_logo',
            'app_favicon',
            'app_logo_dark',
            'app_tagline'
        ]);
    }
    
    /**
     * Get logo URL
     */
    public function get_logo_url($dark = false) {
        $key = $dark ? 'app_logo_dark' : 'app_logo';
        $logo = $this->get($key);
        
        if ($logo && file_exists(FCPATH . 'uploads/branding/' . $logo)) {
            return base_url('uploads/branding/' . $logo);
        }
        
        // Default logo
        return base_url('assets/img/logo.png');
    }
    
    /**
     * Get favicon URL
     */
    public function get_favicon_url() {
        $favicon = $this->get('app_favicon');
        
        if ($favicon && file_exists(FCPATH . 'uploads/branding/' . $favicon)) {
            return base_url('uploads/branding/' . $favicon);
        }
        
        // Default favicon
        return base_url('assets/img/favicon.png');
    }
    
    /**
     * Get app name
     */
    public function get_app_name() {
        return $this->get('app_name', 'MeridianBank');
    }
}
