<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\SettingModel;

class BaseController extends Controller
{
    protected $data = [];

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);

        // Safely load settings
        try {
            $settingModel = new SettingModel();
            $settings = $settingModel->findAll();
            $this->data['settings'] = [];
            foreach ($settings as $s) {
                $this->data['settings'][$s['key']] = $s['value'];
            }
        } catch (\Exception $e) {
            // Fallback if DB fails
            $this->data['settings'] = [
                'exchange_rate' => '130.50',
                'whatsapp_number' => '254700123456',
                'openweather_api_key' => ''
            ];
            log_message('error', 'Failed to load settings: ' . $e->getMessage());
        }

    }
}