<?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::table('users', function (Blueprint $table): void {
            $table->string('profile_image')->nullable()->after('avatar_path');
            $table->text('bio')->nullable()->after('notes');
        });

        Schema::table('payment_methods', function (Blueprint $table): void {
            $table->string('method_name')->nullable()->after('name');
            $table->string('bank_name')->nullable()->after('account_number');
            $table->string('qr_code')->nullable()->after('instructions');
        });

        Schema::create('website_settings', function (Blueprint $table): void {
            $table->id();
            $table->string('website_name')->default('M Ghaffar Graphics');
            $table->string('agency_name')->default('M Ghaffar Graphics');
            $table->string('tagline')->nullable();
            $table->string('logo')->nullable();
            $table->string('favicon')->nullable();
            $table->string('contact_number')->nullable();
            $table->string('whatsapp_number')->nullable();
            $table->string('email')->nullable();
            $table->text('address')->nullable();
            $table->string('facebook')->nullable();
            $table->string('instagram')->nullable();
            $table->string('tiktok')->nullable();
            $table->string('youtube')->nullable();
            $table->text('footer_text')->nullable();
            $table->timestamps();
        });

        Schema::create('brand_settings', function (Blueprint $table): void {
            $table->id();
            $table->string('primary_color')->default('#0D8BFF');
            $table->string('secondary_color')->default('#BFE4FF');
            $table->string('background_color')->default('#FFFFFF');
            $table->string('text_color')->default('#111827');
            $table->string('font_family')->default('Inter');
            $table->unsignedSmallInteger('button_radius')->default(14);
            $table->unsignedSmallInteger('card_radius')->default(20);
            $table->boolean('gradient_enabled')->default(true);
            $table->boolean('animations_enabled')->default(true);
            $table->timestamps();
        });

        Schema::create('role_permissions', function (Blueprint $table): void {
            $table->id();
            $table->string('role');
            $table->string('permission_name');
            $table->boolean('allowed')->default(false);
            $table->timestamps();
            $table->unique(['role', 'permission_name']);
        });

        Schema::create('notification_settings', function (Blueprint $table): void {
            $table->id();
            $table->string('notification_type');
            $table->boolean('dashboard_enabled')->default(true);
            $table->boolean('email_enabled')->default(false);
            $table->boolean('whatsapp_enabled')->default(false);
            $table->string('reminder_time')->default('same_day');
            $table->timestamps();
            $table->unique('notification_type');
        });

        Schema::create('security_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('ip_address', 45)->nullable();
            $table->string('device')->nullable();
            $table->string('location')->nullable();
            $table->timestamp('login_time')->nullable();
            $table->string('status')->default('success');
            $table->timestamps();
        });

        Schema::create('system_settings', function (Blueprint $table): void {
            $table->id();
            $table->string('currency')->default('PKR');
            $table->string('date_format')->default('M j, Y');
            $table->string('timezone')->default('Asia/Karachi');
            $table->unsignedSmallInteger('default_task_deadline')->default(3);
            $table->boolean('auto_assign_designer')->default(false);
            $table->boolean('auto_approve_payment')->default(false);
            $table->unsignedInteger('file_upload_limit')->default(10);
            $table->string('allowed_file_types')->default('jpg,jpeg,png,pdf,doc,docx');
            $table->boolean('maintenance_mode')->default(false);
            $table->timestamps();
        });

        Schema::create('backup_exports', function (Blueprint $table): void {
            $table->id();
            $table->string('file_name');
            $table->string('file_type');
            $table->string('file_url');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->boolean('auto_backup')->default(false);
            $table->string('backup_frequency')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('backup_exports');
        Schema::dropIfExists('system_settings');
        Schema::dropIfExists('security_logs');
        Schema::dropIfExists('notification_settings');
        Schema::dropIfExists('role_permissions');
        Schema::dropIfExists('brand_settings');
        Schema::dropIfExists('website_settings');

        Schema::table('payment_methods', function (Blueprint $table): void {
            $table->dropColumn(['method_name', 'bank_name', 'qr_code']);
        });

        Schema::table('users', function (Blueprint $table): void {
            $table->dropColumn(['profile_image', 'bio']);
        });
    }
};
