<?php

namespace App\Jobs;

use App\Models\PlatformConnection;
use App\Services\Sync\PlatformSyncManager;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SyncPlatformConnectionJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 2;
    public int $timeout = 180;

    public function __construct(
        public readonly int $connectionId,
        public readonly string $from,
        public readonly string $to,
    ) {}

    public function handle(PlatformSyncManager $syncManager): void
    {
        $connection = PlatformConnection::with('secureCredential')->findOrFail($this->connectionId);
        $syncManager->syncConnection($connection, $this->from, $this->to);
    }
}
