<?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('news', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->constrained()->cascadeOnDelete(); $t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $t->string('title'); $t->string('slug')->unique(); $t->text('excerpt')->nullable(); $t->longText('content'); $t->string('featured_image')->nullable();
            $t->string('category')->nullable(); $t->string('status',30)->default('draft'); $t->boolean('public_visible')->default(true); $t->boolean('aggregate_to_main')->default(true);
            $t->timestamp('published_at')->nullable(); $t->unsignedBigInteger('views')->default(0); $t->timestamps(); $t->index(['organization_id','status']);
        });
        Schema::create('letters', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->constrained()->cascadeOnDelete(); $t->string('direction',20); $t->string('number')->nullable();
            $t->string('type',80); $t->string('classification_code')->nullable(); $t->string('subject'); $t->string('sender')->nullable(); $t->string('recipient')->nullable();
            $t->date('letter_date'); $t->date('received_date')->nullable(); $t->longText('body')->nullable(); $t->string('attachment_path')->nullable();
            $t->string('status',30)->default('draft'); $t->string('signatory_1')->nullable(); $t->string('signatory_2')->nullable(); $t->string('qr_token',24)->nullable()->unique();
            $t->timestamps(); $t->index(['organization_id','direction','letter_date']);
        });
        Schema::create('letter_templates', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->nullable()->constrained()->cascadeOnDelete(); $t->string('name'); $t->string('code')->nullable();
            $t->longText('body_html'); $t->string('numbering_format')->nullable(); $t->boolean('is_active')->default(true); $t->timestamps();
        });
        Schema::create('popup_announcements', function (Blueprint $t) {
            $t->id(); $t->foreignId('organization_id')->nullable()->constrained()->cascadeOnDelete(); $t->string('title'); $t->text('body');
            $t->string('image_path')->nullable(); $t->string('button_label')->nullable(); $t->string('button_url')->nullable(); $t->string('frequency',30)->default('once_session');
            $t->timestamp('starts_at')->nullable(); $t->timestamp('ends_at')->nullable(); $t->boolean('show_desktop')->default(true); $t->boolean('show_mobile')->default(true);
            $t->boolean('is_active')->default(true); $t->unsignedInteger('sort_order')->default(0); $t->timestamps();
        });
    }
    public function down(): void { Schema::dropIfExists('popup_announcements'); Schema::dropIfExists('letter_templates'); Schema::dropIfExists('letters'); Schema::dropIfExists('news'); }
};
