<?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('product_categories', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->foreignId('parent_id')->nullable()->constrained('product_categories')->nullOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
        });

        Schema::create('tax_rules', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->string('hsn_code')->nullable();
            $table->decimal('percent', 6, 2)->default(0);
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
        });

        Schema::table('products', function (Blueprint $table): void {
            $table->foreignId('category_id')->nullable()->after('id')->constrained('product_categories')->nullOnDelete();
            $table->foreignId('subcategory_id')->nullable()->after('category_id')->constrained('product_categories')->nullOnDelete();
            $table->boolean('is_digital')->default(false)->after('image_path');
            $table->foreignId('tax_rule_id')->nullable()->after('is_digital')->constrained('tax_rules')->nullOnDelete();
        });

        Schema::create('product_images', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->string('path');
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('product_discounts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->string('rule_type', 50);
            $table->decimal('value', 12, 2)->default(0);
            $table->unsignedInteger('min_qty')->nullable();
            $table->unsignedInteger('max_qty')->nullable();
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
        });

        Schema::create('product_combos', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->decimal('price', 12, 2)->default(0);
            $table->decimal('bv', 12, 4)->default(0);
            $table->json('items_json')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
        });

        Schema::create('shipping_rules', function (Blueprint $table): void {
            $table->id();
            $table->string('region');
            $table->decimal('min_weight', 12, 3)->default(0);
            $table->decimal('max_weight', 12, 3)->default(0);
            $table->decimal('charge', 12, 2)->default(0);
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
        });

        Schema::create('product_ratings', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->unsignedTinyInteger('rating')->default(5);
            $table->text('review')->nullable();
            $table->string('status', 20)->default('pending')->index();
            $table->timestamps();
        });

        Schema::create('order_status_histories', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
            $table->string('status', 30)->index();
            $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('payment_gateways', function (Blueprint $table): void {
            $table->id();
            $table->string('code', 50)->unique();
            $table->string('name');
            $table->boolean('is_enabled')->default(false)->index();
            $table->json('config_json')->nullable();
            $table->timestamps();
        });

        Schema::create('api_keys', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->string('key_hash')->unique();
            $table->boolean('is_active')->default(true)->index();
            $table->timestamp('last_used_at')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('api_key_ips', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('api_key_id')->constrained('api_keys')->cascadeOnDelete();
            $table->string('ip_address', 45);
            $table->timestamps();
            $table->unique(['api_key_id', 'ip_address']);
        });

        Schema::create('api_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('api_key_id')->nullable()->constrained('api_keys')->nullOnDelete();
            $table->string('endpoint');
            $table->string('method', 10)->default('GET');
            $table->unsignedSmallInteger('status')->default(200);
            $table->string('ip', 45)->nullable();
            $table->unsignedInteger('response_time_ms')->default(0);
            $table->timestamps();
        });

        Schema::create('user_rank_overrides', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('rank_id')->nullable()->constrained('ranks')->nullOnDelete();
            $table->foreignId('set_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('reason')->nullable();
            $table->boolean('active')->default(true)->index();
            $table->timestamps();
        });

        Schema::create('user_transaction_pins', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained('users')->cascadeOnDelete();
            $table->string('pin_hash');
            $table->timestamps();
        });

        Schema::create('admin_batch_jobs', function (Blueprint $table): void {
            $table->id();
            $table->string('type', 50)->index();
            $table->string('status', 30)->default('pending')->index();
            $table->string('file_path');
            $table->string('error_report_path')->nullable();
            $table->unsignedInteger('total_rows')->default(0);
            $table->unsignedInteger('processed_rows')->default(0);
            $table->unsignedInteger('failed_rows')->default(0);
            $table->json('meta')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();
        });

        Schema::create('bv_adjustments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->enum('bv_type', ['joining', 'repurchase'])->index();
            $table->decimal('units', 14, 4);
            $table->string('status', 30)->default('pending')->index();
            $table->text('reason')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });

        Schema::table('withdrawals', function (Blueprint $table): void {
            $table->string('payment_reference', 100)->nullable()->after('rejection_reason');
            $table->string('payment_proof_path')->nullable()->after('payment_reference');
        });
    }

    public function down(): void
    {
        Schema::table('withdrawals', function (Blueprint $table): void {
            $table->dropColumn(['payment_reference', 'payment_proof_path']);
        });

        Schema::dropIfExists('bv_adjustments');
        Schema::dropIfExists('admin_batch_jobs');
        Schema::dropIfExists('user_transaction_pins');
        Schema::dropIfExists('user_rank_overrides');
        Schema::dropIfExists('api_logs');
        Schema::dropIfExists('api_key_ips');
        Schema::dropIfExists('api_keys');
        Schema::dropIfExists('payment_gateways');
        Schema::dropIfExists('order_status_histories');
        Schema::dropIfExists('product_ratings');
        Schema::dropIfExists('product_images');
        Schema::dropIfExists('product_discounts');
        Schema::dropIfExists('product_combos');
        Schema::dropIfExists('shipping_rules');
        Schema::table('products', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('category_id');
            $table->dropConstrainedForeignId('subcategory_id');
            $table->dropConstrainedForeignId('tax_rule_id');
            $table->dropColumn('is_digital');
        });
        Schema::dropIfExists('tax_rules');
        Schema::dropIfExists('product_categories');
    }
};
