<?php

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

class CreateMedicalServicesTable extends Migration
{
    public function up()
    {
        Schema::create('medical_services', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->string('name', 100);
            $table->text('description')->nullable();
            $table->decimal('cost', 10, 2);
            $table->string('currency_id');
            $table->boolean('is_active')->default(true);
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();
            $table->timestamp('deleted_at')->nullable();
            
            $table->foreign('currency_id')
                  ->references('id')
                  ->on('currencies')
                  ->onDelete('restrict')
                  ->onUpdate('cascade');
        });

        // Agregar permisos relacionados con servicios médicos
        DB::table('permissions')->insert([
            [
                'name' => 'view-medical-service',
                'display_name' => 'Ver servicios médicos',
                'description' => 'Puede ver la lista de servicios médicos',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'create-medical-service',
                'display_name' => 'Crear servicios médicos',
                'description' => 'Puede crear nuevos servicios médicos',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'edit-medical-service',
                'display_name' => 'Editar servicios médicos',
                'description' => 'Puede editar servicios médicos existentes',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'name' => 'delete-medical-service',
                'display_name' => 'Eliminar servicios médicos',
                'description' => 'Puede eliminar servicios médicos',
                'created_at' => now(),
                'updated_at' => now()
            ]
        ]);

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

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

    public function down()
    {
        // Eliminar los permisos de la tabla permission_role
        $permissions = DB::table('permissions')
            ->whereIn('name', [
                'view-medical-service',
                'create-medical-service',
                'edit-medical-service',
                'delete-medical-service'
            ])
            ->pluck('id');

        DB::table('permission_role')
            ->whereIn('permission_id', $permissions)
            ->delete();

        // Eliminar los permisos
        DB::table('permissions')
            ->whereIn('name', [
                'view-medical-service',
                'create-medical-service',
                'edit-medical-service',
                'delete-medical-service'
            ])
            ->delete();

        Schema::dropIfExists('medical_services');
    }
} 