<?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->uuid('uuid')->unique();
            $table->unsignedBigInteger('brand_id')->nullable();
            $table->enum('type', ['individual', 'company'])->default('individual');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('company_name')->nullable();
            // PRC-842 — e-posta tekilliği brand-boyutlu: aynı e-posta farklı
            // brand'lerde ayrı hesap açabilir. ⚠️ MySQL `UNIQUE(email, brand_id)`
            // çoklu NULL brand_id satırına izin verirdi (ana site müşterileri
            // çiftlenebilirdi) → COALESCE'li stored generated column ile çözülür.
            $table->string('email');
            $table->unsignedBigInteger('brand_scope')->storedAs('COALESCE(brand_id, 0)');
            $table->unique(['email', 'brand_scope'], 'clients_email_brand_scope_unique');
            $table->string('phone')->nullable();
            $table->string('password');
            $table->text('totp_secret')->nullable();
            $table->boolean('totp_enabled')->default(false);
            $table->text('totp_backup_codes')->nullable();
            $table->timestamp('totp_verified_at')->nullable();
            $table->foreignId('language_id')->nullable();
            $table->enum('status', ['active', 'inactive', 'suspended'])->default('active');
            $table->boolean('consent_marketing')->nullable();
            $table->timestamp('consent_updated_at')->nullable();
            $table->foreignId('group_id')->nullable();
            $table->unsignedBigInteger('reseller_id')->nullable();

            // Verification
            $table->string('verification_status', 20)->default('unverified');
            $table->text('individual_id_number')->nullable();
            $table->text('company_id_number')->nullable();

            // Contact information
            $table->text('address')->nullable();
            $table->string('address1')->nullable();
            $table->string('address2')->nullable();
            $table->string('postcode', 20)->nullable();
            $table->string('tax_id', 50)->nullable();
            $table->foreignId('country_id')->nullable();
            $table->foreignId('city_id')->nullable();
            $table->string('state')->nullable();

            // Billing
            $table->foreignId('currency_id')->nullable();
            $table->enum('payment_method', ['credit_card', 'bank_transfer', 'paypal'])->nullable();
            $table->string('billing_contact')->nullable();

            // Email notifications
            $table->boolean('general_emails')->default(true);
            $table->boolean('invoice_emails')->default(true);
            $table->boolean('support_emails')->default(true);
            $table->boolean('product_emails')->default(true);
            $table->boolean('domain_emails')->default(true);
            $table->boolean('affiliate_emails')->default(true);

            // Settings
            $table->boolean('late_fees')->default(false);
            $table->boolean('separate_invoices')->default(false);
            $table->boolean('overdue_notices')->default(true);
            $table->boolean('disable_cc_processing')->default(false);
            $table->boolean('allow_sso')->default(true);
            $table->boolean('tax_exempt')->nullable()->default(null);
            $table->boolean('marketing_emails')->default(false);
            $table->text('notes')->nullable();
            $table->decimal('late_fee_override', 5, 2)->nullable();

            // Authentication and verification timestamps
            $table->timestamp('email_verified_at')->nullable();
            $table->timestamp('phone_verified_at')->nullable();
            $table->timestamp('tos_accepted_at')->nullable();
            $table->timestamp('registered_at')->nullable();

            // Login tracking
            $table->timestamp('last_login_at')->nullable();
            $table->ipAddress('last_login_ip')->nullable();
            $table->string('last_login_user_agent', 500)->nullable();
            $table->string('last_login_country', 2)->nullable();
            $table->string('last_login_city', 100)->nullable();
            $table->integer('login_count')->default(0);
            $table->timestamp('previous_login_at')->nullable();
            $table->ipAddress('previous_login_ip')->nullable();

            // Email verification
            $table->string('email_verification_token', 60)->nullable();
            $table->string('email_verification_code', 6)->nullable();
            $table->timestamp('email_verification_expires_at')->nullable();

            // Phone verification
            $table->string('phone_verification_code', 6)->nullable();
            $table->timestamp('phone_verification_expires_at')->nullable();

            // Password reset
            $table->string('password_reset_key', 60)->nullable();
            $table->timestamp('password_reset_expiry')->nullable();

            // Social authentication
            $table->string('social_provider', 50)->nullable();
            $table->string('social_provider_id')->nullable();

            // System
            $table->boolean('is_deleted')->default(false);
            $table->decimal('balance', 10, 2)->default(0);
            $table->string('remember_token')->nullable();

            $table->timestamps();

            $table->index('group_id');
            $table->index('status');
            $table->index('country_id');
            $table->index('reseller_id');
            $table->index('brand_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('clients');
    }
};
