<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('brand_kits')) {
            Schema::create('brand_kits', function (Blueprint $table) {
                $table->id();
                $table->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
                $table->string('name');
                $table->text('description')->nullable();
                $table->string('agency_name')->nullable();
                $table->string('agency_logo_url', 500)->nullable();
                $table->string('client_logo_url', 500)->nullable();
                $table->string('primary_color', 20)->default('#111827');
                $table->string('accent_color', 20)->default('#ef5f86');
                $table->string('secondary_color', 20)->nullable();
                $table->string('text_color', 20)->nullable();
                $table->string('muted_color', 20)->nullable();
                $table->string('cover_style', 40)->default('gradient');
                $table->string('cover_background_url', 500)->nullable();
                $table->string('footer_note', 600)->nullable();
                $table->string('contact_email')->nullable();
                $table->string('contact_phone', 80)->nullable();
                $table->string('website_url', 220)->nullable();
                $table->json('settings')->nullable();
                $table->boolean('is_default')->default(false);
                $table->boolean('is_active')->default(true);
                $table->unsignedSmallInteger('sort_order')->default(100);
                $table->timestamps();

                $table->index(['client_id', 'is_default', 'is_active']);
            });
        }

        if (Schema::hasTable('report_templates') && ! Schema::hasColumn('report_templates', 'brand_kit_id')) {
            Schema::table('report_templates', function (Blueprint $table) {
                $table->foreignId('brand_kit_id')->nullable()->after('client_id')->constrained('brand_kits')->nullOnDelete();
            });
        }

        if (Schema::hasTable('brand_kits') && DB::table('brand_kits')->whereNull('client_id')->where('is_default', true)->count() === 0) {
            DB::table('brand_kits')->insert([
                'client_id' => null,
                'name' => 'DW Varsayılan Marka Kiti',
                'description' => 'Tüm müşteriler için kullanılabilecek varsayılan Datweb rapor kimliği.',
                'agency_name' => 'Datweb',
                'agency_logo_url' => 'https://datweb.com.tr/wp-content/uploads/2024/05/datweb-logo.png',
                'primary_color' => '#111827',
                'accent_color' => '#ef5f86',
                'secondary_color' => '#f8fafc',
                'text_color' => '#111827',
                'muted_color' => '#6b7280',
                'cover_style' => 'gradient',
                'footer_note' => 'Bu rapor Datweb reklam raporlama paneli tarafından otomatik oluşturulmuştur.',
                'website_url' => 'https://datweb.com.tr',
                'settings' => json_encode(['created_by_migration' => true], JSON_UNESCAPED_UNICODE),
                'is_default' => true,
                'is_active' => true,
                'sort_order' => 10,
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('report_templates') && Schema::hasColumn('report_templates', 'brand_kit_id')) {
            Schema::table('report_templates', function (Blueprint $table) {
                $table->dropConstrainedForeignId('brand_kit_id');
            });
        }

        Schema::dropIfExists('brand_kits');
    }
};
