// @vitest-environment node

import { readFile } from "node:fs/promises";
import path from "node:path";
import { describe, expect, it } from "vitest";

async function source(relativePath: string) {
  return readFile(path.join(process.cwd(), relativePath), "utf8");
}

describe("orphan upload layout", () => {
  it("stores documents and receipts below each named orphan folder", async () => {
    const storage = await source("src/lib/storage.ts");
    expect(storage).toContain('"public", "uploads"');
    expect(storage).toContain("namedOrphanUploadRoot");
    expect(storage).toContain("orphanUploadFolderName(orphan)");
    expect(storage).toContain("legacyOrphanUploadRoot");
    expect(storage).toContain("pendingOrphanUploadRoot");
    expect(storage).toContain('type UploadKind = "documents" | "receipts"');
    expect(storage).toContain("ensureOrphanUploadFolders");
  });

  it("blocks direct public upload URLs", async () => {
    const proxy = await source("src/proxy.ts");
    expect(proxy).toContain('pathname.startsWith("/uploads/")');
    expect(proxy).toContain("status: 404");
  });

  it("keeps monthly linkage and transfer status out of the transfer form", async () => {
    const form = await source("src/components/forms/transfer-create-form.tsx");
    const partyFields = await source("src/components/forms/transfer-party-fields.tsx");
    const transfersPage = await source("src/app/admin/transfers/page.tsx");
    expect(form).toContain("<TransferPartyFields");
    expect(transfersPage).toContain("<TransferPartyFields");
    expect(partyFields).toContain('name="sponsorId"');
    expect(partyFields).toContain('name="orphanId"');
    expect(form).toContain("autoSelectFirstOrphan");
    expect(partyFields).toContain("nextOrphans.length > 0");
    expect(partyFields).toContain("disabled={!sponsorId}");
    expect(form).not.toContain('name="sponsorshipId"');
    expect(form).not.toContain('name="sponsorMonthlyPaymentId"');
    expect(form).not.toContain('name="status"');
    expect(form).not.toContain("دفعة المتبرع الشهرية المرتبطة");
    expect(form).not.toContain("حالة التحويل");
  });
});
