<?php

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

/**
 * Consent forms feature.
 *  - vc_consent_templates : the catalogue of consent types (bilingual, editable,
 *    placeholder-driven). business_id NULL = built-in system template.
 *  - vc_consents          : a consent actually issued for an appointment, with a
 *    snapshot of the rendered text so later template edits never alter history.
 */
class CreateVcConsentTables extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('vc_consent_templates')) {
            Schema::create('vc_consent_templates', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->nullable()->index(); // null = system default
                $table->string('code', 60)->index();
                $table->string('category', 40)->default('general')->index();
                $table->string('name_en', 191);
                $table->string('name_ar', 191)->nullable();
                $table->longText('body_en')->nullable();
                $table->longText('body_ar')->nullable();
                $table->boolean('requires_witness')->default(false);
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->enum('status', ['active', 'inactive'])->default('active');
                $table->unsignedInteger('created_by')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('vc_consents')) {
            Schema::create('vc_consents', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->index();
                $table->unsignedInteger('consent_template_id')->nullable()->index();
                $table->unsignedInteger('appointment_id')->nullable()->index();
                $table->unsignedInteger('pet_id')->nullable()->index();
                $table->unsignedInteger('contact_id')->nullable()->index();
                $table->string('code', 60)->nullable();
                $table->string('category', 40)->nullable();
                $table->string('title', 191);
                $table->enum('language', ['en', 'ar', 'both'])->default('both');
                $table->longText('body_en')->nullable();   // rendered snapshot
                $table->longText('body_ar')->nullable();   // rendered snapshot
                $table->string('signatory_name', 191)->nullable();
                $table->string('signatory_relationship', 60)->nullable();
                $table->string('witness_name', 191)->nullable();
                $table->unsignedInteger('issued_by')->nullable();
                $table->timestamp('issued_at')->nullable();
                $table->text('notes')->nullable();
                $table->timestamps();
            });
        }

        // Seed the built-in (system) consent templates.
        if (class_exists(\Modules\VetClinic\Database\Seeders\ConsentTemplatesSeeder::class)) {
            (new \Modules\VetClinic\Database\Seeders\ConsentTemplatesSeeder)->run();
        }
    }

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