<?php
use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;
return new class extends Migration{public function up():void{Schema::create('audit_logs',function(Blueprint $t){$t->id();$t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();$t->string('action',60);$t->string('module',80);$t->string('record_type');$t->unsignedBigInteger('record_id');$t->json('old_values')->nullable();$t->json('new_values')->nullable();$t->text('reason')->nullable();$t->string('ip_address',45)->nullable();$t->text('user_agent')->nullable();$t->timestamp('occurred_at')->index();$t->index(['record_type','record_id']);});Schema::create('einvoice_configurations',function(Blueprint $t){$t->id();$t->foreignId('company_id')->constrained()->cascadeOnDelete();$t->string('provider_name');$t->string('environment')->default('sandbox');$t->text('encrypted_credentials');$t->string('electronic_address')->nullable();$t->string('electronic_address_scheme')->nullable();$t->boolean('is_active')->default(false);$t->boolean('production_enabled')->default(false);$t->timestamp('last_tested_at')->nullable();$t->string('connection_status')->default('not_tested');$t->timestamps();$t->unique(['company_id','provider_name','environment'],'einvoice_config_company_provider_env_unique');});Schema::create('einvoice_documents',function(Blueprint $t){$t->id();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->morphs('document');$t->string('provider_name');$t->string('status',40)->index();$t->string('message_id')->nullable()->index();$t->longText('original_payload')->nullable();$t->longText('final_payload')->nullable();$t->string('payload_hash',64)->nullable();$t->json('provider_request')->nullable();$t->json('provider_response')->nullable();$t->json('validation_errors')->nullable();$t->unsignedInteger('retry_count')->default(0);$t->timestamp('submitted_at')->nullable();$t->timestamp('delivered_at')->nullable();$t->timestamps();});Schema::create('einvoice_status_histories',function(Blueprint $t){$t->id();$t->foreignId('einvoice_document_id')->constrained()->cascadeOnDelete();$t->string('from_status')->nullable();$t->string('to_status');$t->json('provider_response')->nullable();$t->text('reason')->nullable();$t->timestamp('occurred_at');});}public function down():void{Schema::dropIfExists('einvoice_status_histories');Schema::dropIfExists('einvoice_documents');Schema::dropIfExists('einvoice_configurations');Schema::dropIfExists('audit_logs');}};
