<?php

namespace Maestro\Core\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Maestro\Core\Models\Organization;
use Maestro\Core\Support\AuthUserResolver;
use Maestro\Core\Support\PermissionGuardName;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $userClass = AuthUserResolver::eloquentModelClass();

        $guardName = PermissionGuardName::forApp();

        $permissionNames = [
            'platform.superadmin',
            'activitylog.view',
            'settings.organization.view',
            'settings.organization.edit',
            'users.view',
            'users.create',
            'users.edit',
            'users.delete',
            'users.assign-roles',
            'roles.view',
            'roles.create',
            'roles.edit',
            'roles.delete',
            'customers.view',
            'customers.create',
            'customers.edit',
            'customers.delete',
            'hours.week.manage',
            'hours.approve',
            'hours.reports.view',
            'hours.export',
            'hours.invoicing.read',
            'hours.admin',
            'offers.view',
            'offers.create',
            'offers.edit',
            'offers.delete',
            'offers.send',
            'offers.signatures.review',
            'offers.catalog.manage',
            'offers.templates.manage',
            'offers.convert.project',
            'invoices.view',
            'invoices.create',
            'invoices.edit',
            'invoices.delete',
            'invoices.send',
            'invoices.book',
            'invoices.vat.manage',
            'items.manage',
        ];

        foreach ($permissionNames as $name) {
            Permission::firstOrCreate([
                'name' => $name,
                'guard_name' => $guardName,
            ]);
        }

        $employeeRole = Role::firstOrCreate([
            'name' => 'employee',
            'guard_name' => $guardName,
        ]);

        $leadRole = Role::firstOrCreate([
            'name' => 'project_lead',
            'guard_name' => $guardName,
        ]);

        $employeeRole->syncPermissions([
            'hours.week.manage',
        ]);

        $leadRole->syncPermissions([
            'hours.approve',
            'hours.reports.view',
            'hours.export',
        ]);

        $adminRole = Role::firstOrCreate([
            'name' => 'admin',
            'guard_name' => $guardName,
        ]);

        $adminPermissions = Permission::query()
            ->where('guard_name', $guardName)
            ->whereIn('name', $permissionNames)
            ->get();

        $adminRole->syncPermissions($adminPermissions);

        app(PermissionRegistrar::class)->forgetCachedPermissions();

        Organization::query()->updateOrCreate(
            ['id' => 1],
            [
                'name' => 'Maestro Demo BV',
                'company_name' => 'Maestro Demo BV',
                'street' => 'Stationsplein 1',
                'address_line_1' => 'Stationsplein 1',
                'city' => 'Amsterdam',
                'postal_code' => '1012 AB',
                'country_code' => 'nl',
                'country' => 'Nederland',
            ],
        );

        $user = $userClass::query()->updateOrCreate(
            ['email' => 'demo@example.com'],
            [
                'name' => 'Demo Admin',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ],
        );

        $user->syncRoles(['admin']);
    }
}
