<?php

namespace Database\Seeders\New;

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

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

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

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

            foreach ($items as $data) {
                if ($data['categorizable_type'] == 'Domain\\Item\\Models\\Item') {
                    DB::table($table)->insert([
                        'product_id' => $data['categorizable_id'],
                        'category_id' => $data['category_id'],
                    ]);
                }
            }

        });
    }
}
