<?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
    {
        // Support Tickets & Replies (Requirements 43-46)
        Schema::create('support_tickets', function (Blueprint $table) {
            $table->id();
            $table->string('ticket_code')->unique();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('reported_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('category')->default('general'); // billing, subscription, login, users, applications, whatsapp, ai, documents, accounting, bug, feature_request, integration
            $table->string('subject');
            $table->text('description');
            $table->string('priority')->default('medium'); // low, medium, high, urgent, critical
            $table->string('status')->default('open'); // open, in_progress, waiting_for_customer, resolved, closed
            $table->foreignId('assigned_user_id')->nullable()->constrained('users')->nullOnDelete(); // Platform staff agent
            $table->timestamp('sla_due_at')->nullable();
            $table->timestamp('last_response_at')->nullable();
            $table->timestamps();
        });

        Schema::create('support_replies', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ticket_id')->constrained('support_tickets')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->text('message');
            $table->boolean('is_internal_note')->default(false); // Platform staff private note
            $table->timestamps();
        });

        Schema::create('support_attachments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ticket_id')->constrained('support_tickets')->cascadeOnDelete();
            $table->foreignId('reply_id')->nullable()->constrained('support_replies')->cascadeOnDelete();
            $table->string('file_name');
            $table->string('file_path');
            $table->timestamps();
        });

        // Announcements & Targeted Notices (Requirements 49-50)
        Schema::create('announcements', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('message');
            $table->string('type')->default('info'); // info, warning, maintenance, feature, billing, critical
            $table->string('target_type')->default('all'); // all, plan, tenant, emirate, trial, platform
            $table->unsignedBigInteger('target_id')->nullable();
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->boolean('is_dismissible')->default(true);
            $table->string('priority')->default('normal'); // low, normal, high, urgent
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('platform_notifications', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('message');
            $table->string('category')->default('system'); // payment_overdue, credit_depleted, storage_limit, trial_expiring, security_alert, support_ticket
            $table->foreignId('tenant_id')->nullable()->constrained('tenants')->cascadeOnDelete();
            $table->boolean('is_read')->default(false);
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });

        // Platform Settings (Requirements 62-72)
        Schema::create('platform_settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->string('group')->default('general'); // general, branding, subscription, billing, tax, email, storage, ai, whatsapp, sms, api, security, maintenance
            $table->boolean('is_encrypted')->default(false);
            $table->timestamps();
        });

        // Impersonation & Security Activity Logs (Requirements 42, 53-54)
        Schema::create('impersonation_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('platform_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->text('reason');
            $table->timestamp('started_at');
            $table->timestamp('ended_at')->nullable();
            $table->string('ip_address')->nullable();
            $table->string('user_agent')->nullable();
            $table->string('session_id')->nullable();
            $table->timestamps();
        });

        Schema::create('security_alerts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->nullable()->constrained('tenants')->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('alert_type'); // failed_logins, suspicious_ip, rapid_switching, bulk_export, excessive_api
            $table->string('severity')->default('potential_risk'); // potential_risk, requires_review, critical
            $table->text('description');
            $table->string('ip_address')->nullable();
            $table->boolean('is_resolved')->default(false);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('security_alerts');
        Schema::dropIfExists('impersonation_logs');
        Schema::dropIfExists('platform_settings');
        Schema::dropIfExists('platform_notifications');
        Schema::dropIfExists('announcements');
        Schema::dropIfExists('support_attachments');
        Schema::dropIfExists('support_replies');
        Schema::dropIfExists('support_tickets');
    }
};
