<?php

namespace App\Jobs;

use App\Models\Customer;
use App\Services\BranchContext;
use App\Services\CustomerRiskService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class RefreshCustomerCreditScore implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;

    public int $timeout = 60;

    public function __construct(
        public readonly int $customerId,
        public readonly int $branchId,
        public readonly string $trigger,
    ) {
    }

    public function handle(BranchContext $branchContext, CustomerRiskService $risk): void
    {
        $branchContext->forBranch($this->branchId, function () use ($risk): void {
            $customer = Customer::query()->find($this->customerId);

            if ($customer) {
                $risk->refreshWithHistory($customer, $this->trigger);
            }
        });
    }
}
