<?php

namespace App\Jobs;

use App\Models\PromptTask;
use App\Service\ExternalApiService;
use App\Service\TaskService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public array $data = [];

    public function __construct(array $data = [])
    {
        $this->queue = "{download-image}";
        $this->data = $data;
    }

    public function handle()
    {
        $result = app(ExternalApiService::class)->downloadImage($this->data);
        if (filled($result) && $result['code'] == 200) {

            $data = $result['data'];
            $updateData = [
                'cut_status' => TaskService::STATUS_SUCCESS,
                'cut_img' => $data[0] ?? '',
            ];
            PromptTask::where('id', $this->data['prompt_id'])
                ->update($updateData);
        }
    }
}
