<?php

declare(strict_types=1);

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

/**
 * Add branding and general settings columns to companies table.
 *
 * These fields allow Company Admins to customize their company's
 * appearance and basic operational settings.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('companies', function (Blueprint $table) {
            // ═══════════════════════════════════════════════════════════════════════
            // BRANDING
            // ═══════════════════════════════════════════════════════════════════════
            $table->string('logo_path')->nullable()->after('name')
                ->comment('Path to company logo image');

            $table->string('favicon_path')->nullable()->after('logo_path')
                ->comment('Path to company favicon');

            $table->string('primary_color', 7)->default('#3b82f6')->after('favicon_path')
                ->comment('Primary brand color (hex)');

            $table->string('secondary_color', 7)->default('#64748b')->after('primary_color')
                ->comment('Secondary brand color (hex)');

            // ═══════════════════════════════════════════════════════════════════════
            // BUSINESS INFORMATION
            // ═══════════════════════════════════════════════════════════════════════
            $table->string('business_name')->nullable()->after('name')
                ->comment('Legal/commercial business name for documents');

            $table->string('tax_id', 50)->nullable()->after('business_name')
                ->comment('Tax identification number (NIT, RUC, etc.)');

            $table->string('legal_representative')->nullable()->after('tax_id')
                ->comment('Legal representative name');

            // ═══════════════════════════════════════════════════════════════════════
            // LOCALIZATION
            // ═══════════════════════════════════════════════════════════════════════
            $table->string('timezone', 50)->default('America/Bogota')->after('address')
                ->comment('Company timezone for date calculations');

            $table->string('date_format', 20)->default('d/m/Y')->after('timezone')
                ->comment('Date display format');

            $table->string('time_format', 10)->default('H:i')->after('date_format')
                ->comment('Time display format');

            $table->string('locale', 10)->default('es_CO')->after('time_format')
                ->comment('Locale for number/currency formatting');

            // ═══════════════════════════════════════════════════════════════════════
            // STATUS
            // ═══════════════════════════════════════════════════════════════════════
            $table->enum('status', ['active', 'suspended', 'inactive'])
                ->default('active')->after('locale')
                ->comment('Company account status');

            $table->timestamp('suspended_at')->nullable()->after('status')
                ->comment('When the company was suspended');

            $table->string('suspension_reason')->nullable()->after('suspended_at')
                ->comment('Reason for suspension');

            // ═══════════════════════════════════════════════════════════════════════
            // INDEXES
            // ═══════════════════════════════════════════════════════════════════════
            $table->index('status');
        });
    }

    public function down(): void
    {
        Schema::table('companies', function (Blueprint $table) {
            $table->dropIndex(['status']);

            $table->dropColumn([
                'logo_path',
                'favicon_path',
                'primary_color',
                'secondary_color',
                'business_name',
                'tax_id',
                'legal_representative',
                'timezone',
                'date_format',
                'time_format',
                'locale',
                'status',
                'suspended_at',
                'suspension_reason',
            ]);
        });
    }
};
