<?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::create('subscriptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->unique()->constrained()->restrictOnDelete();
            $table->foreignId('plan_id')->constrained()->restrictOnDelete();
            $table->enum('status', [
                'trialing', 'active', 'past_due', 'grace', 'suspended', 'cancelled',
            ])->default('trialing');
            $table->timestamp('starts_at');
            $table->timestamp('current_period_ends_at')->index();
            $table->timestamp('grace_ends_at')->nullable();
            $table->enum('gateway', ['paddle', 'zaincash', 'superqi'])->nullable();
            $table->string('paddle_subscription_id', 190)->nullable();
            $table->json('expiry_notified')->nullable(); // {d7, d3, h24}
            $table->timestamps();
            $table->index(['status', 'current_period_ends_at']);
        });
    }

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