<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubscriptionPurchasesTable extends Migration
{
    public function up()
    {
        Schema::create('subscription_purchases', function (Blueprint $table) {
            $table->id();
            $table->string('user_id'); // Assuming user_id is a VARCHAR as requested
            $table->unsignedBigInteger('subscription_plan_id');
            $table->timestamp('purchase_date')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->date('valid_until')->nullable();
            $table->string('transaction_id')->nullable();
            $table->timestamps();

            // Foreign key constraints
            $table->foreign('subscription_plan_id')->references('id')->on('subscription_plans')->onDelete('cascade');
             $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('subscription_purchases');
    }
}
