<?php

if (!defined('ABSPATH')) {
    exit;
}

class AIEO_Google_Reviews_Settings {

    const OPTION_KEY = 'aieo_google_reviews_settings';

    private static $cached = null;

    public static function defaults() {
        return array(
            'global_api_key'           => '',
            'language'                 => 'el',
            'schedule'                 => 'daily',
            'run_time'                 => '03:30',
            'rate_limit_per_hour'      => 20,
            'rate_limit_per_day'       => 500,
            'block_cache_enabled'      => true,
            'match_customer_min_score' => 0.85,
        );
    }

    public static function get_all() {
        if (self::$cached !== null) {
            return self::$cached;
        }
        $stored = get_option(self::OPTION_KEY, array());
        if (!is_array($stored)) { $stored = array(); }
        self::$cached = array_merge(self::defaults(), $stored);
        return self::$cached;
    }

    public static function get($key, $fallback = null) {
        $all = self::get_all();
        return array_key_exists($key, $all) ? $all[$key] : $fallback;
    }

    
    public static function update(array $patch) {
        $defaults = self::defaults();
        $current  = self::get_all();
        foreach ($patch as $k => $v) {
            if (!array_key_exists($k, $defaults)) { continue; }
            $current[$k] = self::sanitize_value($k, $v);
        }
        update_option(self::OPTION_KEY, $current, false);
        self::$cached = $current;
        return $current;
    }

    private static function sanitize_value($key, $value) {
        switch ($key) {
            case 'global_api_key':
                return sanitize_text_field((string) $value);
            case 'language':
                $v = sanitize_text_field((string) $value);
                return $v !== '' ? substr($v, 0, 10) : 'el';
            case 'schedule':
                $v = sanitize_key((string) $value);
                $allowed = array('twice_daily', 'daily', 'weekly', 'monthly');
                return in_array($v, $allowed, true) ? $v : 'daily';
            case 'run_time':
                $v = sanitize_text_field((string) $value);
                if (preg_match('/^([01]\d|2[0-3]):[0-5]\d$/', $v)) {
                    return $v;
                }
                return '03:30';
            case 'rate_limit_per_hour':
            case 'rate_limit_per_day':
                return max(1, (int) $value);
            case 'block_cache_enabled':
                return !empty($value);
            case 'match_customer_min_score':
                $f = (float) $value;
                if ($f < 0)  { $f = 0; }
                if ($f > 1)  { $f = 1; }
                return round($f, 2);
        }
        return $value;
    }

    
    public static function api_key_for_location(array $location) {
        $per = isset($location['api_key']) ? trim((string) $location['api_key']) : '';
        if ($per !== '') { return $per; }
        return (string) self::get('global_api_key');
    }
}
