<?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('addenda',function(Blueprint $t){$t->id();$t->foreignId('project_id')->constrained();$t->foreignId('service_period_id')->unique()->constrained();$t->json('data')->nullable();$t->json('draft_data')->nullable();$t->unsignedInteger('version')->default(0);$t->unsignedInteger('edits_used')->default(0);$t->timestamp('submitted_at')->nullable();$t->timestamps();});
  Schema::create('addendum_revisions',function(Blueprint $t){$t->id();$t->foreignId('addendum_id')->constrained('addenda');$t->foreignId('user_id')->constrained();$t->unsignedInteger('version');$t->json('data');$t->timestamps();$t->unique(['addendum_id','version']);});
  Schema::table('attachments',fn(Blueprint $t)=>$t->index(['context','context_id']));
  Schema::table('projects',fn(Blueprint $t)=>$t->index(['department_id','expires_at']));
  Schema::table('point_entries',fn(Blueprint $t)=>$t->index(['user_id','reference']));
  Schema::table('sms_outboxes',fn(Blueprint $t)=>$t->index(['status','due_at']));
  Schema::table('service_periods',fn(Blueprint $t)=>$t->index(['project_id','starts_at','expires_at']));
 }
 public function down():void {Schema::dropIfExists('addendum_revisions');Schema::dropIfExists('addenda');Schema::table('attachments',fn(Blueprint $t)=>$t->dropIndex(['context','context_id']));Schema::table('projects',fn(Blueprint $t)=>$t->dropIndex(['department_id','expires_at']));Schema::table('point_entries',fn(Blueprint $t)=>$t->dropIndex(['user_id','reference']));Schema::table('sms_outboxes',fn(Blueprint $t)=>$t->dropIndex(['status','due_at']));Schema::table('service_periods',fn(Blueprint $t)=>$t->dropIndex(['project_id','starts_at','expires_at']));}
};
