<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Permission;
use App\Models\Role;

class AppointmentPermissionsSeeder extends Seeder
{
    public function run()
    {
        // Crear permisos de citas si no existen
        $permissions = [
            'view-appointment' => 'Ver citas',
            'create-appointment' => 'Crear citas',
            'delete-appointment' => 'Eliminar citas',
            'close-appointment' => 'Cerrar citas',
            'edit-appointment' => 'Editar citas'
        ];

        foreach ($permissions as $name => $display_name) {
            Permission::firstOrCreate(
                ['name' => $name],
                [
                    'display_name' => $display_name,
                    'description' => $display_name
                ]
            );
        }

        // Asignar permisos a roles
        $roles = [
            'root' => array_keys($permissions),
            'admin' => array_keys($permissions),
            'doctor' => ['view-appointment', 'create-appointment', 'close-appointment'],
            'receptionist' => ['view-appointment', 'create-appointment']
        ];

        foreach ($roles as $roleName => $permissionNames) {
            $role = Role::where('name', $roleName)->first();
            if ($role) {
                foreach ($permissionNames as $permissionName) {
                    $permission = Permission::where('name', $permissionName)->first();
                    if ($permission && !$role->hasPermission($permissionName)) {
                        $role->attachPermission($permission);
                    }
                }
            }
        }
    }
} 