<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class CreateBudgetsTable extends Migration
{
    public function up()
    {
        Schema::create('budgets', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->string('code')->unique();
            $table->string('patients_id');
            $table->decimal('total', 10, 2)->default(0);
            $table->enum('status', ['draft', 'sent', 'approved', 'rejected'])->default('draft');
            $table->text('notes')->nullable();
            $table->unsignedBigInteger('created_by');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('patients_id')
                  ->references('id')
                  ->on('patients')
                  ->onDelete('restrict');

            $table->foreign('created_by')
                  ->references('id')
                  ->on('users')
                  ->onDelete('restrict');
        });

        // Agregar permisos relacionados con presupuestos
        DB::table('permissions')->insert([
            [
                'name' => 'view-budget',
                'display_name' => 'Ver presupuestos',
                'description' => 'Puede ver la lista de presupuestos',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'create-budget',
                'display_name' => 'Crear presupuestos',
                'description' => 'Puede crear nuevos presupuestos',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'edit-budget',
                'display_name' => 'Editar presupuestos',
                'description' => 'Puede editar presupuestos existentes',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'delete-budget',
                'display_name' => 'Eliminar presupuestos',
                'description' => 'Puede eliminar presupuestos',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'send-budget',
                'display_name' => 'Enviar presupuestos',
                'description' => 'Puede enviar presupuestos por correo',
                'created_at' => now(),
                'updated_at' => now()
            ]
        ]);

        // Asignar permisos al rol de administrador
        $adminRoleId = DB::table('roles')->where('name', 'admin')->value('id');
        if ($adminRoleId) {
            $permissions = DB::table('permissions')
                ->whereIn('name', [
                    'view-budget',
                    'create-budget',
                    'edit-budget',
                    'delete-budget',
                    'send-budget'
                ])
                ->get();

            foreach ($permissions as $permission) {
                DB::table('permission_role')->insert([
                    'permission_id' => $permission->id,
                    'role_id' => $adminRoleId
                ]);
            }
        }
    }

    public function down()
    {
        Schema::dropIfExists('budgets');
    }
} 