<?php

declare(strict_types=1);

namespace App\Filament\Support;

use App\Models\OperationRun;

final class VerificationReportChangeIndicator
{
    /**
     * @return array{state: 'no_changes'|'changed', previous_report_id: int}|null
     */
    public static function forRun(OperationRun $run): ?array
    {
        $report = VerificationReportViewer::report($run);

        if ($report === null) {
            return null;
        }

        $previousRun = VerificationReportViewer::previousRun($run, $report);

        if ($previousRun === null) {
            return null;
        }

        $previousReport = VerificationReportViewer::report($previousRun);

        if ($previousReport === null) {
            return null;
        }

        $currentFingerprint = VerificationReportViewer::fingerprint($report);
        $previousFingerprint = VerificationReportViewer::fingerprint($previousReport);

        if ($currentFingerprint === null || $previousFingerprint === null) {
            return null;
        }

        return [
            'state' => $currentFingerprint === $previousFingerprint ? 'no_changes' : 'changed',
            'previous_report_id' => (int) $previousRun->getKey(),
        ];
    }
}
