<?php

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

return new class extends Migration
{
    /**
     * Consentimientos informados firmados por el cliente.
     */
    public function up(): void
    {
        Schema::create('consent_forms', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_history_id')->constrained()->cascadeOnDelete();
            $table->foreignId('booking_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained()->nullOnDelete();

            $table->string('title');
            $table->longText('content');                  // texto del consentimiento
            $table->string('document_path')->nullable();  // PDF o imagen adjunta
            $table->string('signature_path')->nullable(); // firma simple del cliente
            $table->string('signer_name')->nullable();
            $table->string('signer_document', 50)->nullable();
            $table->ipAddress('signed_ip')->nullable();
            $table->string('signed_user_agent')->nullable();
            $table->timestamp('signed_at')->nullable();

            $table->enum('status', ['pending', 'signed', 'rejected', 'expired'])->default('pending');

            $table->timestamps();
            $table->softDeletes();

            $table->index(['company_id', 'clinical_history_id', 'status']);
        });
    }

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