<?php

namespace Database\Seeders;

use App\Models\{User,
    Company,
    People,
    Company_user,
    membership_users
    };
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class RooleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // crear cpmpany, luego crear users, luego asignar roles y permisos a los usuarios
        $company = Company::firstOrCreate(
            ['name' => 'Box SJQ'],
            [
                'address' => '123 Fitness St',
                'phone' => '555-1234',
                'email' => 'info@fitcompany.com',
                'bg_color' => '#222222',
                'text_color' => '#ffffff',
                'url_imagen_back' => 'fondo-box.jpg',
                'bg_color_ligth' => '#eeeeee'
            ]
        );








        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        $permissions = [
            'dashboard.ver',
            'users.listar',
            'users.crear',
            'users.editar',
            'users.eliminar',
            'memberships.listar',
            'memberships.crear',
            'memberships.editar',
            'memberships.eliminar',
            'employees.listar',
            'employees.crear',
            'employees.editar',
            'employees.eliminar',
            'products.listar',
            'products.crear',
            'products.editar',
            'products.eliminar',
        ];

        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission, 'guard_name' => 'web']);
        }

        $adminRole = Role::firstOrCreate(['name' => 'Administrador', 'guard_name' => 'web']);
        $employeeRole = Role::firstOrCreate(['name' => 'Empleado', 'guard_name' => 'web']);
        $peopleRole = Role::firstOrCreate(['name' => 'Cliente', 'guard_name' => 'web']);
        $superAdminRole = Role::firstOrCreate(['name' => 'SuperAdmin', 'guard_name' => 'web']);

        $adminRole->syncPermissions(Permission::where('guard_name', 'web')->pluck('name')->toArray());
        $superAdminRole->syncPermissions(Permission::where('guard_name', 'web')->pluck('name')->toArray());

        $employeeRole->syncPermissions([
            'dashboard.ver',
            'memberships.listar',
            'memberships.crear',
            'memberships.editar',
            'products.listar',
        ]);

        $peopleRole->syncPermissions([
            'dashboard.ver',
            'products.listar',
        ]);

        $adminUser = User::firstOrCreate(
            ['email' => 'adminfit@example.com'],
            ['name' => 'Admin Fit', 'password' => bcrypt('A123456')]
        );
        // crear people, asignar company_user y membership_users
        $peopleAdmin = People::firstOrCreate(
            ['document_number' => '1234567890'],
            [
                'last_name' => 'Admin',
                'birth_date' => '1990-01-01',
                'gender' => 'male',
                'phone' => '555-0000',
                'address' => 'Admin Street',
                'document_type' => 'cedula',
            ]
        );
        $adminUser->people_id = $peopleAdmin->id;
        $adminUser->save();

        Company_user::firstOrCreate(
            ['company_id' => $company->id, 'user_id' => $adminUser->id],
            ['status' => true]
        );

        $adminUser->syncRoles(['Administrador']);

        $superAdminUser = User::firstOrCreate(
            ['email' => 'superadmin@example.com'],
            ['name' => 'Super Admin', 'password' => bcrypt('SuperAdmin123')]
        );
        $superAdminUser->syncRoles(['SuperAdmin']);
        


    }
}
