<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Company Settings
        Schema::create('company_settings', function (Blueprint $table) {
            $table->id();
            $table->string('company_name');
            $table->string('company_email')->nullable();
            $table->string('company_phone')->nullable();
            $table->text('company_address')->nullable();
            $table->string('company_city')->nullable();
            $table->string('company_state')->nullable();
            $table->string('company_zip')->nullable();
            $table->string('company_country')->nullable();
            $table->string('company_tax_id')->nullable();
            $table->string('company_registration_number')->nullable();
            $table->string('company_logo')->nullable();
            $table->text('company_notes')->nullable();
            $table->timestamps();
        });

        // Tax Rates
        Schema::create('tax_rates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->decimal('rate', 5, 2); // e.g., 20.00 for 20%
            $table->enum('type', ['percentage', 'fixed'])->default('percentage');
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // Tax Groups
        Schema::create('tax_groups', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('code')->unique();
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // Tax Group Rates (Many-to-Many)
        Schema::create('tax_group_rates', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tax_group_id')->constrained('tax_groups')->onDelete('cascade');
            $table->foreignId('tax_rate_id')->constrained('tax_rates')->onDelete('cascade');
            $table->integer('sort_order')->default(0);
            $table->timestamps();
        });

        // Currencies
        Schema::create('currencies', function (Blueprint $table) {
            $table->id();
            $table->string('code', 3)->unique(); // USD, EUR, etc.
            $table->string('name'); // US Dollar
            $table->string('symbol'); // $
            $table->string('symbol_position', 10)->default('before'); // before or after
            $table->integer('decimal_places')->default(2);
            $table->decimal('exchange_rate', 15, 6)->default(1.000000);
            $table->boolean('is_default')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        // Bank Accounts
        Schema::create('bank_accounts', function (Blueprint $table) {
            $table->id();
            $table->string('account_name');
            $table->string('account_number');
            $table->string('bank_name');
            $table->string('bank_code')->nullable();
            $table->string('swift_code')->nullable();
            $table->string('iban')->nullable();
            $table->string('branch_name')->nullable();
            $table->string('branch_address')->nullable();
            $table->string('currency', 3)->default('USD');
            $table->decimal('opening_balance', 15, 2)->default(0);
            $table->decimal('current_balance', 15, 2)->default(0);
            $table->text('notes')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // Invoice Numbering Settings
        Schema::create('invoice_numbering_settings', function (Blueprint $table) {
            $table->id();
            $table->string('prefix')->default('INV');
            $table->integer('next_number')->default(1);
            $table->integer('number_length')->default(6); // Zero padding length
            $table->string('format')->default('{prefix}-{number}'); // INV-000001
            $table->boolean('reset_yearly')->default(false);
            $table->timestamps();
        });

        // Email Templates
        Schema::create('email_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('subject');
            $table->text('body');
            $table->text('variables')->nullable(); // JSON: available variables
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // SMS Templates
        Schema::create('sms_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('message');
            $table->text('variables')->nullable(); // JSON: available variables
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // WhatsApp Templates
        Schema::create('whatsapp_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('message');
            $table->text('variables')->nullable(); // JSON: available variables
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // Email Configuration
        Schema::create('email_configurations', function (Blueprint $table) {
            $table->id();
            $table->string('mailer')->default('smtp');
            $table->string('host')->nullable();
            $table->integer('port')->nullable();
            $table->string('username')->nullable();
            $table->string('password')->nullable();
            $table->string('encryption')->nullable(); // tls, ssl
            $table->string('from_address');
            $table->string('from_name');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // SMS Configuration
        Schema::create('sms_configurations', function (Blueprint $table) {
            $table->id();
            $table->string('provider')->default('twilio'); // twilio, nexmo, etc.
            $table->string('api_key')->nullable();
            $table->string('api_secret')->nullable();
            $table->string('sender_id')->nullable();
            $table->text('config')->nullable(); // JSON: provider-specific config
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // WhatsApp Configuration
        Schema::create('whatsapp_configurations', function (Blueprint $table) {
            $table->id();
            $table->string('provider')->default('twilio'); // twilio, whatsapp business api, etc.
            $table->string('api_key')->nullable();
            $table->string('api_secret')->nullable();
            $table->string('phone_number_id')->nullable();
            $table->string('business_account_id')->nullable();
            $table->text('config')->nullable(); // JSON: provider-specific config
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // Signatures
        Schema::create('signatures', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('type')->default('image'); // image, text
            $table->text('content')->nullable(); // Image path or text content
            $table->text('notes')->nullable();
            $table->boolean('is_default')->default(false);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // API Keys
        Schema::create('api_keys', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('key', 64)->unique();
            $table->text('permissions')->nullable(); // JSON: allowed permissions
            $table->timestamp('last_used_at')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        // Quotation Settings
        Schema::create('quotation_settings', function (Blueprint $table) {
            $table->id();
            $table->string('prefix')->default('QUO');
            $table->integer('next_number')->default(1);
            $table->integer('number_length')->default(6);
            $table->string('format')->default('{prefix}-{number}');
            $table->integer('validity_days')->default(30);
            $table->text('terms')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        // Expense Settings
        Schema::create('expense_settings', function (Blueprint $table) {
            $table->id();
            $table->boolean('require_approval')->default(false);
            $table->decimal('approval_threshold', 15, 2)->nullable();
            $table->text('default_categories')->nullable(); // JSON
            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('expense_settings');
        Schema::dropIfExists('quotation_settings');
        Schema::dropIfExists('api_keys');
        Schema::dropIfExists('signatures');
        Schema::dropIfExists('whatsapp_configurations');
        Schema::dropIfExists('sms_configurations');
        Schema::dropIfExists('email_configurations');
        Schema::dropIfExists('whatsapp_templates');
        Schema::dropIfExists('sms_templates');
        Schema::dropIfExists('email_templates');
        Schema::dropIfExists('invoice_numbering_settings');
        Schema::dropIfExists('bank_accounts');
        Schema::dropIfExists('currencies');
        Schema::dropIfExists('tax_group_rates');
        Schema::dropIfExists('tax_groups');
        Schema::dropIfExists('tax_rates');
        Schema::dropIfExists('company_settings');
    }
};
