<?php

namespace Database\Seeders\New;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class SellersTableSeeder extends Seeder
{
    public function run()
    {
        $datas = json_decode(File::get('database/data/sellers.json'));
        $table = 'users';

        // First we create user by this data, and attach `vendor` role. Then we create channel for this user.

        foreach ($datas as $data) {
            $user = User::create([
                'first_name' => $data->firstname,
                'last_name' => $data->lastname,
                'email' => $data->email,
                'phone_number' => str_replace('+993', '', $data->phone),
                'password' => Hash::make('12345678'),
            ]);

            $user->assignRole('vendor');

            $user->channels()->create([
                'name' => $data->first_name.' '.$data->last_name,
                'slug' => Str::slug($data->first_name.' '.$data->last_name),
            ]);
        }

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