<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Tabel pembayaran — menyimpan tagihan dan transaksi per sesi konsultasi.
 * FK ke dokter_pasien (satu konsultasi = satu tagihan).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('pembayaran', function (Blueprint $table) {
            $table->id();

            // Relasi ke sesi konsultasi
            $table->foreignId('dokter_pasien_id')
                  ->constrained('dokter_pasien')
                  ->onDelete('cascade')
                  ->comment('Konsultasi yang ditagihkan');

            $table->foreignId('pasien_id')
                  ->constrained('pasien')
                  ->onDelete('cascade');

            $table->foreignId('dokter_id')
                  ->constrained('dokter')
                  ->onDelete('cascade');

            // Nomor invoice unik
            $table->string('nomor_invoice', 30)->unique()->comment('INV-YYYYMMDD-XXXX');

            // Rincian biaya
            $table->decimal('biaya_konsultasi', 12, 2)->default(0);
            $table->decimal('biaya_tambahan', 12, 2)->default(0);
            $table->string('keterangan_biaya_tambahan', 255)->nullable();
            $table->decimal('diskon', 12, 2)->default(0);
            $table->decimal('total_bayar', 12, 2)->default(0);

            // Metode & status
            $table->enum('metode_pembayaran', ['QRIS', 'TUNAI'])->default('TUNAI');
            $table->enum('status_pembayaran', ['MENUNGGU', 'LUNAS', 'BATAL'])->default('MENUNGGU');

            // QRIS
            $table->string('qris_kode_unik', 100)->nullable()->comment('String yang diencode jadi QR');
            $table->string('qris_foto_bukti')->nullable()->comment('Path foto bukti bayar QRIS');
            $table->timestamp('qris_waktu_upload_bukti')->nullable();

            // Tunai
            $table->decimal('tunai_diterima', 12, 2)->nullable();
            $table->decimal('tunai_kembalian', 12, 2)->nullable();

            // Konfirmasi admin
            $table->foreignId('dikonfirmasi_oleh')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete();
            $table->timestamp('waktu_konfirmasi')->nullable();
            $table->string('catatan_admin', 255)->nullable();

            // Kuitansi
            $table->boolean('kuitansi_dicetak')->default(false);
            $table->timestamp('waktu_cetak_kuitansi')->nullable();

            // Metadata
            $table->foreignId('dibuat_oleh')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete();

            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('nomor_invoice');
            $table->index('status_pembayaran');
            $table->index('metode_pembayaran');
            $table->index('dokter_pasien_id');
            $table->index('pasien_id');
            $table->index(['status_pembayaran', 'waktu_konfirmasi']);
        });
    }

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