<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Services\Recommendations\RecommendationFeedResolver;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

final class RegenerateUserRecommendationCacheJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public int $tries = 3;

    /** @var array<int, int> */
    public array $backoff = [10, 60, 180];

    public function __construct(
        public readonly int $userId,
        public readonly string $algoVersion
    ) {
    }

    public function handle(RecommendationFeedResolver $feedResolver): void
    {
        try {
            $feedResolver->regenerateCacheForUser($this->userId, $this->algoVersion);
        } catch (\Throwable $e) {
            Log::error('RegenerateUserRecommendationCacheJob failed', [
                'user_id' => $this->userId,
                'algo_version' => $this->algoVersion,
                'error' => $e->getMessage(),
            ]);

            throw $e;
        }
    }
}
