<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Group;
use App\Models\Permission;
use Illuminate\Support\Facades\Hash;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Create permissions first
        $permissions = [
            'admin.access',
            'bookings.manage', 
            'clients.manage',
            'invoices.manage',
            'stock.manage',
            'reports.view',
            'settings.manage'
        ];

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

        // Create user groups
        $adminGroup = Group::firstOrCreate(['name' => 'Administrators']);
        $techGroup = Group::firstOrCreate(['name' => 'Technicians']);
        $salesGroup = Group::firstOrCreate(['name' => 'Sales Team']);
        $supportGroup = Group::firstOrCreate(['name' => 'Support Team']);

        // Assign permissions to groups
        $allPermissions = Permission::all();
        $adminGroup->permissions()->sync($allPermissions->pluck('id'));
        
        $techPermissions = Permission::whereIn('name', ['bookings.manage', 'clients.manage', 'stock.manage'])->get();
        $techGroup->permissions()->sync($techPermissions->pluck('id'));
        
        $salesPermissions = Permission::whereIn('name', ['clients.manage', 'invoices.manage', 'reports.view'])->get();
        $salesGroup->permissions()->sync($salesPermissions->pluck('id'));
        
        $supportPermissions = Permission::whereIn('name', ['bookings.manage', 'clients.manage'])->get();
        $supportGroup->permissions()->sync($supportPermissions->pluck('id'));

        // Create users
        $users = [
            [
                'first_name' => 'Admin',
                'last_name' => 'User',
                'username' => 'admin',
                'email' => 'admin@seraph.local',
                'password' => Hash::make('admin123'),
                'mobile' => '07700900001',
                'extension' => 201,
                'active' => 1,
                'group' => $adminGroup
            ],
            [
                'first_name' => 'John',
                'last_name' => 'Smith',
                'username' => 'john.smith',
                'email' => 'john.smith@seraph.local',
                'password' => Hash::make('password123'),
                'mobile' => '07700900002',
                'extension' => 202,
                'active' => 1,
                'group' => $techGroup
            ],
            [
                'first_name' => 'Sarah',
                'last_name' => 'Johnson',
                'username' => 'sarah.johnson',
                'email' => 'sarah.johnson@seraph.local',
                'password' => Hash::make('password123'),
                'mobile' => '07700900003',
                'extension' => 203,
                'active' => 1,
                'group' => $techGroup
            ],
            [
                'first_name' => 'Emma',
                'last_name' => 'Wilson',
                'username' => 'emma.wilson',
                'email' => 'emma.wilson@seraph.local',
                'password' => Hash::make('password123'),
                'mobile' => '07700900004',
                'extension' => 204,
                'active' => 1,
                'group' => $salesGroup
            ]
        ];

        foreach ($users as $userData) {
            $group = $userData['group'];
            unset($userData['group']);
            
            $user = User::firstOrCreate(
                ['username' => $userData['username']],
                $userData
            );
            
            $user->groups()->sync([$group->id]);
        }

        $this->command->info('Created ' . User::count() . ' users with groups and permissions');
    }
} 