import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("~/models/sport", () => ({
  findAllSports: vi.fn(),
  findSportById: vi.fn(),
  isSportIconUrlUsed: vi.fn(),
  updateSport: vi.fn(),
}));
vi.mock("~/lib/cloudinary.server", () => ({
  deleteCloudinaryImageByUrl: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("~/lib/logger", () => ({
  logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },
}));
vi.mock("~/services/simulations/registry", () => ({
  SIMULATOR_TYPES: ["playoff_bracket"],
  getSimulatorInfo: vi.fn((type: string) => ({ name: type })),
}));

import { action } from "../admin.sports.$id";
import { deleteCloudinaryImageByUrl } from "~/lib/cloudinary.server";
import { findSportById, isSportIconUrlUsed, updateSport } from "~/models/sport";

function makeRequest(iconUrl: string, options: { excludeFromHomepage?: boolean } = {}) {
  const formData = new URLSearchParams();
  formData.set("name", "NFL");
  formData.set("type", "team");
  formData.set("slug", "nfl");
  formData.set("description", "Football");
  formData.set("simulatorType", "playoff_bracket");
  formData.set("iconUrl", iconUrl);
  if (options.excludeFromHomepage) {
    formData.set("excludeFromHomepage", "on");
  }

  return new Request("http://test/admin/sports/sport-1", {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: formData.toString(),
  });
}

beforeEach(() => {
  vi.clearAllMocks();
  vi.mocked(isSportIconUrlUsed).mockResolvedValue(false);
  vi.mocked(findSportById).mockResolvedValue({
    id: "sport-1",
    name: "Old NFL",
    type: "team",
    slug: "nfl",
    description: null,
    iconUrl: "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/old.svg",
    simulatorType: null,
    excludeFromHomepage: false,
    createdAt: new Date(),
    updatedAt: new Date(),
  } as never);
});

describe("admin sport edit action", () => {
  it("stores the public page exclusion flag", async () => {
    vi.mocked(updateSport).mockResolvedValue({
      iconUrl: "nfl.svg",
    } as never);

    await action({
      request: makeRequest("nfl.svg", { excludeFromHomepage: true }),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(updateSport).toHaveBeenCalledWith("sport-1", expect.objectContaining({
      excludeFromHomepage: true,
    }));
  });

  it("stores the submitted icon URL and deletes a replaced Cloudinary icon", async () => {
    const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
    vi.mocked(updateSport).mockResolvedValue({
      iconUrl: nextIconUrl,
    } as never);

    await action({
      request: makeRequest(nextIconUrl),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(updateSport).toHaveBeenCalledWith("sport-1", expect.objectContaining({ iconUrl: nextIconUrl }));
    expect(deleteCloudinaryImageByUrl).toHaveBeenCalledWith(
      "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/old.svg"
    );
  });

  it("stores null when the icon is removed", async () => {
    vi.mocked(updateSport).mockResolvedValue({
      iconUrl: null,
    } as never);

    await action({
      request: makeRequest(""),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(updateSport).toHaveBeenCalledWith("sport-1", expect.objectContaining({ iconUrl: null }));
  });

  it("rejects arbitrary icon URLs", async () => {
    const response = await action({
      request: makeRequest("https://example.com/icon.svg"),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(response).toEqual({ error: "Sport icon must be an uploaded SVG icon." });
    expect(updateSport).not.toHaveBeenCalled();
  });

  it("deletes a newly uploaded icon when update fails", async () => {
    const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
    vi.mocked(updateSport).mockRejectedValue(new Error("duplicate"));

    await action({
      request: makeRequest(nextIconUrl),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(deleteCloudinaryImageByUrl).toHaveBeenCalledWith(nextIconUrl);
  });

  it("does not delete a reused existing icon when update fails", async () => {
    const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
    vi.mocked(updateSport).mockRejectedValue(new Error("duplicate"));
    vi.mocked(isSportIconUrlUsed).mockResolvedValue(true);

    await action({
      request: makeRequest(nextIconUrl),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(deleteCloudinaryImageByUrl).not.toHaveBeenCalled();
  });

  it("returns the update error when icon reuse lookup fails during cleanup", async () => {
    const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
    vi.mocked(updateSport).mockRejectedValue(new Error("database unavailable"));
    vi.mocked(isSportIconUrlUsed).mockRejectedValue(new Error("database unavailable"));

    const response = await action({
      request: makeRequest(nextIconUrl),
      params: { id: "sport-1" },
      context: {},
    } as never);

    expect(response).toEqual({ error: "Failed to update sport: database unavailable" });
    expect(deleteCloudinaryImageByUrl).not.toHaveBeenCalled();
  });
});
