<?php

namespace App\Filament\Pages\Verification\Services;

use App\Enums\VerificationStatus;
use App\Models\Company;
use App\Models\User;
use App\Models\VerificationRequest;

class VerificationSubmissionService
{
    public function submit(Company $company, ?VerificationRequest $verificationRequest, User $actor): VerificationRequest
    {
        if ($verificationRequest && $verificationRequest->status === VerificationStatus::NEED_REVISION) {
            // Resubmit - update existing request
            $verificationRequest->update([
                'status' => VerificationStatus::PENDING,
            ]);

            return $verificationRequest->fresh(['reviews.reviewer']);
        }

        // New submission
        return VerificationRequest::create([
            'company_id' => $company->id,
            'submitted_by' => $actor->id,
            'status' => VerificationStatus::PENDING,
        ])->load(['reviews.reviewer']);
    }
}
