<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->createRolesAndPermissions();
        $this->createSuperAdmin();
    }

    private function createRolesAndPermissions()
    {
        // ========== PERMISSIONS ==========
        $permissionGroups = [
            'company' => [
                'company.view', 'company.create', 'company.edit', 'company.delete', 'company.manage.all'
            ],
            'user' => [
                'user.view', 'user.create', 'user.edit', 'user.delete', 'user.manage.roles'
            ],
            'employee' => [
                'employee.view', 'employee.create', 'employee.edit', 'employee.delete',
                'employee.import', 'employee.export', 'employee.transfer'
            ],
            'department' => [
                'department.view', 'department.create', 'department.edit', 'department.delete'
            ],
            'payroll' => [
                'payroll.view', 'payroll.process', 'payroll.edit', 'salary.view', 'salary.edit'
            ],
            'leave' => [
                'leave.view', 'leave.request', 'leave.approve', 'leave.manage'
            ],
            'performance' => [
                'performance.view', 'performance.review', 'performance.manage'
            ],
            'finance' => [
                'finance.view', 'expense.view', 'expense.create', 'expense.approve',
                'loan.view', 'loan.create', 'loan.approve',
                'profit_sweep.view', 'profit_sweep.execute'
            ],
            'audit' => [
                'audit.view', 'audit.trail', 'audit.export'
            ],
            'report' => [
                'report.view', 'report.create', 'report.export',
                'dashboard.view', 'dashboard.company', 'dashboard.group'
            ],
            'settings' => [
                'settings.view', 'settings.edit', 'role.manage', 'permission.manage'
            ],
            'invoice' => [
                'invoice.view', 'invoice.create', 'invoice.edit', 'invoice.delete'
            ],
        ];

        $allPermissions = [];
        foreach ($permissionGroups as $perms) {
            foreach ($perms as $perm) {
                Permission::firstOrCreate(['name' => $perm, 'guard_name' => 'web']);
                $allPermissions[] = $perm;
            }
        }

        // ========== ALL ROLES (global, company_id = null) ==========
        $roles = [
            'Super Admin' => $allPermissions,
            'Group Admin' => [
                'company.view', 'company.create', 'company.edit',
                'user.view', 'user.create', 'user.edit', 'user.manage.roles',
                'employee.view', 'employee.create', 'employee.edit', 'employee.transfer',
                'department.view', 'department.create', 'department.edit',
                'payroll.view', 'salary.view',
                'leave.view', 'leave.approve',
                'performance.view', 'performance.review',
                'finance.view', 'expense.view', 'expense.approve', 'loan.view', 'loan.approve',
                'profit_sweep.view', 'profit_sweep.execute',
                'audit.view', 'audit.trail', 'audit.export',
                'report.view', 'report.create', 'report.export',
                'dashboard.view', 'dashboard.company', 'dashboard.group',
                'settings.view', 'settings.edit', 'role.manage', 'permission.manage',
                'invoice.view', 'invoice.create', 'invoice.edit',
            ],
            'Company Admin' => [
                'company.view', 'company.edit',
                'user.view', 'user.create', 'user.edit', 'user.delete', 'user.manage.roles',
                'employee.view', 'employee.create', 'employee.edit', 'employee.delete', 'employee.import', 'employee.export', 'employee.transfer',
                'department.view', 'department.create', 'department.edit', 'department.delete',
                'payroll.view', 'payroll.process', 'payroll.edit', 'salary.view', 'salary.edit',
                'leave.view', 'leave.approve', 'leave.manage',
                'performance.view', 'performance.review', 'performance.manage',
                'finance.view', 'expense.view', 'expense.create', 'expense.approve', 'loan.view', 'loan.create',
                'profit_sweep.view',
                'audit.view', 'audit.trail',
                'report.view', 'report.create', 'report.export',
                'dashboard.view', 'dashboard.company',
                'settings.view', 'settings.edit', 'role.manage', 'permission.manage',
                'invoice.view', 'invoice.create', 'invoice.edit', 'invoice.delete',
            ],
            'HR Manager' => [
                'employee.view', 'employee.create', 'employee.edit', 'employee.import', 'employee.export',
                'department.view', 'department.create', 'department.edit',
                'payroll.view', 'payroll.process', 'salary.view',
                'leave.view', 'leave.approve', 'leave.manage',
                'performance.view', 'performance.review', 'performance.manage',
                'report.view', 'dashboard.view', 'dashboard.company',
                'user.view', 'user.create', 'user.edit',
            ],
            'Finance Manager' => [
                'finance.view', 'expense.view', 'expense.create', 'expense.approve',
                'loan.view', 'loan.create',
                'profit_sweep.view',
                'payroll.view', 'payroll.process', 'salary.view',
                'report.view', 'report.create', 'report.export',
                'dashboard.view', 'dashboard.company',
                'audit.view', 'audit.trail',
                'invoice.view', 'invoice.create', 'invoice.edit',
            ],
            'Operations Manager' => [
                'department.view', 'department.create', 'department.edit',
                'employee.view', 'employee.edit',
                'expense.view', 'expense.create',
                'report.view', 'dashboard.view', 'dashboard.company',
            ],
            'Staff' => [
                'employee.view',
                'leave.view', 'leave.request',
                'performance.view',
                'expense.view', 'expense.create',
                'dashboard.view', 'dashboard.company',
            ],
            'Auditor' => [
                'audit.view', 'audit.trail', 'audit.export',
                'report.view', 'report.export',
                'finance.view', 'expense.view',
                'payroll.view', 'salary.view',
                'dashboard.view', 'dashboard.company',
            ],
        ];

        foreach ($roles as $roleName => $perms) {
            $role = Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web', 'company_id' => null]);
            $role->syncPermissions($perms);
            $this->command->info("Role created/updated: {$roleName}");
        }
    }

    private function createSuperAdmin()
    {
        $superAdmin = User::firstOrCreate(
            ['email' => 'admin@sarkinmotagroup.com'],
            [
                'name' => 'Super Administrator',
                'password' => Hash::make('Admin@1234'),
                'status' => 'active',
            ]
        );

        $superAdmin->syncRoles(['Super Admin']);

        $this->command->info('');
        $this->command->info('✓ Super Admin ready:');
        $this->command->info('  Email:    admin@sarkinmotagroup.com');
        $this->command->info('  Password: Admin@1234');
        $this->command->info('');
        $this->command->info('All roles seeded. No sample data created — use the UI to add real data.');
    }
}
