<?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 Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

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

    public function __construct(
        public string $sourceStore,
        public string $shopifyStore,
        public string $shopifyToken,
        public int $priceMarkup = 0
    ) {}

    public function handle()
    {
        $res = Http::get($this->sourceStore.'/collections/all/products.json', [
            'limit' => 5
        ]);

        if ($res->failed()) {
            Log::error('Source store JSON blocked');
            return;
        }

        foreach ($res->json('products', []) as $product) {
            Log::info('Fetched product: '.$product['title']);
            // next step: create product in Shopify
        }
    }
}
