<?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
    {
        if (! Schema::hasColumn('packages', 'paddle_product_id')) {
            Schema::table('packages', function (Blueprint $table) {
                $table->string('paddle_product_id')->nullable()->after('price');
                $table->string('paddle_price_id')->nullable()->after('paddle_product_id');
                $table->index('paddle_product_id');
                $table->index('paddle_price_id');
            });
        }

        if (! Schema::hasColumn('tenants', 'paddle_customer_id')) {
            Schema::table('tenants', function (Blueprint $table) {
                $table->string('paddle_customer_id')->nullable()->after('subscription_status');
                $table->index('paddle_customer_id');
            });
        }

        if (! Schema::hasColumn('tenant_packages', 'paddle_subscription_id')) {
            Schema::table('tenant_packages', function (Blueprint $table) {
                $table->string('paddle_subscription_id')->nullable()->after('package_id');
                $table->index('paddle_subscription_id');
            });
        }

        if (! Schema::hasColumn('package_purchases', 'provider')) {
            Schema::table('package_purchases', function (Blueprint $table) {
                $table->string('provider')->nullable()->after('package_id');
                $table->index('provider');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasColumn('packages', 'paddle_price_id')) {
            Schema::table('packages', function (Blueprint $table) {
                $table->dropIndex('packages_paddle_price_id_index');
                $table->dropColumn('paddle_price_id');
            });
        }

        if (Schema::hasColumn('packages', 'paddle_product_id')) {
            Schema::table('packages', function (Blueprint $table) {
                $table->dropIndex('packages_paddle_product_id_index');
                $table->dropColumn('paddle_product_id');
            });
        }

        if (Schema::hasColumn('tenants', 'paddle_customer_id')) {
            Schema::table('tenants', function (Blueprint $table) {
                $table->dropIndex('tenants_paddle_customer_id_index');
                $table->dropColumn('paddle_customer_id');
            });
        }

        if (Schema::hasColumn('tenant_packages', 'paddle_subscription_id')) {
            Schema::table('tenant_packages', function (Blueprint $table) {
                $table->dropIndex('tenant_packages_paddle_subscription_id_index');
                $table->dropColumn('paddle_subscription_id');
            });
        }

        if (Schema::hasColumn('package_purchases', 'provider')) {
            Schema::table('package_purchases', function (Blueprint $table) {
                $table->dropIndex('package_purchases_provider_index');
                $table->dropColumn('provider');
            });
        }
    }
};
