<?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 $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('plan_id')->nullable()->constrained()->nullOnDelete();

            $t->enum('period', ['monthly', 'yearly'])->default('monthly');
            $t->decimal('amount', 10, 2);
            $t->string('currency', 3)->default('MDL');

            $t->enum('status', ['pending', 'paid', 'overdue', 'cancelled', 'refunded'])
                ->default('pending');

            $t->date('period_start');
            $t->date('period_end');
            $t->timestamp('paid_at')->nullable();
            $t->timestamp('due_at')->nullable();

            $t->string('invoice_number', 30)->nullable();
            $t->string('payment_method', 30)->nullable(); // card, bank_transfer, cash
            $t->string('reference', 100)->nullable();      // Stripe id, transfer ref
            $t->text('notes')->nullable();

            $t->timestamps();

            $t->index(['company_id', 'status']);
            $t->index('due_at');
            $t->index('paid_at');
        });
    }

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