<?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('care_bills', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained('care_branches')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->string('invoice_number')->unique();
            $table->string('status')->default('open'); // draft, open, partial, paid, void
            $table->unsignedInteger('subtotal_minor')->default(0);
            $table->unsignedInteger('discount_minor')->default(0);
            $table->unsignedInteger('tax_minor')->default(0);
            $table->unsignedInteger('total_minor')->default(0);
            $table->unsignedInteger('amount_paid_minor')->default(0);
            $table->unsignedInteger('balance_minor')->default(0);
            $table->text('notes')->nullable();
            $table->string('created_by')->nullable();
            $table->timestamp('finalized_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['patient_id', 'status']);
            $table->index(['branch_id', 'status']);
        });

        Schema::create('care_bill_line_items', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('bill_id')->constrained('care_bills')->cascadeOnDelete();
            $table->string('type')->default('misc'); // consultation, lab, imaging, procedure, pharmacy, nursing, misc
            $table->string('description');
            $table->unsignedSmallInteger('quantity')->default(1);
            $table->unsignedInteger('unit_price_minor')->default(0);
            $table->unsignedInteger('total_minor')->default(0);
            $table->string('source_type')->nullable();
            $table->unsignedBigInteger('source_id')->nullable();
            $table->timestamps();
        });

        Schema::create('care_payments', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('bill_id')->constrained('care_bills')->cascadeOnDelete();
            $table->unsignedInteger('amount_minor');
            $table->string('method')->default('cash'); // cash, momo, card, other
            $table->string('reference')->nullable();
            $table->timestamp('paid_at');
            $table->string('recorded_by')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('care_drugs', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->string('name');
            $table->string('generic_name')->nullable();
            $table->string('sku')->nullable();
            $table->string('unit')->default('unit');
            $table->unsignedInteger('unit_price_minor')->default(0);
            $table->unsignedInteger('reorder_level')->default(10);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('care_drug_batches', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('drug_id')->constrained('care_drugs')->cascadeOnDelete();
            $table->string('batch_number');
            $table->date('expiry_date')->nullable();
            $table->unsignedInteger('quantity_on_hand')->default(0);
            $table->unsignedInteger('cost_minor')->default(0);
            $table->timestamp('received_at')->nullable();
            $table->timestamps();
            $table->index(['drug_id', 'expiry_date']);
        });

        Schema::create('care_dispensing_records', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('prescription_id')->constrained('care_prescriptions')->cascadeOnDelete();
            $table->foreignId('prescription_item_id')->constrained('care_prescription_items')->cascadeOnDelete();
            $table->foreignId('drug_batch_id')->constrained('care_drug_batches')->cascadeOnDelete();
            $table->unsignedInteger('quantity');
            $table->string('dispensed_by')->nullable();
            $table->timestamp('dispensed_at');
            $table->timestamps();
        });

        Schema::table('care_prescription_items', function (Blueprint $table) {
            $table->foreignId('drug_id')->nullable()->after('prescription_id')->constrained('care_drugs')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('care_prescription_items', function (Blueprint $table) {
            $table->dropConstrainedForeignId('drug_id');
        });
        Schema::dropIfExists('care_dispensing_records');
        Schema::dropIfExists('care_drug_batches');
        Schema::dropIfExists('care_drugs');
        Schema::dropIfExists('care_payments');
        Schema::dropIfExists('care_bill_line_items');
        Schema::dropIfExists('care_bills');
    }
};
