<?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('b2b_account_orders', function (Blueprint $table) {
            $table->id();
            $table->integer('order_id')->unique();
            $table->unsignedBigInteger('trade_customer_id');
            $table->string('po_number', 20)->unique();
            $table->decimal('amount_due', 10, 2);
            $table->enum('status', ['unpaid', 'paid'])->default('unpaid');
            $table->timestamp('paid_at')->nullable();
            $table->string('paid_by', 50)->nullable();
            $table->string('payment_reference', 255)->nullable();
            $table->unsignedInteger('marked_paid_by')->nullable();
            $table->timestamp('last_reminder_sent_at')->nullable();
            $table->timestamps();

            $table->foreign('order_id')->references('id')->on('order')->onDelete('cascade');
            $table->foreign('trade_customer_id')->references('id')->on('b2b_trade_customers')->onDelete('cascade');
            $table->index(['trade_customer_id', 'status']);
            $table->index('status');
        });
    }

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