<?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('line_billing_charges', function (Blueprint $table) {
            $table->id();
            $table->foreignId('line_id')->constrained('lines')->restrictOnDelete();
            $table->foreignId('billing_period_id')->constrained('billing_periods')->restrictOnDelete();
            $table->foreignId('invoice_id')->nullable()->constrained('invoices')->nullOnDelete();
            $table->foreignId('plan_id')->nullable()->constrained('plans')->nullOnDelete();
            $table->decimal('amount', 15, 2);
            $table->string('status', 20)->default('unpaid')->index();
            $table->timestamps();

            $table->unique(['line_id', 'billing_period_id'], 'line_period_unique');
        });
    }

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