<?php

namespace Database\Seeders\New;

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

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

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

            foreach ($items as $data) {
                DB::table($table)->insert([
                    'id' => $data['id'],
                    'parent_id' => $data['parent_id'],
                    'slug' => $data['slug'],
                    'name' => str_replace('"tm"', '"tk"', $data['name']),
                    'is_visible' => $data['is_blocked'],
                    'sort_order' => $data['priority'],
                    'tax_percentage' => $data['percentage'],
                    'created_at' => $data['created_at'],
                    'updated_at' => $data['updated_at'],
                ]);
            }

            DB::statement("
                SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
            ");
        });
    }
}
