<?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::table('merchants',function(Blueprint $t){$t->foreignId('parent_merchant_id')->nullable()->after('id')->constrained('merchants')->nullOnDelete();$t->string('merchant_type',20)->default('merchant')->index();$t->string('external_reference',100)->nullable();$t->json('treasury_policy')->nullable();$t->index(['parent_merchant_id','status']);});
  Schema::table('assets',function(Blueprint $t){$t->string('provider')->default('manual');$t->string('provider_chain_id',100)->nullable();$t->string('provider_token_id',255)->nullable();$t->string('provider_currency',355)->nullable();$t->boolean('deposit_enabled')->default(false);$t->boolean('payout_enabled')->default(false);$t->timestamp('provider_synced_at')->nullable();$t->json('provider_metadata')->nullable();$t->unique(['provider','provider_chain_id','provider_token_id'],'assets_provider_coin_unique');});
  Schema::table('wallet_addresses',function(Blueprint $t){$t->foreignId('merchant_id')->nullable()->after('wallet_id')->constrained()->nullOnDelete();$t->string('alias')->nullable();$t->string('status',20)->default('active')->index();$t->decimal('provider_balance_atomic',65,0)->default(0);$t->timestamp('balance_synced_at')->nullable();$t->timestamp('last_collected_at')->nullable();$t->index(['merchant_id','environment','status']);});
  Schema::table('invoices',function(Blueprint $t){$t->foreignId('sub_merchant_id')->nullable()->after('merchant_id')->constrained('merchants')->nullOnDelete();$t->string('payer_id',100)->nullable();$t->string('payer_name')->nullable();$t->string('payer_email')->nullable();$t->boolean('accept_partial')->default(true);$t->boolean('accept_overpayment')->default(true);$t->decimal('underpayment_tolerance_atomic',65,0)->default(0);$t->decimal('overpayment_tolerance_atomic',65,0)->default(0);$t->string('late_payment_policy',30)->default('manual_review');$t->string('locale',10)->default('en');});
  Schema::create('invoice_items',function(Blueprint $t){$t->id();$t->foreignId('invoice_id')->constrained()->cascadeOnDelete();$t->string('sku',100)->nullable();$t->string('name');$t->text('description')->nullable();$t->unsignedInteger('quantity');$t->bigInteger('unit_amount_minor');$t->bigInteger('tax_amount_minor')->default(0);$t->bigInteger('discount_amount_minor')->default(0);$t->char('currency',3);$t->json('metadata')->nullable();$t->timestamps();});
  Schema::create('address_collections',function(Blueprint $t){$t->id();$t->string('public_id',40)->unique();$t->foreignId('wallet_address_id')->constrained()->restrictOnDelete();$t->foreignId('asset_id')->constrained();$t->string('environment',16);$t->decimal('requested_amount_atomic',65,0)->nullable();$t->decimal('collected_amount_atomic',65,0)->default(0);$t->string('provider_reference')->nullable()->index();$t->string('provider_idempotency_key')->unique();$t->string('tx_hash')->nullable();$t->string('status',30)->index();$t->text('failure_reason')->nullable();$t->timestamps();});
  Schema::create('treasury_wallets',function(Blueprint $t){$t->id();$t->string('public_id',40)->unique();$t->foreignId('merchant_id')->nullable()->constrained()->nullOnDelete();$t->foreignId('asset_id')->constrained();$t->string('environment',16);$t->string('provider_wallet_id')->nullable();$t->string('address')->nullable();$t->string('status',20)->default('active');$t->boolean('collection_target')->default(false);$t->json('policy')->nullable();$t->timestamps();$t->unique(['merchant_id','asset_id','environment','collection_target'],'treasury_scope_unique');});
  Schema::create('risk_screenings',function(Blueprint $t){$t->id();$t->string('public_id',40)->unique();$t->foreignId('merchant_id')->nullable()->constrained()->nullOnDelete();$t->string('subject_type');$t->string('subject_id');$t->string('provider')->default('manual');$t->string('address');$t->string('network');$t->string('status',30)->index();$t->string('risk_level',20)->nullable();$t->decimal('risk_score',8,4)->nullable();$t->json('flags')->nullable();$t->json('raw_result')->nullable();$t->timestamp('screened_at')->nullable();$t->timestamp('expires_at')->nullable();$t->timestamps();$t->index(['subject_type','subject_id']);});
  Schema::create('provider_coin_sync_runs',function(Blueprint $t){$t->id();$t->string('provider')->default('manual');$t->string('environment',16);$t->string('status',20);$t->unsignedInteger('received_count')->default(0);$t->unsignedInteger('created_count')->default(0);$t->unsignedInteger('updated_count')->default(0);$t->json('errors')->nullable();$t->timestamps();});
 }
 public function down():void {Schema::dropIfExists('provider_coin_sync_runs');Schema::dropIfExists('risk_screenings');Schema::dropIfExists('treasury_wallets');Schema::dropIfExists('address_collections');Schema::dropIfExists('invoice_items');Schema::table('invoices',fn(Blueprint $t)=>$t->dropColumn(['sub_merchant_id','payer_id','payer_name','payer_email','accept_partial','accept_overpayment','underpayment_tolerance_atomic','overpayment_tolerance_atomic','late_payment_policy','locale']));Schema::table('wallet_addresses',fn(Blueprint $t)=>$t->dropColumn(['merchant_id','alias','status','provider_balance_atomic','balance_synced_at','last_collected_at']));Schema::table('assets',fn(Blueprint $t)=>$t->dropColumn(['provider','provider_chain_id','provider_token_id','provider_currency','deposit_enabled','payout_enabled','provider_synced_at','provider_metadata']));Schema::table('merchants',fn(Blueprint $t)=>$t->dropColumn(['parent_merchant_id','merchant_type','external_reference','treasury_policy']));}
};