-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateTable
CREATE TABLE "public"."blockchain_log_queue" (
    "id" BIGSERIAL NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "event" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "created_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP,
    "payload" JSONB NOT NULL,
    "processed_at" TIMESTAMPTZ(6),
    "transactionid" VARCHAR(64),

    CONSTRAINT "blockchain_log_queue_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."pemberian_obat" (
    "id" SERIAL NOT NULL,
    "id_visit" VARCHAR(25) NOT NULL,
    "obat" VARCHAR(100) NOT NULL,
    "jumlah_obat" INTEGER NOT NULL,
    "aturan_pakai" TEXT,

    CONSTRAINT "pemberian_obat_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."pemberian_tindakan" (
    "id" SERIAL NOT NULL,
    "id_visit" VARCHAR(25) NOT NULL,
    "tindakan" VARCHAR(100) NOT NULL,
    "kategori_tindakan" VARCHAR(50),
    "kelompok_tindakan" VARCHAR(50),

    CONSTRAINT "pemberian_tindakan_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."rekam_medis" (
    "id_visit" VARCHAR(25) NOT NULL,
    "waktu_visit" TIMESTAMP(6) NOT NULL,
    "no_rm" VARCHAR(20) NOT NULL,
    "nama_pasien" VARCHAR(100) NOT NULL,
    "umur" INTEGER,
    "jenis_kelamin" VARCHAR(10),
    "gol_darah" VARCHAR(3),
    "pekerjaan" VARCHAR(100),
    "suku" VARCHAR(100),
    "kode_diagnosa" VARCHAR(20),
    "diagnosa" TEXT,
    "anamnese" TEXT,
    "sistolik" INTEGER,
    "diastolik" INTEGER,
    "nadi" INTEGER,
    "suhu" DECIMAL(4,1),
    "nafas" INTEGER,
    "tinggi_badan" DECIMAL(10,5),
    "berat_badan" DECIMAL(10,5),
    "jenis_kasus" VARCHAR(50),
    "tindak_lanjut" TEXT,

    CONSTRAINT "rekam_medis_pkey" PRIMARY KEY ("id_visit")
);

-- CreateTable
CREATE TABLE "public"."users" (
    "id" BIGSERIAL NOT NULL,
    "nama_lengkap" VARCHAR(255) NOT NULL,
    "username" VARCHAR(255) NOT NULL,
    "password_hash" VARCHAR(255) NOT NULL,
    "role" VARCHAR(50) DEFAULT 'user',
    "created_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "public"."users"("username" ASC);

-- AddForeignKey
ALTER TABLE "public"."blockchain_log_queue" ADD CONSTRAINT "fk_log_user" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."pemberian_obat" ADD CONSTRAINT "fk_pemberian_obat_visit" FOREIGN KEY ("id_visit") REFERENCES "public"."rekam_medis"("id_visit") ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE "public"."pemberian_tindakan" ADD CONSTRAINT "fk_tindakan_visit" FOREIGN KEY ("id_visit") REFERENCES "public"."rekam_medis"("id_visit") ON DELETE CASCADE ON UPDATE NO ACTION;

