<?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
    {
        // Add fields to packages table
        Schema::table('packages', function (Blueprint $table) {
            if (!Schema::hasColumn('packages', 'base_price')) {
                $table->decimal('base_price', 10, 2)->default(0)->after('price');
            }
            if (!Schema::hasColumn('packages', 'price_per_driver')) {
                $table->decimal('price_per_driver', 10, 2)->default(0)->after('base_price');
            }
            if (!Schema::hasColumn('packages', 'billing_cycle')) {
                $table->string('billing_cycle')->default('monthly')->after('price_per_driver'); // monthly, weekly
            }
        });

        // Add fields to subscriptions table
        Schema::table('subscriptions', function (Blueprint $table) {
            if (!Schema::hasColumn('subscriptions', 'stripe_customer_id')) {
                $table->string('stripe_customer_id')->nullable()->after('user_id');
            }
            if (!Schema::hasColumn('subscriptions', 'stripe_subscription_id')) {
                $table->string('stripe_subscription_id')->nullable()->after('stripe_customer_id');
            }
            if (!Schema::hasColumn('subscriptions', 'next_billing_date')) {
                $table->dateTime('next_billing_date')->nullable()->after('ends_at');
            }
            if (!Schema::hasColumn('subscriptions', 'billing_cycle')) {
                $table->string('billing_cycle')->default('monthly')->after('next_billing_date');
            }
            if (!Schema::hasColumn('subscriptions', 'last_driver_count')) {
                $table->integer('last_driver_count')->default(0)->after('billing_cycle');
            }
        });

        // Add fields to payments table
        Schema::table('payments', function (Blueprint $table) {
            if (!Schema::hasColumn('payments', 'invoice_amount')) {
                $table->decimal('invoice_amount', 10, 2)->nullable()->after('amount');
            }
            if (!Schema::hasColumn('payments', 'driver_count')) {
                $table->integer('driver_count')->nullable()->after('invoice_amount');
            }
            if (!Schema::hasColumn('payments', 'billing_period_start')) {
                $table->dateTime('billing_period_start')->nullable()->after('driver_count');
            }
            if (!Schema::hasColumn('payments', 'billing_period_end')) {
                $table->dateTime('billing_period_end')->nullable()->after('billing_period_start');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('payments', function (Blueprint $table) {
            $table->dropColumnIfExists('billing_period_end');
            $table->dropColumnIfExists('billing_period_start');
            $table->dropColumnIfExists('driver_count');
            $table->dropColumnIfExists('invoice_amount');
        });

        Schema::table('subscriptions', function (Blueprint $table) {
            $table->dropColumnIfExists('last_driver_count');
            $table->dropColumnIfExists('billing_cycle');
            $table->dropColumnIfExists('next_billing_date');
            $table->dropColumnIfExists('stripe_subscription_id');
            $table->dropColumnIfExists('stripe_customer_id');
        });

        Schema::table('packages', function (Blueprint $table) {
            $table->dropColumnIfExists('billing_cycle');
            $table->dropColumnIfExists('price_per_driver');
            $table->dropColumnIfExists('base_price');
        });
    }
};
