<?php

namespace Database\Seeders\New;

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

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

            DB::table($table)->truncate();

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

            foreach ($items as $data) {
                DB::table($table)->insert([
                    'product_id' => $data['item_id'],
                    'attribute_id' => $data['property_id'],
                ]);
            }
        });
    }
}
