import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
  APPLICANT_CASE_ID_SESSION_KEY,
  APPLICANT_REPORT_STEP_SESSION_KEY,
  CASE_ID_SEARCH_PARAM,
  FRESH_DASHBOARD_SEARCH_PARAM,
  clearApplicantRegistrationSessionStorage,
  getFreshApplicantDashboardHref,
  getResumeApplicantDashboardHref,
  prepareFreshApplicantDashboardNavigation,
} from "./applicantRegistrationSession";

vi.mock("@/lib/applicantSubmissionRecord", () => ({
  clearApplicantSubmissionRecord: vi.fn(),
}));

import { clearApplicantSubmissionRecord } from "@/lib/applicantSubmissionRecord";

describe("applicantRegistrationSession", () => {
  beforeEach(() => {
    sessionStorage.clear();
    localStorage.clear();
    vi.mocked(clearApplicantSubmissionRecord).mockClear();
  });

  afterEach(() => {
    sessionStorage.clear();
    localStorage.clear();
  });

  describe("getFreshApplicantDashboardHref", () => {
    it("returns a string path pointing to /dashboard with fresh flag", () => {
      const href = getFreshApplicantDashboardHref();
      expect(typeof href).toBe("string");
      expect(href).toBe(`/dashboard?${FRESH_DASHBOARD_SEARCH_PARAM}=1`);
    });

    it("is safe to pass to window.location.assign (no non-string content)", () => {
      const href = getFreshApplicantDashboardHref();
      expect(String(href)).not.toContain("[object Object]");
      expect(href.startsWith("/dashboard")).toBe(true);
    });
  });

  describe("getResumeApplicantDashboardHref", () => {
    it("returns /dashboard with caseId when id is provided", () => {
      const href = getResumeApplicantDashboardHref("CASE-123");
      expect(href).toBe(`/dashboard?${CASE_ID_SEARCH_PARAM}=CASE-123`);
    });

    it("trims whitespace around the caseId", () => {
      const href = getResumeApplicantDashboardHref("  CASE-42  ");
      expect(href).toBe(`/dashboard?${CASE_ID_SEARCH_PARAM}=CASE-42`);
    });

    it("falls back to bare /dashboard when caseId is empty", () => {
      expect(getResumeApplicantDashboardHref("")).toBe("/dashboard");
      expect(getResumeApplicantDashboardHref("   ")).toBe("/dashboard");
    });

    it("URL-encodes special characters in caseId", () => {
      const href = getResumeApplicantDashboardHref("case id/1");
      expect(href).toBe(`/dashboard?${CASE_ID_SEARCH_PARAM}=case+id%2F1`);
    });
  });

  describe("clearApplicantRegistrationSessionStorage", () => {
    it("removes applicant draft session keys", () => {
      sessionStorage.setItem(APPLICANT_CASE_ID_SESSION_KEY, "CASE-abc");
      sessionStorage.setItem(APPLICANT_REPORT_STEP_SESSION_KEY, "1");

      clearApplicantRegistrationSessionStorage();

      expect(sessionStorage.getItem(APPLICANT_CASE_ID_SESSION_KEY)).toBeNull();
      expect(sessionStorage.getItem(APPLICANT_REPORT_STEP_SESSION_KEY)).toBeNull();
    });

    it("does not throw when keys are absent", () => {
      expect(() => clearApplicantRegistrationSessionStorage()).not.toThrow();
    });
  });

  describe("prepareFreshApplicantDashboardNavigation", () => {
    it("clears session storage, local draft snapshot, and submission shortcut", () => {
      sessionStorage.setItem(APPLICANT_CASE_ID_SESSION_KEY, "CASE-abc");
      sessionStorage.setItem(APPLICANT_REPORT_STEP_SESSION_KEY, "1");
      localStorage.setItem("initiativeDraft.v1", JSON.stringify({ foo: "bar" }));

      prepareFreshApplicantDashboardNavigation();

      expect(sessionStorage.getItem(APPLICANT_CASE_ID_SESSION_KEY)).toBeNull();
      expect(sessionStorage.getItem(APPLICANT_REPORT_STEP_SESSION_KEY)).toBeNull();
      expect(localStorage.getItem("initiativeDraft.v1")).toBeNull();
      expect(clearApplicantSubmissionRecord).toHaveBeenCalledTimes(1);
    });
  });
});
