<?php

namespace Database\Seeders;

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

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

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

        $table = 'roles';
        foreach ($datas as $data) {
            DB::table($table)->insert([
                'id' => $data->id,
                'name' => $data->name,
                'guard_name' => $data->guard_name,
                'display_name' => json_decode($data->display_name),
                'description' => json_decode($data->description),
                'created_at' => $data->created_at,
                'updated_at' => $data->updated_at,
            ]);
        }

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