<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

use App\Modules\User\Models\User;
use App\Modules\Admin\Models\Admin;

class RolesAndUsersSeeder extends Seeder
{
    public function run(): void
    {
        /**
         *  Reset Spatie cache
         */
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        /**
         * =========================
         * ROLES (USERS)
         * =========================
         */
        $customerRole = Role::firstOrCreate([
            'name' => 'customer',
            'guard_name' => 'api',
        ]);

        /**
         * =========================
         * ROLES (ADMINS)
         * =========================
         */
        $adminRole = Role::firstOrCreate([
            'name' => 'admin',
            'guard_name' => 'admin',
        ]);

        $managerRole = Role::firstOrCreate([
            'name' => 'manager',
            'guard_name' => 'admin',
        ]);

        /**
         * =========================
         * USERS (CUSTOMERS)
         * =========================
         */
        for ($i = 1; $i <= 3; $i++) {

            $user = User::firstOrCreate(
                ['email' => "customer$i@test.com"],
                [
                    'name' => "Customer $i",
                    'password' => Hash::make('password'),
                ]
            );

            $user->syncRoles(['customer']);
        }

        /**
         * =========================
         * ADMINS
         * =========================
         */

        $admin = Admin::firstOrCreate(
            ['email' => 'admin@test.com'],
            [
                'name' => 'Super Admin',
                'password' => Hash::make('password'),
            ]
        );

        $admin->syncRoles(['admin']);

        $manager = Admin::firstOrCreate(
            ['email' => 'manager@test.com'],
            [
                'name' => 'Manager',
                'password' => Hash::make('password'),
            ]
        );

        $manager->syncRoles(['manager']);

        $staff = Admin::firstOrCreate(
            ['email' => 'staff@test.com'],
            [
                'name' => 'Staff',
                'password' => Hash::make('password'),
            ]
        );

        $staff->syncRoles(['manager']);

        /**
         * =========================
         * PERMISSIONS (ADMIN SIDE)
         * =========================
         */
        Permission::firstOrCreate([
            'name' => 'view dashboard',
            'guard_name' => 'admin',
        ]);
    }
}