-- CreateEnum
CREATE TYPE "Role" AS ENUM ('STAFF', 'SUPERVISOR', 'ADMIN');

-- CreateEnum
CREATE TYPE "StatusDokumen" AS ENUM ('TERBUKA', 'PROSES', 'SELESAI');

-- CreateEnum
CREATE TYPE "JenisTransaksi" AS ENUM ('FCL', 'LCL');

-- CreateEnum
CREATE TYPE "JenisLayanan" AS ENUM ('FCL_TPP_BTD', 'FCL_TPP_BDN', 'LCL_TPP_BTD', 'LCL_TPP_BDN', 'GUDANG_FCL', 'GUDANG_LCL', 'FULL_EKSPOR', 'FULL_IMPOR');

-- CreateEnum
CREATE TYPE "StatusInvoice" AS ENUM ('DRAFT', 'TERBIT', 'LUNAS', 'BATAL');

-- CreateEnum
CREATE TYPE "StatusPembayaran" AS ENUM ('BELUM_BAYAR', 'SEBAGIAN', 'LUNAS', 'KADALUARSA');

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "nama" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "passwordHash" TEXT NOT NULL,
    "role" "Role" NOT NULL DEFAULT 'STAFF',
    "aktif" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "refresh_tokens" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "klien" (
    "id" TEXT NOT NULL,
    "kode" TEXT NOT NULL,
    "nama" TEXT NOT NULL,
    "alamat" TEXT,
    "npwp" TEXT,
    "telepon" TEXT,
    "email" TEXT,
    "aktif" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "tarif_global" (
    "id" TEXT NOT NULL,
    "kategori" TEXT NOT NULL,
    "field" TEXT NOT NULL,
    "nilai" DECIMAL(15,2) NOT NULL,
    "keterangan" TEXT,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "updatedBy" TEXT,

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

-- CreateTable
CREATE TABLE "tarif_khusus" (
    "id" TEXT NOT NULL,
    "klienId" TEXT NOT NULL,
    "kategori" TEXT NOT NULL,
    "field" TEXT NOT NULL,
    "nilai" DECIMAL(15,2) NOT NULL,
    "catatan" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "btd_dokumen" (
    "id" TEXT NOT NULL,
    "nomorBcf" TEXT NOT NULL,
    "tanggalBcf" TIMESTAMP(3),
    "nomorBc11" TEXT,
    "tanggalBc11" TIMESTAMP(3),
    "posBc11" TEXT,
    "saranaPengangkut" TEXT,
    "pemilikBarang" TEXT NOT NULL,
    "klienId" TEXT,
    "jumlahJenisKemasan" TEXT,
    "uraianBarang" TEXT,
    "tppTujuan" TEXT NOT NULL DEFAULT 'PT. Sarana Gemilang',
    "jenisTransaksi" "JenisTransaksi" NOT NULL,
    "status" "StatusDokumen" NOT NULL DEFAULT 'TERBUKA',
    "containers" JSONB NOT NULL DEFAULT '[]',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "createdBy" TEXT,

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

-- CreateTable
CREATE TABLE "bdn_dokumen" (
    "id" TEXT NOT NULL,
    "nomorKepBdn" TEXT NOT NULL,
    "tanggalKepBdn" TIMESTAMP(3),
    "nomorPib" TEXT,
    "tanggalPib" TIMESTAMP(3),
    "nomorPersetujuan" TEXT,
    "tanggalPersetujuan" TIMESTAMP(3),
    "pemilikBarang" TEXT NOT NULL,
    "klienId" TEXT,
    "jumlahJenisKemasan" TEXT,
    "uraianBarang" TEXT,
    "tppTujuan" TEXT NOT NULL DEFAULT 'PT. Sarana Gemilang',
    "jenisTransaksi" "JenisTransaksi" NOT NULL,
    "status" "StatusDokumen" NOT NULL DEFAULT 'TERBUKA',
    "containers" JSONB NOT NULL DEFAULT '[]',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "createdBy" TEXT,

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

-- CreateTable
CREATE TABLE "gudang_dokumen" (
    "id" TEXT NOT NULL,
    "blNomor" TEXT NOT NULL,
    "consignee" TEXT NOT NULL,
    "klienId" TEXT,
    "party" TEXT,
    "jenisTransaksi" "JenisTransaksi" NOT NULL,
    "status" "StatusDokumen" NOT NULL DEFAULT 'TERBUKA',
    "catatan" TEXT,
    "nomorHBL" TEXT,
    "cbm" DECIMAL(10,3),
    "tglMasuk" TIMESTAMP(3),
    "tglKeluar" TIMESTAMP(3),
    "containers" JSONB NOT NULL DEFAULT '[]',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "createdBy" TEXT,

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

-- CreateTable
CREATE TABLE "invoices" (
    "id" TEXT NOT NULL,
    "nomorKW" TEXT NOT NULL,
    "tahun" INTEGER NOT NULL,
    "bulan" INTEGER NOT NULL,
    "sequence" INTEGER NOT NULL,
    "tanggalInvoice" TIMESTAMP(3) NOT NULL,
    "jenisLayanan" "JenisLayanan" NOT NULL,
    "tujuan" TEXT,
    "btdDokumenId" TEXT,
    "bdnDokumenId" TEXT,
    "gudangDokumenId" TEXT,
    "klienId" TEXT,
    "consignee" TEXT NOT NULL,
    "subTotal" DECIMAL(15,2) NOT NULL,
    "ppn" DECIMAL(15,2) NOT NULL,
    "grandTotal" DECIMAL(15,2) NOT NULL,
    "tpsAsal" DECIMAL(15,2) NOT NULL DEFAULT 0,
    "totalAkhir" DECIMAL(15,2) NOT NULL,
    "detailSnapshot" JSONB NOT NULL,
    "status" "StatusInvoice" NOT NULL DEFAULT 'TERBIT',
    "statusPembayaran" "StatusPembayaran" NOT NULL DEFAULT 'BELUM_BAYAR',
    "catatan" TEXT,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "deletedAt" TIMESTAMP(3),
    "deletedById" TEXT,
    "alasanHapus" TEXT,
    "paymentId" TEXT,
    "paymentUrl" TEXT,
    "paidAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "invoice_baris" (
    "id" TEXT NOT NULL,
    "invoiceId" TEXT NOT NULL,
    "urutan" INTEGER NOT NULL,
    "label" TEXT NOT NULL,
    "qty20" INTEGER,
    "qty40" INTEGER,
    "hari" INTEGER,
    "cbm" DECIMAL(10,3),
    "tarif" DECIMAL(15,2) NOT NULL,
    "jumlah" DECIMAL(15,2) NOT NULL,

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

-- CreateTable
CREATE TABLE "nomor_kw_sequence" (
    "tahun" INTEGER NOT NULL,
    "terakhir" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "nomor_kw_sequence_pkey" PRIMARY KEY ("tahun")
);

-- CreateTable
CREATE TABLE "audit_log" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "aksi" TEXT NOT NULL,
    "entitas" TEXT NOT NULL,
    "entitasId" TEXT,
    "dataBefore" JSONB,
    "dataAfter" JSONB,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "refresh_tokens_token_key" ON "refresh_tokens"("token");

-- CreateIndex
CREATE UNIQUE INDEX "klien_kode_key" ON "klien"("kode");

-- CreateIndex
CREATE UNIQUE INDEX "tarif_global_kategori_field_key" ON "tarif_global"("kategori", "field");

-- CreateIndex
CREATE UNIQUE INDEX "tarif_khusus_klienId_kategori_field_key" ON "tarif_khusus"("klienId", "kategori", "field");

-- CreateIndex
CREATE INDEX "btd_dokumen_nomorBcf_idx" ON "btd_dokumen"("nomorBcf");

-- CreateIndex
CREATE INDEX "btd_dokumen_pemilikBarang_idx" ON "btd_dokumen"("pemilikBarang");

-- CreateIndex
CREATE INDEX "btd_dokumen_status_idx" ON "btd_dokumen"("status");

-- CreateIndex
CREATE INDEX "bdn_dokumen_nomorKepBdn_idx" ON "bdn_dokumen"("nomorKepBdn");

-- CreateIndex
CREATE INDEX "bdn_dokumen_pemilikBarang_idx" ON "bdn_dokumen"("pemilikBarang");

-- CreateIndex
CREATE INDEX "gudang_dokumen_blNomor_idx" ON "gudang_dokumen"("blNomor");

-- CreateIndex
CREATE INDEX "gudang_dokumen_consignee_idx" ON "gudang_dokumen"("consignee");

-- CreateIndex
CREATE UNIQUE INDEX "invoices_nomorKW_key" ON "invoices"("nomorKW");

-- CreateIndex
CREATE INDEX "invoices_nomorKW_idx" ON "invoices"("nomorKW");

-- CreateIndex
CREATE INDEX "invoices_tahun_sequence_idx" ON "invoices"("tahun", "sequence");

-- CreateIndex
CREATE INDEX "invoices_tanggalInvoice_idx" ON "invoices"("tanggalInvoice");

-- CreateIndex
CREATE INDEX "invoices_deletedAt_idx" ON "invoices"("deletedAt");

-- CreateIndex
CREATE INDEX "invoice_baris_invoiceId_idx" ON "invoice_baris"("invoiceId");

-- CreateIndex
CREATE INDEX "audit_log_userId_idx" ON "audit_log"("userId");

-- CreateIndex
CREATE INDEX "audit_log_entitas_entitasId_idx" ON "audit_log"("entitas", "entitasId");

-- CreateIndex
CREATE INDEX "audit_log_createdAt_idx" ON "audit_log"("createdAt");

-- AddForeignKey
ALTER TABLE "refresh_tokens" ADD CONSTRAINT "refresh_tokens_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tarif_khusus" ADD CONSTRAINT "tarif_khusus_klienId_fkey" FOREIGN KEY ("klienId") REFERENCES "klien"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "btd_dokumen" ADD CONSTRAINT "btd_dokumen_klienId_fkey" FOREIGN KEY ("klienId") REFERENCES "klien"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bdn_dokumen" ADD CONSTRAINT "bdn_dokumen_klienId_fkey" FOREIGN KEY ("klienId") REFERENCES "klien"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_btdDokumenId_fkey" FOREIGN KEY ("btdDokumenId") REFERENCES "btd_dokumen"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_bdnDokumenId_fkey" FOREIGN KEY ("bdnDokumenId") REFERENCES "bdn_dokumen"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_gudangDokumenId_fkey" FOREIGN KEY ("gudangDokumenId") REFERENCES "gudang_dokumen"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_klienId_fkey" FOREIGN KEY ("klienId") REFERENCES "klien"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_deletedById_fkey" FOREIGN KEY ("deletedById") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoice_baris" ADD CONSTRAINT "invoice_baris_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "audit_log" ADD CONSTRAINT "audit_log_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
