import assert from "node:assert/strict";
import test from "node:test";
import { GET as getCase, POST as postCase } from "@/app/api/mortgage/case/route";
import { POST as postChat } from "@/app/api/mortgage/chat/route";
import { GET as getProfile, PATCH as patchProfile } from "@/app/api/mortgage/profile/route";

test("API-04 GET case without id is 400", async () => {
  const res = await getCase(new Request("http://localhost/api/mortgage/case"));
  assert.equal(res.status, 400);
  const body = await res.json();
  assert.match(String(body.error), /id required/i);
});

test("API-07 POST chat missing caseId/message is 400", async () => {
  const res = await postChat(
    new Request("http://localhost/api/mortgage/chat", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({}),
    })
  );
  assert.equal(res.status, 400);
  const body = await res.json();
  assert.match(String(body.error), /caseId and message required/i);
});

test("API-09 GET profile without id is 400", async () => {
  const res = await getProfile(new Request("http://localhost/api/mortgage/profile"));
  assert.equal(res.status, 400);
  const body = await res.json();
  assert.match(String(body.error), /id required/i);
});

async function expectNotFoundOrSkip(t: { skip: (r?: string) => void }, run: () => Promise<Response>) {
  try {
    const res = await run();
    assert.equal(res.status, 404);
  } catch (e) {
    const msg = e instanceof Error ? e.message : String(e);
    if (/Can't reach database|does not exist|P1001|P1003/i.test(msg)) {
      t.skip("MySQL not running — 404 path needs a reachable DATABASE_URL");
      return;
    }
    throw e;
  }
}

test("API-08 POST chat nonexistent case is 404", async (t) => {
  await expectNotFoundOrSkip(t, () =>
    postChat(
      new Request("http://localhost/api/mortgage/chat", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ caseId: "bogus-case-id-000", message: "hi" }),
      })
    )
  );
});

test("API-05 GET nonexistent case is 404", async (t) => {
  await expectNotFoundOrSkip(t, () =>
    getCase(new Request("http://localhost/api/mortgage/case?id=bogus-case-id-000"))
  );
});

test("API-10 PATCH profile nonexistent case is 404", async (t) => {
  await expectNotFoundOrSkip(t, () =>
    patchProfile(
      new Request("http://localhost/api/mortgage/profile", {
        method: "PATCH",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ caseId: "bogus-case-id-000", patch: { annual_income: 1 } }),
      })
    )
  );
});

test("API-02 POST case without MySQL surfaces 500", async () => {
  const prev = process.env.DATABASE_URL;
  process.env.DATABASE_URL = "mysql://root:@127.0.0.1:3306/does_not_exist_matrix";
  try {
    const res = await postCase();
    if (res.status === 200) {
      assert.ok(true, "live DB is available — create path works");
    } else {
      assert.equal(res.status, 500);
      const body = await res.json();
      assert.ok(body.error);
    }
  } finally {
    if (prev) process.env.DATABASE_URL = prev;
  }
});
