<?php

namespace Database\Seeders;

use App\Models\Ecommerce\Channel\Channel;
use App\Models\Ecommerce\Product\Inventory\Inventory;
use App\Models\System\Roles\Role;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

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

    /**
     * Starter kit data
     */
    public function seedStarterKit(): void
    {
        $this->seedRoles();

        collect([
            [
                'first_name' => 'Nurmuhammet',
                'last_name' => 'Allanov',
                'email' => 'nurmuhammet@mail.com',
                'password' => bcrypt('payload10'),
            ],
        ])->each(function ($data) {
            $user = User::create($data);

            $user->assignRole('admin');
        });

        $this->createChannels();
    }

    public function seedRoles(): void
    {
        $roles = [
            [
                'id' => 1,
                'name' => 'admin',
                'guard_name' => 'web',
            ],
            [
                'id' => 2,
                'name' => 'manager',
                'guard_name' => 'web',
            ],
            [
                'id' => 3,
                'name' => 'user',
                'guard_name' => 'web',
            ],
            [
                'id' => 4,
                'name' => 'vendor',
                'guard_name' => 'web',
            ],
            [
                'id' => 5,
                'name' => 'viewer',
                'guard_name' => 'web',
            ],
        ];

        foreach ($roles as $role) {
            Role::forceCreate($role);
        }

        DB::statement("
            SELECT setval('roles_id_seq', (SELECT MAX(id) from roles))
        ");
    }

    public function createChannels(): void
    {
        collect([
            [
                'name' => 'Tmpost',
                'slug' => 'tmpost',
                'description' => 'Tmpost default',
                'timezone' => 'Asia/Ashgabat',
                'url' => 'http://shop.post.tm',
                'is_default' => true,
            ],
        ])->each(fn ($data) => Channel::create($data));

        DB::statement("
            SELECT setval('channels_id_seq', (SELECT MAX(id) from channels))
        ");

        Inventory::create([
            'id' => 14,
            'code' => 'tmpost',
            'name' => 'Tmpost inventory',
            'region' => 'ag',
            'phone_number' => '99361099310',
            'channel_id' => Channel::where('slug', 'tmpost')->first()->id,
        ]);
    }
}
