<?php

declare(strict_types=1);

namespace App\Services;

use App\Repositories\LegacySpecialtyFinanceRepository;
use App\Repositories\SpecialtyFinanceRepository;

final class SpecialtyTotalRevenueParityService
{
    /**
     * @return array<string, mixed>
     */
    public function run(int $year, int $compareYear): array
    {
        $branches = BranchReadinessPolicy::blockingQaBranches();
        $deferredBranches = BranchReadinessPolicy::deferredBranches();
        $years = [$year, $compareYear];

        $datasets = [];
        $details = [];
        $maxDifference = 0.0;
        $identityMismatches = 0;
        $failedDatasets = 0;

        foreach ($branches as $branch) {
            foreach ($years as $periodYear) {
                $legacyRows = (new LegacySpecialtyFinanceRepository($branch))
                    ->specialtyTotalRevenueRanking($periodYear);

                $currentRows = (new SpecialtyFinanceRepository($branch))
                    ->specialtyTotalRevenueRanking($periodYear);

                $comparison = $this->compareDataset(
                    $branch,
                    $periodYear,
                    $legacyRows,
                    $currentRows
                );

                $datasets[] = $comparison;
                $maxDifference = max(
                    $maxDifference,
                    (float)$comparison['max_difference']
                );
                $identityMismatches += (int)$comparison['identity_mismatches'];

                if (($comparison['status'] ?? 'FAIL') !== 'PASS') {
                    $failedDatasets++;
                }

                foreach ((array)$comparison['details'] as $detail) {
                    $details[] = $detail;
                }
            }
        }

        $status = (
            count($branches) === 2
            && count($datasets) === 4
            && $failedDatasets === 0
            && $identityMismatches === 0
            && $maxDifference === 0.0
        ) ? 'PASS' : 'FAIL';

        return [
            'schema_version' => '1.0',
            'phase' => 'H11.0-P6',
            'legacy_function' => 'special_prof',
            'legacy_function_name' => 'get_special_prof',
            'source_evidence_boundary' => 'FULL_HISTORICAL_FUNCTION_BODY_RECOVERED',
            'historical_contract' => [
                'source_table' => 'KPI_DOCTOR_PERF_DETAIL_JOB',
                'metric' => 'SUM(DISTINCT TOT_REV)',
                'grouping' => 'SPECIAL_SPEC_ID + SPECIAL_SPEC_EN_NAME',
                'year_filter' => 'YEARID = selected year',
                'minimum_value' => 50000,
                'ordering' => 'metric descending',
            ],
            'branch_readiness_policy' => 'READY_BRANCHES_ONLY_JCKH_DEFERRED',
            'active_qa_branches' => $branches,
            'deferred_branches' => $deferredBranches,
            'database_update_branches' => BranchReadinessPolicy::databaseUpdateBranches(),
            'years_checked' => $years,
            'dataset_checks' => count($datasets),
            'value_checks' => count($details),
            'identity_mismatches' => $identityMismatches,
            'failed_datasets' => $failedDatasets,
            'max_difference' => $maxDifference,
            'datasets' => $datasets,
            'details' => $details,
            'write_operations' => 'NONE',
            'matched_promotion' => $status === 'PASS',
            'baseline_before' => [
                'known' => 31,
                'matched' => 20,
                'partial' => 9,
                'missing' => 2,
                'matched_pct' => 64.5,
            ],
            'baseline_after_live_pass' => [
                'known' => 31,
                'matched' => 21,
                'partial' => 8,
                'missing' => 2,
                'matched_pct' => 67.7,
            ],
            'status' => $status,
        ];
    }

    /**
     * @param array<int, array<string, mixed>> $legacyRows
     * @param array<int, array<string, mixed>> $currentRows
     * @return array<string, mixed>
     */
    private function compareDataset(
        string $branch,
        int $year,
        array $legacyRows,
        array $currentRows
    ): array {
        $legacyMap = $this->mapRows($legacyRows);
        $currentMap = $this->mapRows($currentRows);

        $keys = array_values(array_unique(array_merge(
            array_keys($legacyMap),
            array_keys($currentMap)
        )));
        sort($keys);

        $details = [];
        $maxDifference = 0.0;
        $identityMismatches = 0;

        foreach ($keys as $key) {
            $legacyExists = array_key_exists($key, $legacyMap);
            $currentExists = array_key_exists($key, $currentMap);

            if (!$legacyExists || !$currentExists) {
                $identityMismatches++;
            }

            $legacyValue = $legacyExists
                ? (float)$legacyMap[$key]['value']
                : 0.0;

            $currentValue = $currentExists
                ? (float)$currentMap[$key]['value']
                : 0.0;

            $difference = abs($legacyValue - $currentValue);
            $maxDifference = max($maxDifference, $difference);

            $details[] = [
                'branch' => $branch,
                'year' => $year,
                'identity' => $key,
                'specialty_id' => (string)(
                    $legacyMap[$key]['id']
                    ?? $currentMap[$key]['id']
                    ?? ''
                ),
                'specialty_name' => (string)(
                    $legacyMap[$key]['label']
                    ?? $currentMap[$key]['label']
                    ?? ''
                ),
                'legacy_exists' => $legacyExists,
                'current_exists' => $currentExists,
                'legacy_value' => $legacyValue,
                'current_value' => $currentValue,
                'difference' => $difference,
                'status' => (
                    $legacyExists
                    && $currentExists
                    && $difference === 0.0
                ) ? 'PASS' : 'FAIL',
            ];
        }

        $failedRows = count(array_filter(
            $details,
            static function (array $row): bool {
                return ($row['status'] ?? 'FAIL') !== 'PASS';
            }
        ));

        return [
            'branch' => $branch,
            'year' => $year,
            'legacy_rows' => count($legacyRows),
            'current_rows' => count($currentRows),
            'value_checks' => count($details),
            'identity_mismatches' => $identityMismatches,
            'failed_rows' => $failedRows,
            'max_difference' => $maxDifference,
            'details' => $details,
            'status' => (
                count($legacyRows) === count($currentRows)
                && $identityMismatches === 0
                && $failedRows === 0
                && $maxDifference === 0.0
            ) ? 'PASS' : 'FAIL',
        ];
    }

    /**
     * @param array<int, array<string, mixed>> $rows
     * @return array<string, array<string, mixed>>
     */
    private function mapRows(array $rows): array
    {
        $out = [];

        foreach ($rows as $row) {
            $id = trim((string)($row['id'] ?? ''));
            $label = trim((string)($row['label'] ?? ''));
            $key = strtolower($id . '|' . $label);

            $out[$key] = [
                'id' => $id,
                'label' => $label,
                'value' => (float)($row['value'] ?? 0),
            ];
        }

        return $out;
    }
}
