<?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('subscription_plans', function (Blueprint $table) {
            if (! Schema::hasColumn('subscription_plans', 'is_custom')) {
                $table->boolean('is_custom')->default(false)->after('active');
            }
            if (! Schema::hasColumn('subscription_plans', 'discount_type')) {
                $table->string('discount_type', 20)->default('none')->after('is_custom');
            }
            if (! Schema::hasColumn('subscription_plans', 'discount_value')) {
                $table->decimal('discount_value', 12, 2)->default(0)->after('discount_type');
            }
            if (! Schema::hasColumn('subscription_plans', 'strike_price_yearly')) {
                $table->decimal('strike_price_yearly', 12, 2)->nullable()->after('discount_value');
            }
        });

        if (! Schema::hasTable('subscription_coupons')) {
            Schema::create('subscription_coupons', function (Blueprint $table) {
                $table->id();
                $table->string('code', 64)->unique();
                $table->string('name')->nullable();
                $table->string('discount_type', 20);
                $table->decimal('discount_value', 12, 2);
                $table->unsignedInteger('max_uses')->nullable();
                $table->unsignedInteger('used_count')->default(0);
                $table->unsignedInteger('max_uses_per_company')->default(1);
                $table->timestamp('valid_from')->nullable();
                $table->timestamp('valid_until')->nullable();
                $table->json('applies_to_plan_ids')->nullable();
                $table->decimal('min_amount', 12, 2)->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });
        }

        Schema::table('subscription_requests', function (Blueprint $table) {
            if (! Schema::hasColumn('subscription_requests', 'payment_path')) {
                $table->string('payment_path', 20)->default('offline')->after('billing_cycle');
            }
            if (! Schema::hasColumn('subscription_requests', 'coupon_id')) {
                $table->unsignedBigInteger('coupon_id')->nullable()->after('payment_path');
            }
            if (! Schema::hasColumn('subscription_requests', 'coupon_code')) {
                $table->string('coupon_code', 64)->nullable()->after('coupon_id');
            }
            if (! Schema::hasColumn('subscription_requests', 'subtotal_before_discount')) {
                $table->decimal('subtotal_before_discount', 12, 2)->default(0)->after('coupon_code');
            }
            if (! Schema::hasColumn('subscription_requests', 'plan_discount_amount')) {
                $table->decimal('plan_discount_amount', 12, 2)->default(0)->after('subtotal_before_discount');
            }
            if (! Schema::hasColumn('subscription_requests', 'coupon_discount_amount')) {
                $table->decimal('coupon_discount_amount', 12, 2)->default(0)->after('plan_discount_amount');
            }
            if (! Schema::hasColumn('subscription_requests', 'is_upgrade')) {
                $table->boolean('is_upgrade')->default(false)->after('coupon_discount_amount');
            }
            if (! Schema::hasColumn('subscription_requests', 'days_remaining')) {
                $table->unsignedInteger('days_remaining')->nullable()->after('is_upgrade');
            }
            if (! Schema::hasColumn('subscription_requests', 'unused_credit')) {
                $table->decimal('unused_credit', 12, 2)->nullable()->after('days_remaining');
            }
            if (! Schema::hasColumn('subscription_requests', 'upgrade_gross')) {
                $table->decimal('upgrade_gross', 12, 2)->nullable()->after('unused_credit');
            }
        });

        Schema::table('subscription_payments', function (Blueprint $table) {
            if (! Schema::hasColumn('subscription_payments', 'subscription_plan_id')) {
                $table->unsignedBigInteger('subscription_plan_id')->nullable()->after('company_id');
            }
            if (! Schema::hasColumn('subscription_payments', 'subscription_request_id')) {
                $table->unsignedBigInteger('subscription_request_id')->nullable()->after('subscription_plan_id');
            }
            if (! Schema::hasColumn('subscription_payments', 'coupon_id')) {
                $table->unsignedBigInteger('coupon_id')->nullable()->after('subscription_request_id');
            }
            if (! Schema::hasColumn('subscription_payments', 'checkout_mode')) {
                $table->string('checkout_mode', 20)->nullable()->after('coupon_id');
            }
            if (! Schema::hasColumn('subscription_payments', 'is_upgrade')) {
                $table->boolean('is_upgrade')->default(false)->after('checkout_mode');
            }
            if (! Schema::hasColumn('subscription_payments', 'plan_discount_amount')) {
                $table->decimal('plan_discount_amount', 12, 2)->default(0)->after('is_upgrade');
            }
            if (! Schema::hasColumn('subscription_payments', 'coupon_discount_amount')) {
                $table->decimal('coupon_discount_amount', 12, 2)->default(0)->after('plan_discount_amount');
            }
            if (! Schema::hasColumn('subscription_payments', 'apply_plan_on_success')) {
                $table->boolean('apply_plan_on_success')->default(false)->after('coupon_discount_amount');
            }
            if (! Schema::hasColumn('subscription_payments', 'extend_licence_on_success')) {
                $table->boolean('extend_licence_on_success')->default(false)->after('apply_plan_on_success');
            }
        });

        if (! Schema::hasTable('subscription_expiry_reminders')) {
            Schema::create('subscription_expiry_reminders', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('company_id');
                $table->string('reminder_key', 40);
                $table->timestamp('sent_at');
                $table->timestamps();

                $table->unique(['company_id', 'reminder_key'], 'sub_expiry_reminders_company_key_unique');
                $table->index('company_id', 'sub_expiry_reminders_company_idx');
            });
        }

        if (! Schema::hasTable('subscription_coupon_redemptions')) {
            Schema::create('subscription_coupon_redemptions', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('subscription_coupon_id');
                $table->unsignedBigInteger('company_id');
                $table->unsignedBigInteger('subscription_payment_id')->nullable();
                $table->unsignedBigInteger('subscription_request_id')->nullable();
                $table->timestamps();

                $table->index(['subscription_coupon_id', 'company_id'], 'sub_coupon_redemptions_coupon_company_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('subscription_coupon_redemptions');
        Schema::dropIfExists('subscription_expiry_reminders');

        Schema::table('subscription_payments', function (Blueprint $table) {
            foreach ([
                'subscription_plan_id',
                'subscription_request_id',
                'coupon_id',
                'checkout_mode',
                'is_upgrade',
                'plan_discount_amount',
                'coupon_discount_amount',
                'apply_plan_on_success',
                'extend_licence_on_success',
            ] as $column) {
                if (Schema::hasColumn('subscription_payments', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::table('subscription_requests', function (Blueprint $table) {
            foreach ([
                'payment_path',
                'coupon_id',
                'coupon_code',
                'subtotal_before_discount',
                'plan_discount_amount',
                'coupon_discount_amount',
                'is_upgrade',
                'days_remaining',
                'unused_credit',
                'upgrade_gross',
            ] as $column) {
                if (Schema::hasColumn('subscription_requests', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::dropIfExists('subscription_coupons');

        Schema::table('subscription_plans', function (Blueprint $table) {
            foreach (['is_custom', 'discount_type', 'discount_value', 'strike_price_yearly'] as $column) {
                if (Schema::hasColumn('subscription_plans', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
