<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class BudgetPermissionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $permissions = [
            [
                'name' => 'view-budget',
                'display_name' => 'View Budget',
                'description' => 'View Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'create-budget',
                'display_name' => 'Create Budget',
                'description' => 'Create Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'edit-budget',
                'display_name' => 'Edit Budget',
                'description' => 'Edit Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'delete-budget',
                'display_name' => 'Delete Budget',
                'description' => 'Delete Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'send-budget',
                'display_name' => 'Send Budget',
                'description' => 'Send Budget by Email',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'approve-budget',
                'display_name' => 'Approve Budget',
                'description' => 'Approve Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => 'reject-budget',
                'display_name' => 'Reject Budget',
                'description' => 'Reject Budget',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        // Definir qué permisos tiene cada rol
        $rolePermissions = [
            'doctor' => ['view-budget', 'create-budget', 'edit-budget', 'send-budget', 'approve-budget', 'reject-budget'],
            'receptionist' => ['view-budget', 'create-budget', 'edit-budget', 'send-budget'],
            'admin' => array_column($permissions, 'name'),
            'root' => array_column($permissions, 'name')
        ];

        foreach ($permissions as $permission) {
            // Solo insertar si no existe
            if (!DB::table('permissions')->where('name', $permission['name'])->exists()) {
                $permissionId = DB::table('permissions')->insertGetId($permission);

                // Asignar a roles según la configuración
                $roles = DB::table('roles')->whereIn('name', array_keys($rolePermissions))->get();
                foreach ($roles as $role) {
                    // Verificar si este rol debe tener este permiso
                    if (in_array($permission['name'], $rolePermissions[$role->name])) {
                        if (!DB::table('permission_role')->where([
                            'permission_id' => $permissionId,
                            'role_id' => $role->id
                        ])->exists()) {
                            DB::table('permission_role')->insert([
                                'permission_id' => $permissionId,
                                'role_id' => $role->id
                            ]);
                        }
                    }
                }
            }
        }
    }
} 