<?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
    {
        Schema::create('subscriptions', function (Blueprint $table) {
            $table->id();

            $table->unsignedBigInteger('subscription_product_xid');
            $table->unsignedBigInteger('iam_principal_xid');

            $table->foreign('subscription_product_xid')->references('id')->on('subscription_products')->onDelete('cascade');
            $table->foreign('iam_principal_xid')->references('id')->on('iam_principal')->onDelete('cascade');
            
            $table->string('amount')->nullable();
            $table->string('stripe_customer_id')->nullable();
            $table->string('payment_intent_id')->nullable();
            $table->string('payment_intent_client_secret')->nullable();
            $table->string('subscription_id')->nullable();
            $table->string('subscription_status')->nullable();
            $table->dateTime('current_period_start')->nullable();
            $table->dateTime('current_period_end')->nullable();
            $table->string('status')->nullable();
            $table->dateTime('next_payment_date')->nullable();
            $table->boolean('is_cancelled_subscription')->default(0)->comment('1=Cancelled, 0=Not Cancelled');
            $table->dateTime('cancelled_at')->nullable();

            $table->boolean('is_active')->default(1)->comment('1=Active, 0=Expired');
            $table->softDeletes();
            $table->integer('created_by')->nullable();
            $table->integer('modified_by')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('subscriptions');
    }
};
