<?php
/** CRON: تقارير AML اليومية */
require_once dirname(__DIR__).'/config.php';
$threshold = (int)PricingEngine::getSetting('sdpay_aml_threshold_sdg', 10000000);
try {
    $st = db()->prepare(
        "SELECT from_account, SUM(amount_sdg) as total, COUNT(*) as cnt
         FROM sdpay_transactions
         WHERE DATE(created_at)=CURDATE() AND status='completed'
         GROUP BY from_account HAVING total >= ?");
    $st->execute([$threshold]);
    $flagged = $st->fetchAll();
    foreach ($flagged as $f) {
        db()->prepare(
            "INSERT INTO sdpay_aml_reports (account_no,report_type,amount_sdg,details)
             VALUES (?,'CTR',?,?) ON DUPLICATE KEY UPDATE amount_sdg=VALUES(amount_sdg)"
        )->execute([$f['from_account'],$f['total'],json_encode($f)]);
    }
    echo date('Y-m-d H:i:s')." — AML CTR: ".count($flagged)." حساب\n";
} catch(Throwable $e){ echo "❌ ".$e->getMessage()."\n"; }
