<?php

declare(strict_types=1);

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('invoices', function (Blueprint $table): void {
            $table->id();
            $table->string('type', 32);
            $table->string('status', 32);
            $table->string('issued_via', 32)->nullable();
            $table->string('document_number', 64)->nullable()->index();
            $table->unsignedSmallInteger('fiscal_year')->nullable();
            $table->unsignedInteger('sequence_value')->nullable();
            $table->foreignId('customer_id')->constrained('customers')->restrictOnDelete();
            $table->foreignId('credited_invoice_id')->nullable()->constrained('invoices')->nullOnDelete();
            $table->date('invoice_date');
            $table->date('expiration_date');
            $table->text('description')->nullable();
            $table->text('email_body')->nullable();
            $table->decimal('subtotal_ex_vat', 15, 2)->default(0);
            $table->decimal('total_vat', 15, 2)->default(0);
            $table->decimal('total_inc_vat', 15, 2)->default(0);
            $table->timestamp('finalized_at')->nullable();
            $table->timestamp('email_sent_at')->nullable();
            $table->timestamps();
        });
    }

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