<?php

declare(strict_types=1);

namespace Tests\Feature\Baselines\Support;

use App\Support\Baselines\OperatorActionCategory;
use App\Support\Baselines\ResolutionOutcome;
use App\Support\Baselines\ResolutionPath;
use App\Support\Baselines\SubjectClass;

final class BaselineSubjectResolutionFixtures
{
    /**
     * @param  array<string, mixed>  $overrides
     * @return array<string, mixed>
     */
    public static function structuredGap(array $overrides = []): array
    {
        return array_replace([
            'policy_type' => 'deviceConfiguration',
            'subject_external_id' => 'subject-1',
            'subject_key' => 'deviceconfiguration|subject-1',
            'subject_class' => SubjectClass::PolicyBacked->value,
            'resolution_path' => ResolutionPath::Policy->value,
            'resolution_outcome' => ResolutionOutcome::PolicyRecordMissing->value,
            'reason_code' => 'policy_record_missing',
            'operator_action_category' => OperatorActionCategory::RunPolicySyncOrBackup->value,
            'structural' => false,
            'retryable' => false,
            'source_model_expected' => 'policy',
            'source_model_found' => null,
        ], $overrides);
    }

    /**
     * @param  list<array<string, mixed>>  $subjects
     * @param  array<string, mixed>  $overrides
     * @return array<string, mixed>
     */
    public static function compareContext(array $subjects, array $overrides = []): array
    {
        $byReason = [];

        foreach ($subjects as $subject) {
            $reasonCode = is_string($subject['reason_code'] ?? null) ? $subject['reason_code'] : 'unknown';
            $byReason[$reasonCode] = ($byReason[$reasonCode] ?? 0) + 1;
        }

        return array_replace_recursive([
            'baseline_compare' => [
                'evidence_gaps' => [
                    'count' => count($subjects),
                    'by_reason' => $byReason,
                    'subjects' => $subjects,
                ],
            ],
        ], $overrides);
    }

    /**
     * @param  list<array<string, mixed>>  $subjects
     * @param  array<string, mixed>  $overrides
     * @return array<string, mixed>
     */
    public static function captureContext(array $subjects, array $overrides = []): array
    {
        $byReason = [];

        foreach ($subjects as $subject) {
            $reasonCode = is_string($subject['reason_code'] ?? null) ? $subject['reason_code'] : 'unknown';
            $byReason[$reasonCode] = ($byReason[$reasonCode] ?? 0) + 1;
        }

        return array_replace_recursive([
            'baseline_capture' => [
                'gaps' => [
                    'count' => count($subjects),
                    'by_reason' => $byReason,
                    'subjects' => $subjects,
                ],
            ],
        ], $overrides);
    }
}
