<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Crea i ruoli base
        $roles = [
            'super-admin',
            'admin',
            'amministratore',
            'condomino',
            'inquilino',
            'proprietario'
        ];

        foreach ($roles as $roleName) {
            Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web']);
        }

        // Crea permissions di base
        $permissions = [
            'view-dashboard',
            'manage-stabili',
            'manage-condomini',
            'manage-users',
            'view-reports',
            'manage-tickets',
            'manage-accounting'
        ];

        foreach ($permissions as $permissionName) {
            Permission::firstOrCreate(['name' => $permissionName, 'guard_name' => 'web']);
        }

        // Assegna permissions ai ruoli
        $superAdmin = Role::findByName('super-admin');
        $superAdmin->givePermissionTo(Permission::all());

        $admin = Role::findByName('admin');
        $admin->givePermissionTo([
            'view-dashboard',
            'manage-stabili',
            'manage-condomini',
            'view-reports',
            'manage-tickets',
            'manage-accounting'
        ]);

        $amministratore = Role::findByName('amministratore');
        $amministratore->givePermissionTo([
            'view-dashboard',
            'manage-stabili',
            'manage-condomini',
            'view-reports',
            'manage-tickets'
        ]);

        $condomino = Role::findByName('condomino');
        $condomino->givePermissionTo([
            'view-dashboard',
            'view-reports'
        ]);
    }
}
