<?php
class ChairmanHealthService {
    private $db;
    private $config;

    public function __construct(mysqli $db, array $config=array()) {
        $this->db = $db;
        $this->config = $config;
    }

    public function health() {
        $cacheDir = isset($this->config['cache_dir']) ? $this->config['cache_dir'] : dirname(__DIR__) . '/cache';
        return array(
            'framework' => 'WinRacing Chairman AI Framework',
            'version' => isset($this->config['framework_version']) ? $this->config['framework_version'] : '1.3',
            'db' => 'OK',
            'loaded_config' => chairman_public_path(isset($this->config['loaded_config']) ? $this->config['loaded_config'] : '', $this->config),
            'cache_dir' => chairman_public_path($cacheDir, $this->config),
            'cache_writable' => is_writable($cacheDir) ? 'YES' : 'NO',
            'time' => date('Y-m-d H:i:s')
        );
    }

    public function requiredTablesStatus() {
        $tables = array(
            'wr_chairman_production_score_v44',
            'wr_chairman_race_optimizer_v44',
            'wr_chairman_analysis_api_v44',
            'wr_chairman_production_run_log_v44'
        );
        $out = array();
        foreach($tables as $t){ $out[$t] = chairman_table_exists($this->db,$t) ? 'OK' : 'MISSING'; }
        return $out;
    }
}
