<?php

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Create Super Admin
        $superAdmin = User::create([
            'name' => 'Super Admin',
            'email' => 'admin@juris.local',
            'password' => Hash::make('password'),
            'email_verified_at' => now(),
        ]);
        $superAdmin->assignRole('super_admin');

        $this->command->info('✓ Super Admin created: admin@juris.local / password');

        // Get portals
        $cmsPortal = Portal::where('key', 'cms')->first();
        $lmsPortal = Portal::where('key', 'lms')->first();
        $paymentsPortal = Portal::where('key', 'payments')->first();

        // Create CMS Portal Admin
        if ($cmsPortal) {
            $cmsAdmin = User::create([
                'name' => 'CMS Admin',
                'email' => 'cms.admin@juris.local',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]);
            $cmsAdmin->assignRole('portal_admin');
            
            // Assign to CMS portal with portal_admin scope
            $cmsAdmin->portals()->attach($cmsPortal->id, ['role_scope' => 'portal_admin']);

            $this->command->info('✓ CMS Portal Admin created: cms.admin@juris.local / password');
        }

        // Create CMS Regular User
        if ($cmsPortal) {
            $cmsUser = User::create([
                'name' => 'CMS User',
                'email' => 'cms.user@juris.local',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]);
            $cmsUser->assignRole('user');
            
            // Assign to CMS portal with user scope
            $cmsUser->portals()->attach($cmsPortal->id, ['role_scope' => 'user']);

            $this->command->info('✓ CMS Regular User created: cms.user@juris.local / password');
        }

        // Create LMS Portal Admin
        if ($lmsPortal) {
            $lmsAdmin = User::create([
                'name' => 'LMS Admin',
                'email' => 'lms.admin@juris.local',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]);
            $lmsAdmin->assignRole('portal_admin');
            $lmsAdmin->portals()->attach($lmsPortal->id, ['role_scope' => 'portal_admin']);

            $this->command->info('✓ LMS Portal Admin created: lms.admin@juris.local / password');
        }

        // Create Payments Portal Admin
        if ($paymentsPortal) {
            $paymentsAdmin = User::create([
                'name' => 'Payments Admin',
                'email' => 'payments.admin@juris.local',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]);
            $paymentsAdmin->assignRole('portal_admin');
            $paymentsAdmin->portals()->attach($paymentsPortal->id, ['role_scope' => 'portal_admin']);

            $this->command->info('✓ Payments Portal Admin created: payments.admin@juris.local / password');
        }

        // Create a multi-portal user (access to multiple portals)
        $multiUser = User::create([
            'name' => 'Multi Portal User',
            'email' => 'multi.user@juris.local',
            'password' => Hash::make('password'),
            'email_verified_at' => now(),
        ]);
        $multiUser->assignRole('user');
        
        if ($cmsPortal) {
            $multiUser->portals()->attach($cmsPortal->id, ['role_scope' => 'user']);
        }
        if ($lmsPortal) {
            $multiUser->portals()->attach($lmsPortal->id, ['role_scope' => 'user']);
        }

        $this->command->info('✓ Multi-Portal User created: multi.user@juris.local / password (CMS + LMS access)');

        $this->command->info('');
        $this->command->info('=== DEFAULT CREDENTIALS ===');
        $this->command->info('Super Admin:         admin@juris.local / password');
        $this->command->info('CMS Portal Admin:    cms.admin@juris.local / password');
        $this->command->info('CMS User:            cms.user@juris.local / password');
        $this->command->info('LMS Portal Admin:    lms.admin@juris.local / password');
        $this->command->info('Payments Admin:      payments.admin@juris.local / password');
        $this->command->info('Multi-Portal User:   multi.user@juris.local / password');
    }
}
