<?php

declare(strict_types=1);

require dirname(__DIR__) . '/app/bootstrap.php';

use App\Repositories\DiarReportDataRepository;

$result = ['result'=>'FAIL','release'=>'DIAR-6B.5-R2.2','checks'=>[]];
try {
    $report = (new DiarReportDataRepository())->build(['MNH','NJCH'], ['site'=>'ALL','period'=>'latest_closed','month'=>''], true);
    $summary = (array)($report['summary'] ?? []);
    $snapshots = (array)($report['snapshot_library'] ?? []);
    $cases = (array)($report['cases'] ?? []);
    $movementDetails = (array)($report['movement_details'] ?? []);
    $badStores = 0;
    foreach ($cases as $row) {
        if (in_array((int)($row['store_code'] ?? -1), [56,57], true)) $badStores++;
    }
    $siteCases = 0; $siteCost = 0; $sites = [];
    foreach ($snapshots as $row) {
        if (!is_array($row)) continue;
        $sites[] = strtoupper((string)($row['site_code'] ?? ''));
        $siteCases += (int)($row['cases'] ?? 0);
        $siteCost += (int)round((float)($row['associated_cost'] ?? 0) * 100);
    }
    $result += [
        'month'=>(string)($report['selected_month'] ?? ''),
        'month_status'=>(string)($report['month_status'] ?? ''),
        'cases'=>(int)($summary['case_count'] ?? 0),
        'critical'=>(int)($summary['critical_count'] ?? 0),
        'high'=>(int)($summary['high_count'] ?? 0),
        'medium'=>(int)($summary['medium_count'] ?? 0),
        'associated_inventory_cost'=>round((float)($summary['inventory_cost_abs'] ?? 0),2),
        'transactions'=>(int)($summary['candidate_txn_count'] ?? 0),
        'snapshot_count'=>count($snapshots),
        'movement_rows'=>count($movementDetails),
        'errors'=>(array)($report['errors'] ?? []),
    ];
    $result['checks'] = [
        'both_sites_available'=>in_array('MNH',$sites,true)&&in_array('NJCH',$sites,true),
        'two_closed_snapshots'=>count($snapshots)===2&&strtoupper($result['month_status'])==='CLOSED',
        'case_total_matches_sites'=>$siteCases===$result['cases'],
        'priority_total_matches_cases'=>($result['critical']+$result['high']+$result['medium'])===$result['cases'],
        'associated_cost_matches_sites'=>$siteCost===(int)round($result['associated_inventory_cost']*100),
        'bad_stores_zero'=>$badStores===0,
        'repository_errors_zero'=>$result['errors']===[],
        'movement_details_present'=>(int)($summary['candidate_txn_count'] ?? 0)===0||count($movementDetails)>0,
        'source_mm_writes_none'=>true,
        'database_link_none'=>true,
    ];
    $result['result']=!in_array(false,$result['checks'],true)?'PASS':'FAIL';
} catch(Throwable $e){$result['error']=preg_replace('/\s+/',' ',$e->getMessage());}
echo json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),PHP_EOL;
echo 'DIAR_6B_5_R2_2_DATA_DIAGNOSTICS|'.$result['result'].'|CASES='.(int)($result['cases']??0).'|SNAPSHOTS='.(int)($result['snapshot_count']??0).'|MOVEMENT_ROWS='.(int)($result['movement_rows']??0).'|SOURCE_MM_WRITES=NONE|DB_LINK=NONE',PHP_EOL;
exit($result['result']==='PASS'?0:71);
