<?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 all permissions to admin group
        $allPermissions = Permission::all();
        $adminGroup->permissions()->sync($allPermissions->pluck('id'));

        // Assign specific permissions to other groups
        $techGroup->permissions()->sync(
            Permission::whereIn('name', ['bookings.manage', 'clients.manage', 'reports.view'])->pluck('id')
        );
        $salesGroup->permissions()->sync(
            Permission::whereIn('name', ['clients.manage', 'invoices.manage', 'reports.view'])->pluck('id')
        );
        $supportGroup->permissions()->sync(
            Permission::whereIn('name', ['bookings.manage', 'clients.manage'])->pluck('id')
        );

        // Create admin user
        $admin = User::firstOrCreate(
            ['username' => 'admin'],
            [
                'first_name' => 'System',
                'last_name' => 'Administrator',
                'email' => 'admin@seraph.local',
                'password' => bcrypt('admin123'),
                'mobile' => '07700900000',
                'extension' => 200,
                'account_type' => 'local',
                'active' => 1,
                'role' => 'Administrator',
                'points' => 1000
            ]
        );
        $admin->groups()->sync([$adminGroup->id]);

        // Create technician users
        $techUsers = [
            [
                'first_name' => 'John',
                'last_name' => 'Smith',
                'username' => 'john.smith',
                'email' => 'john.smith@seraph.local',
                'extension' => 201,
                'role' => 'Senior Technician'
            ],
            [
                'first_name' => 'Sarah',
                'last_name' => 'Johnson',
                'username' => 'sarah.johnson', 
                'email' => 'sarah.johnson@seraph.local',
                'extension' => 202,
                'role' => 'Technician'
            ],
            [
                'first_name' => 'Mike',
                'last_name' => 'Davis',
                'username' => 'mike.davis',
                'email' => 'mike.davis@seraph.local', 
                'extension' => 203,
                'role' => 'Junior Technician'
            ]
        ];

        foreach ($techUsers as $userData) {
            $user = User::firstOrCreate(
                ['username' => $userData['username']],
                array_merge($userData, [
                    'password' => bcrypt('password123'),
                    'mobile' => '0770090' . rand(1000, 9999),
                    'account_type' => 'local',
                    'active' => 1,
                    'points' => rand(100, 500)
                ])
            );
            $user->groups()->sync([$techGroup->id]);
        }

        // Create sales team
        $salesUsers = [
            [
                'first_name' => 'Emma',
                'last_name' => 'Wilson',
                'username' => 'emma.wilson',
                'email' => 'emma.wilson@seraph.local',
                'extension' => 210,
                'role' => 'Sales Manager'
            ],
            [
                'first_name' => 'Tom',
                'last_name' => 'Brown',
                'username' => 'tom.brown',
                'email' => 'tom.brown@seraph.local',
                'extension' => 211,
                'role' => 'Sales Representative'
            ]
        ];

        foreach ($salesUsers as $userData) {
            $user = User::firstOrCreate(
                ['username' => $userData['username']],
                array_merge($userData, [
                    'password' => bcrypt('password123'),
                    'mobile' => '0770090' . rand(1000, 9999),
                    'account_type' => 'local',
                    'active' => 1,
                    'points' => rand(200, 800)
                ])
            );
            $user->groups()->sync([$salesGroup->id]);
        }

        // Create support team
        $supportUser = User::firstOrCreate(
            ['username' => 'lisa.taylor'],
            [
                'first_name' => 'Lisa',
                'last_name' => 'Taylor',
                'email' => 'lisa.taylor@seraph.local',
                'password' => bcrypt('password123'),
                'mobile' => '07700901234',
                'extension' => 220,
                'account_type' => 'local',
                'active' => 1,
                'role' => 'Support Specialist',
                'points' => 350
            ]
        );
        $supportUser->groups()->sync([$supportGroup->id]);

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