<?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('clients', function (Blueprint $table) {
            $table->id();
            $table->string('company_name');
            $table->string('contact_person')->nullable();
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->string('website')->nullable();
            $table->string('sector')->nullable();
            $table->string('status')->default('active');
            $table->string('currency_code', 3)->default('TRY');
            $table->string('timezone')->default('Europe/Istanbul');
            $table->string('report_language', 5)->default('tr');
            $table->string('logo_path')->nullable();
            $table->string('primary_color', 20)->nullable();
            $table->json('report_recipients')->nullable();
            $table->text('notes')->nullable();
            $table->text('internal_notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['status', 'company_name']);
        });

        Schema::create('client_user', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('role')->default('viewer');
            $table->timestamps();
            $table->unique(['client_id', 'user_id']);
        });

        Schema::create('secure_credentials', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->nullable()->constrained()->cascadeOnDelete();
            $table->string('platform', 40);
            $table->string('credential_type', 60);
            $table->string('name');
            $table->longText('encrypted_payload')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamp('last_checked_at')->nullable();
            $table->text('last_error')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['platform', 'credential_type', 'is_active']);
        });

        Schema::create('platform_connections', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->constrained()->cascadeOnDelete();
            $table->string('platform', 40);
            $table->string('connection_name');
            $table->string('account_id')->nullable();
            $table->string('account_name')->nullable();
            $table->string('status')->default('draft');
            $table->string('currency_code', 3)->nullable();
            $table->string('timezone')->nullable();
            $table->timestamp('last_sync_at')->nullable();
            $table->text('last_error')->nullable();
            $table->json('meta')->nullable();
            $table->foreignId('secure_credential_id')->nullable()->constrained('secure_credentials')->nullOnDelete();
            $table->timestamps();
            $table->index(['client_id', 'platform', 'status']);
        });

        Schema::create('report_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('type', 40)->default('monthly');
            $table->json('channels')->nullable();
            $table->json('sections')->nullable();
            $table->json('settings')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('report_schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->constrained()->cascadeOnDelete();
            $table->foreignId('report_template_id')->nullable()->constrained('report_templates')->nullOnDelete();
            $table->string('frequency', 30)->default('monthly');
            $table->time('daily_time')->nullable();
            $table->unsignedTinyInteger('weekly_day')->nullable();
            $table->unsignedTinyInteger('monthly_day')->nullable();
            $table->string('timezone')->default('Europe/Istanbul');
            $table->json('recipients')->nullable();
            $table->json('cc')->nullable();
            $table->json('bcc')->nullable();
            $table->boolean('include_pdf')->default(true);
            $table->boolean('include_excel')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamp('last_run_at')->nullable();
            $table->timestamp('next_run_at')->nullable();
            $table->string('last_status')->nullable();
            $table->text('last_error')->nullable();
            $table->timestamps();
            $table->index(['client_id', 'frequency', 'is_active']);
        });

        Schema::create('generated_reports', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->constrained()->cascadeOnDelete();
            $table->foreignId('report_template_id')->nullable()->constrained('report_templates')->nullOnDelete();
            $table->foreignId('report_schedule_id')->nullable()->constrained('report_schedules')->nullOnDelete();
            $table->string('report_type', 40);
            $table->date('date_from');
            $table->date('date_to');
            $table->string('status')->default('draft');
            $table->json('summary')->nullable();
            $table->string('pdf_path')->nullable();
            $table->string('excel_path')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->foreignId('generated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('error_message')->nullable();
            $table->timestamps();
            $table->index(['client_id', 'report_type', 'date_from', 'date_to']);
        });

        Schema::create('email_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('generated_report_id')->nullable()->constrained('generated_reports')->nullOnDelete();
            $table->string('subject');
            $table->json('recipients');
            $table->json('cc')->nullable();
            $table->json('bcc')->nullable();
            $table->string('status')->default('queued');
            $table->string('provider_message_id')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();
            $table->index(['status', 'sent_at']);
        });

        Schema::create('alert_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->nullable()->constrained()->cascadeOnDelete();
            $table->string('platform', 40);
            $table->string('name');
            $table->string('metric');
            $table->string('operator', 20);
            $table->decimal('threshold', 18, 4);
            $table->string('period', 30)->default('daily');
            $table->string('severity', 20)->default('warning');
            $table->json('notification_channels')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->index(['client_id', 'platform', 'is_active']);
        });

        Schema::create('alert_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('alert_rule_id')->nullable()->constrained('alert_rules')->nullOnDelete();
            $table->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $table->string('platform', 40);
            $table->string('title');
            $table->text('message')->nullable();
            $table->string('severity', 20)->default('warning');
            $table->json('payload')->nullable();
            $table->timestamp('triggered_at')->nullable();
            $table->timestamp('resolved_at')->nullable();
            $table->timestamps();
            $table->index(['client_id', 'platform', 'severity']);
        });

        Schema::create('api_request_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
            $table->string('platform', 40);
            $table->string('endpoint')->nullable();
            $table->string('method', 10)->default('GET');
            $table->unsignedSmallInteger('status_code')->nullable();
            $table->unsignedInteger('duration_ms')->nullable();
            $table->string('request_hash')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamp('requested_at')->nullable();
            $table->timestamps();
            $table->index(['platform', 'status_code', 'requested_at']);
        });

        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('group', 80)->default('general');
            $table->string('key');
            $table->longText('value')->nullable();
            $table->boolean('is_encrypted')->default(false);
            $table->timestamps();
            $table->unique(['group', 'key']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('settings');
        Schema::dropIfExists('api_request_logs');
        Schema::dropIfExists('alert_logs');
        Schema::dropIfExists('alert_rules');
        Schema::dropIfExists('email_logs');
        Schema::dropIfExists('generated_reports');
        Schema::dropIfExists('report_schedules');
        Schema::dropIfExists('report_templates');
        Schema::dropIfExists('platform_connections');
        Schema::dropIfExists('secure_credentials');
        Schema::dropIfExists('client_user');
        Schema::dropIfExists('clients');
    }
};
