<?php

namespace Database\Seeders;

use Exception;
use File;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class InventoriesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->seedOldData();
    }

    /**
     * Seed old data
     */
    public function seedOldData(): void
    {
        $datas = json_decode(File::get('database/data/inventories.json'));

        $table = 'inventories';
        DB::table($table)->truncate();

        try {
            foreach ($datas as $data) {
                DB::table($table)->insert([
                    'id' => $data->id,
                    'name' => $data->name,
                    'code' => $data->code,
                    'description' => $data->description,
                    'email' => $data->email,
                    'phone_number' => $data->phone_number,
                    'street_address' => $data->street_address,
                    'zipcode' => $data->zipcode,
                    'region' => 'ag',
                    'is_default' => false,
                    'channel_id' => $data->channel_id,
                ]);
            }
        } catch (Exception $e) {
            info(['Ignore error', $e->getMessage()]);
        }

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