<?php
require __DIR__ . '/../vendor/autoload.php';
$app = require __DIR__ . '/../app/bootstrap.php';
function runCase($app,$from,$to,$owner){
  $f=[ 'date_from'=>$from,'date_to'=>$to,'person_date_from'=>$from,'person_date_to'=>$to,
    'owner_didar_id'=>$owner,'owner_didar_ids_csv'=>$owner,'owner_didar_ids'=>[$owner],'product_id'=>'','product_ids_csv'=>'','product_ids'=>[],
    'deal_statuses_csv'=>'','deal_statuses'=>[],'deal_lead_types_csv'=>'','deal_lead_types'=>[],
    'kpi_sales_date_basis'=>'ui_payment_date','kpi_sales_count_rule'=>'first_payment_only','kpi_payment_date_basis'=>'ui_payment_date','kpi_payment_count_rule'=>'all_payments','kpi_payment_status_scope'=>'confirmed_only','legacy_deal_scope'=>'exclude'];
  $repo=$app->repositories->kpi; $ctx=['date'=>'','owner_didar_id'=>'','owner_name'=>'','product_name'=>'','failure_reason'=>'','activity_type'=>''];
  $dash=$repo->getPerformanceDashboardData($f);
  echo "\n=== CASE {$from}..{$to} ===\n";
  echo json_encode([
    'sales'=>(int)($dash['summary_cards']['with_sales_deals_count']??0),
    'settled'=>(int)($dash['summary_cards']['settled_count']??0),
    'outstanding'=>(int)($dash['summary_cards']['outstanding_count']??0),
    'open'=>(int)($dash['summary_cards']['open_pipeline_count']??0),
    'without'=>(int)($dash['summary_cards']['without_payment_count']??0),
    'lost'=>(int)($dash['summary_cards']['lost_count']??0),
    'conv_total'=>(int)($dash['summary_cards']['deal_conversion_total_deals']??0),
    'conv_rate'=>(float)($dash['summary_cards']['deal_conversion_rate']??0),
  ], JSON_UNESCAPED_UNICODE)."\n";
  $sales=$repo->getKpiDrilldown($f,'performance_cards','sales_total',$ctx);
  $without=$repo->getKpiDrilldown($f,'performance_cards','without_payment_count',$ctx);
  $open=$repo->getKpiDrilldown($f,'performance_cards','open_pipeline_count',$ctx);
  $lost=$repo->getKpiDrilldown($f,'performance_cards','lost_count',$ctx);
  $conv=$repo->getKpiDrilldown($f,'performance_cards','deal_conversion_rate',$ctx);
  $listIds=function($rows){$ids=[]; foreach(($rows??[]) as $r){$ids[]=(string)($r['deal_id']??$r['id']??'');} return array_values(array_unique(array_filter($ids)));};
  $salesIds=$listIds($sales['deals']??[]); $withoutIds=$listIds($without['deals']??[]); $openIds=$listIds($open['deals']??[]); $lostIds=$listIds($lost['deals']??[]);
  $inc=[]; foreach(($conv['included_deals']??[]) as $r){$inc[(string)($r['deal_id']??$r['id'])]=$r;} $incIds=array_keys($inc);
  sort($salesIds); sort($withoutIds); sort($openIds); sort($lostIds); sort($incIds);
  echo 'salesIds='.json_encode($salesIds)."\n";
  echo 'withoutIds='.json_encode($withoutIds)."\n";
  echo 'openIds='.json_encode($openIds)."\n";
  echo 'lostIds='.json_encode($lostIds)."\n";
  echo 'convIncludedIds='.json_encode($incIds)."\n";
  $sum=array_values(array_unique(array_merge($salesIds,$withoutIds,$lostIds))); sort($sum);
  echo 'union(sales+without+lost)='.count($sum).' ids='.json_encode($sum)."\n";
  echo 'conv_total='.count($incIds)."\n";
  $extra=array_values(array_diff($sum,$incIds)); $missing=array_values(array_diff($incIds,$sum));
  echo 'extra_vs_conv='.json_encode($extra)."\n";
  echo 'missing_vs_conv='.json_encode($missing)."\n";
  foreach($extra as $id){$r=[]; foreach(($sales['deals']??[]) as $d){ if((string)($d['deal_id']??'')===$id){$r=$d; break;}}
    if(!$r){ foreach(($without['deals']??[]) as $d){ if((string)($d['deal_id']??'')===$id){$r=$d; break;}} }
    if(!$r){ foreach(($lost['deals']??[]) as $d){ if((string)($d['deal_id']??'')===$id){$r=$d; break;}} }
    echo 'EXTRA_DEAL '.$id.' | '.($r['title']??'').' | status='.($r['status']??'').' | deal_date='.($r['deal_date']??'').' | total_paid='.($r['total_paid']??'')."\n";
  }
  $wonInConv=[]; foreach($inc as $id=>$r){ if(($r['status']??'')==='Won'){ $wonInConv[]=$id; } }
  echo 'won_in_conv='.json_encode($wonInConv)."\n";
  if(!empty($conv['excluded_reasons_summary'])) echo 'excluded_reasons_summary='.json_encode($conv['excluded_reasons_summary'], JSON_UNESCAPED_UNICODE)."\n";
}
$owner='75f9d286-0d05-43e0-a6ed-9fdb8ad89a0f';
runCase($app,'2026-02-24','2026-02-24',$owner);
runCase($app,'2026-02-14','2026-02-25',$owner);
