<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Billing\Enums\BillPayStatusEnum;
use Modules\Billing\Enums\BillStatusEnum;
use Modules\Core\Enums\AmountTypeEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('billings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('fiscal_year_id')->constrained('fiscal_years');
            $table->foreignId('accounting_period_id')->constrained('accounting_periods');
            $table->string('billing_no')->unique();
            $table->foreignId('patient_id')->nullable()->constrained('patients');
            $table->date('billing_date');

            $table->decimal('subtotal', 20, 2)->default(0);
            $table->decimal('total_discount', 20, 2)->default(0);
            $table->decimal('total_subsidy', 20, 2)->default(0);
            $table->decimal('taxable_amount', 20, 2)->default(0);
            $table->decimal('total_vat', 20, 2)->default(0);
            $table->decimal('rounding_adjustment', 20, 2)->default(0);
            $table->decimal('grand_total', 20, 2)->default(0);

            $table->decimal('paid_amount', 20, 2)->default(0);
            $table->decimal('due_amount', 20, 2)->default(0);
            $table->enum('payment_status', BillPayStatusEnum::casesAsArray())->default(BillPayStatusEnum::UNPAID->value);
            $table->enum('status', BillStatusEnum::casesAsArray())->default(BillStatusEnum::DRAFT->value);
            // Have to add delivery status -- reserved/delivered
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index([
                'billing_date',
                'patient_id'
            ]);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('billings');
    }
};
