<?php

namespace Database\Seeders\New;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use JsonMachine\Items;
use JsonMachine\JsonDecoder\ExtJsonDecoder;

class ProductPricesSeeder extends Seeder
{
    public function run()
    {
        DB::transaction(function () {
            $table = 'products';

            $items = Items::fromFile(
                database_path('data/prices.json'),
                ['decoder' => new ExtJsonDecoder(true)]
            );

            foreach ($items as $data) {
                // item_id is product id, I need to find product and update price_amount to $data['value'] and cost_amount to $data['cost_value']
                DB::table($table)->where('id', $data['item_id'])->update([
                    'price_amount' => $data['value'],
                    'cost_amount' => $data['cost_value'],
                ]);
            }
        });
    }
}
