<?php

namespace App\Jobs;

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

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

    public $pengajuanIds;

    public function __construct(array $pengajuanIds)
    {
        $this->pengajuanIds = $pengajuanIds;
    }

    public function handle(SawCalculationService $sawService)
    {
        try {
            $result = $sawService->processBatch($this->pengajuanIds);
            
            Log::info('Ranking job completed', [
                'processed_count' => $result['success_count'],
                'error_count' => $result['error_count'],
                'total_ids' => count($this->pengajuanIds)
            ]);
            
        } catch (\Exception $e) {
            Log::error('Ranking job failed: '.$e->getMessage(), [
                'exception' => $e,
                'pengajuan_ids' => $this->pengajuanIds
            ]);
            throw $e;
        }
    }
}