<?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('mexco_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('admin_user_id')->nullable()->constrained('meetup_super_admins')->nullOnDelete();
            $table->string('actor_type', 64)->nullable();
            $table->unsignedBigInteger('actor_id')->nullable();
            $table->string('action', 64);
            $table->string('entity_type', 64)->nullable();
            $table->string('entity_id')->nullable();
            $table->json('old_values')->nullable();
            $table->json('new_values')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['entity_type', 'entity_id'], 'mexco_audit_entity_idx');
            $table->index('action');
            $table->index('created_at');
        });

        Schema::create('mexco_notification_templates', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->unique();
            $table->string('name');
            $table->string('subject');
            $table->text('body');
            $table->string('channel', 16)->default('email');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('mexco_notification_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->nullable()->constrained('mexco_notification_templates')->nullOnDelete();
            $table->string('template_code', 64)->nullable();
            $table->string('recipient');
            $table->string('subject');
            $table->text('body');
            $table->string('channel', 16)->default('email');
            $table->string('status', 32)->default('logged');
            $table->string('entity_type', 64)->nullable();
            $table->string('entity_id')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();

            $table->index('status');
            $table->index('created_at');
        });

        Schema::create('mexco_support_tickets', function (Blueprint $table) {
            $table->id();
            $table->string('ticket_number', 32)->unique();
            $table->foreignId('organisation_id')->nullable()->constrained('mexco_organisations')->nullOnDelete();
            $table->string('event_id')->nullable();
            $table->foreignId('organisation_user_id')->nullable()->constrained('mexco_organisation_users')->nullOnDelete();
            $table->string('contact_name')->nullable();
            $table->string('contact_email')->nullable();
            $table->string('subject');
            $table->text('description');
            $table->string('priority', 16)->default('normal');
            $table->string('status', 32)->default('new');
            $table->foreignId('assigned_admin_id')->nullable()->constrained('meetup_super_admins')->nullOnDelete();
            $table->timestamps();

            $table->foreign('event_id')->references('id')->on('meetup_events')->nullOnDelete();
            $table->index('status');
            $table->index('priority');
        });

        Schema::create('mexco_support_ticket_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ticket_id')->constrained('mexco_support_tickets')->cascadeOnDelete();
            $table->string('author_type', 32);
            $table->unsignedBigInteger('author_id')->nullable();
            $table->text('body');
            $table->timestamp('created_at')->useCurrent();

            $table->index('ticket_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('mexco_support_ticket_messages');
        Schema::dropIfExists('mexco_support_tickets');
        Schema::dropIfExists('mexco_notification_logs');
        Schema::dropIfExists('mexco_notification_templates');
        Schema::dropIfExists('mexco_audit_logs');
    }
};
