<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Modules\PLATFORM\Settings\Models\Company;
use App\Modules\PLATFORM\Settings\Models\Branch;
use App\Modules\HR\Core\Models\TimeZone;
use Spatie\Permission\Models\Role;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create Roles
        $this->command->info('Creating roles...');
        $superAdminRole = Role::create(['name' => 'super-admin']);
        $adminRole = Role::create(['name' => 'admin']);
        $userRole = Role::create(['name' => 'user']);

        // Create TimeZones
        $this->command->info('Creating timezones...');
        $timeZone = TimeZone::create([
            'name' => 'Asia/Colombo',
            'utc_offset' => '+05:30',
            'status' => true,
        ]);

        // Create Company
        $this->command->info('Creating company...');
        $company = Company::create([
            'company_code' => 'DEMO-001',
            'legal_name' => 'Demo Company (Pvt) Ltd',
            'display_name' => 'Demo Company',
            'time_zone_id' => $timeZone->id,
            'status' => true,
        ]);

        // Create Branches
        $this->command->info('Creating branches...');
        $colomboBranch = Branch::create([
            'company_id' => $company->id,
            'branch_code' => 'COL-001',
            'name' => 'Colombo Branch',
            'type' => 'head-office',
            'status' => true,
        ]);

        $kandyBranch = Branch::create([
            'company_id' => $company->id,
            'branch_code' => 'KDY-001',
            'name' => 'Kandy Branch',
            'type' => 'branch',
            'status' => true,
        ]);

        $galleBranch = Branch::create([
            'company_id' => $company->id,
            'branch_code' => 'GLE-001',
            'name' => 'Galle Branch',
            'type' => 'branch',
            'status' => true,
        ]);

        // Create Super Admin User
        $this->command->info('Creating super admin user...');
        $superAdmin = User::create([
            'name' => 'Super Admin',
            'email' => 'admin@demo.com',
            'password' => Hash::make('password'),
            'company_id' => $company->id,
        ]);
        $superAdmin->assignRole($superAdminRole);

        // Create Regular User 1 (Access to Colombo and Kandy branches)
        $this->command->info('Creating regular users...');
        $user1 = User::create([
            'name' => 'John Manager',
            'email' => 'john@demo.com',
            'password' => Hash::make('password'),
            'company_id' => $company->id,
        ]);
        $user1->assignRole($adminRole);
        $user1->branches()->attach([
            $colomboBranch->id => ['is_default' => true],
            $kandyBranch->id => ['is_default' => false],
        ]);

        // Create Regular User 2 (Access to only Galle branch)
        $user2 = User::create([
            'name' => 'Jane Employee',
            'email' => 'jane@demo.com',
            'password' => Hash::make('password'),
            'company_id' => $company->id,
        ]);
        $user2->assignRole($userRole);
        $user2->branches()->attach([
            $galleBranch->id => ['is_default' => true],
        ]);

        $this->command->info('');
        $this->command->info('✅ Database seeded successfully!');
        $this->command->info('');
        $this->command->info('Login Credentials:');
        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
        $this->command->info('Super Admin:');
        $this->command->info('  Email: admin@demo.com');
        $this->command->info('  Password: password');
        $this->command->info('  Access: All branches (no branch selection needed)');
        $this->command->info('');
        $this->command->info('Regular User 1 (Manager):');
        $this->command->info('  Email: john@demo.com');
        $this->command->info('  Password: password');
        $this->command->info('  Access: Colombo Branch (default), Kandy Branch');
        $this->command->info('');
        $this->command->info('Regular User 2 (Employee):');
        $this->command->info('  Email: jane@demo.com');
        $this->command->info('  Password: password');
        $this->command->info('  Access: Galle Branch only');
        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
    }
}
