<?php

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

/**
 * SQLite-compatible schema untuk testing.
 * Menggabungkan semua tabel dalam satu migration.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('nip')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('phone')->nullable();
            $table->string('gender')->nullable();
            $table->string('address')->nullable();
            $table->string('division')->nullable();
            $table->string('position')->nullable();
            $table->string('profile_photo')->nullable();
            $table->string('role')->nullable()->default(null);
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('kategoris', function (Blueprint $table) {
            $table->id();
            $table->string('nama')->unique();
            $table->string('keterangan')->nullable();
            $table->timestamps();
        });

        Schema::create('suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->string('alamat')->nullable();
            $table->string('telepon')->nullable();
            $table->string('email')->nullable();
            $table->timestamps();
        });

        Schema::create('satuans', function (Blueprint $table) {
            $table->id();
            $table->string('nama')->unique();
            $table->string('keterangan')->nullable();
            $table->timestamps();
        });

        Schema::create('obats', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->foreignId('kategori_id')->nullable()->constrained('kategoris')->onDelete('set null');
            $table->timestamps();
        });

        Schema::create('obat_masuks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('obat_id')->nullable()->constrained('obats')->onDelete('cascade');
            $table->foreignId('kategori_id')->nullable()->constrained('kategoris')->onDelete('set null');
            $table->foreignId('satuan_id')->nullable()->constrained('satuans')->onDelete('set null');
            $table->foreignId('supplier_id')->nullable()->constrained('suppliers')->onDelete('set null');
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('nama_obat')->nullable();
            $table->string('kode_batch');
            $table->string('barcode')->nullable();
            $table->integer('stok')->default(0);
            $table->decimal('harga_beli', 12, 2)->nullable();
            $table->decimal('harga_jual', 12, 2)->nullable();
            $table->string('sumber_dana')->nullable();
            $table->string('no_sbbk')->nullable();
            $table->date('tanggal_penerimaan');
            $table->date('tanggal_kadaluarsa');
            $table->string('no_faktur')->nullable();
            $table->string('nama_supplier')->nullable();
            $table->text('catatan')->nullable();
            $table->timestamps();
        });

        Schema::create('obat_keluars', function (Blueprint $table) {
            $table->id();
            $table->foreignId('obat_masuk_id')->nullable()->constrained('obat_masuks')->onDelete('set null');
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('nama_obat')->nullable();
            $table->string('kode_batch')->nullable();
            $table->string('barcode')->nullable();
            $table->integer('jumlah');
            $table->decimal('harga', 15, 2)->nullable();
            $table->decimal('harga_total', 15, 2)->nullable();
            $table->string('tujuan_pemakaian')->nullable();
            $table->string('sumber_dana')->nullable();
            $table->date('tanggal_pengeluaran');
            $table->date('tanggal_kadaluarsa')->nullable();
            $table->string('no_pengeluaran')->nullable();
            $table->string('nama_petugas')->nullable();
            $table->string('nama_penerima')->nullable();
            $table->text('catatan')->nullable();
            // SQLite tidak support ENUM, gunakan string
            $table->string('status')->default('proses');
            $table->timestamps();
        });

        Schema::create('reseps', function (Blueprint $table) {
            $table->id();
            $table->string('no_resep')->unique();
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('nama_dokter')->nullable();
            $table->string('no_sip')->nullable();
            $table->string('jenis_penjamin')->default('umum');
            $table->string('jenis_layanan')->default('BP');
            $table->string('nama_pasien');
            $table->string('no_rm')->nullable();
            $table->integer('umur_pasien')->nullable();
            $table->decimal('berat_badan', 5, 2)->nullable();
            $table->string('alamat_pasien')->nullable();
            $table->string('jenis_kelamin')->nullable();
            $table->date('tanggal_resep');
            $table->text('diagnosa')->nullable();
            $table->text('catatan')->nullable();
            // SQLite tidak support ENUM, gunakan string
            $table->string('status')->default('proses');
            $table->boolean('is_read')->default(false);
            $table->timestamps();
        });

        Schema::create('resep_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('resep_id')->constrained('reseps')->onDelete('cascade');
            $table->foreignId('obat_masuk_id')->nullable()->constrained('obat_masuks')->onDelete('set null');
            $table->string('nama_obat')->nullable();
            $table->integer('jumlah');
            $table->string('aturan_pakai')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('resep_items');
        Schema::dropIfExists('reseps');
        Schema::dropIfExists('obat_keluars');
        Schema::dropIfExists('obat_masuks');
        Schema::dropIfExists('obats');
        Schema::dropIfExists('satuans');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('kategoris');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('users');
    }
};
