<?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('settlement_batches',function(Blueprint $t){$t->id();$t->string('batch_ref')->unique();$t->foreignId('merchant_id')->constrained('merchants');$t->char('currency',3);$t->timestamp('period_from');$t->timestamp('period_to');$t->unsignedBigInteger('gross_collected_minor')->default(0);$t->unsignedBigInteger('gross_paid_minor')->default(0);$t->bigInteger('net_position_minor')->default(0);$t->enum('status',['draft','confirmed','settled'])->default('draft');$t->string('reference')->nullable();$t->date('settled_on')->nullable();$t->foreignId('settled_by')->nullable()->constrained('users')->nullOnDelete();$t->text('note')->nullable();$t->timestamps();$t->unique(['merchant_id','currency','period_from','period_to'],'settlement_period_unique');});
 Schema::table('transfers',fn(Blueprint $t)=>$t->foreign('settlement_batch_id')->references('id')->on('settlement_batches')->nullOnDelete());
 Schema::create('ledger_entries',function(Blueprint $t){$t->id();$t->foreignId('merchant_id')->constrained('merchants');$t->foreignId('transfer_id')->nullable()->constrained('transfers');$t->foreignId('settlement_batch_id')->nullable()->constrained('settlement_batches');$t->enum('direction',['debit','credit']);$t->unsignedBigInteger('amount_minor');$t->char('currency',3);$t->enum('type',['collection','payout','settlement','adjustment','reversal']);$t->bigInteger('balance_after_minor');$t->string('note',255)->nullable();$t->foreignId('created_by')->constrained('users');$t->timestamp('created_at')->useCurrent();$t->index(['merchant_id','currency','created_at']);$t->index(['transfer_id','type']);});
 Schema::create('audit_logs',function(Blueprint $t){$t->id();$t->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();$t->string('action');$t->string('auditable_type');$t->unsignedBigInteger('auditable_id')->nullable();$t->json('old_values')->nullable();$t->json('new_values')->nullable();$t->string('ip',45)->nullable();$t->text('user_agent')->nullable();$t->timestamp('created_at')->useCurrent();$t->index(['auditable_type','auditable_id']);$t->index(['user_id','created_at']);});
 Schema::create('idempotency_records',function(Blueprint $t){$t->id();$t->foreignId('user_id')->constrained('users');$t->string('key',64);$t->string('endpoint',120);$t->char('request_hash',64);$t->unsignedSmallInteger('response_status')->nullable();$t->longText('response_body_encrypted')->nullable();$t->string('resource_type')->nullable();$t->unsignedBigInteger('resource_id')->nullable();$t->timestamp('created_at')->useCurrent();$t->timestamp('expires_at')->nullable();$t->unique(['user_id','key','endpoint']);});
 }public function down():void{Schema::dropIfExists('idempotency_records');Schema::dropIfExists('audit_logs');Schema::dropIfExists('ledger_entries');Schema::table('transfers',fn(Blueprint $t)=>$t->dropForeign(['settlement_batch_id']));Schema::dropIfExists('settlement_batches');}};
