import React from "react";
import { VimeoEmbed } from "./vimeo-embed";

import { render, screen } from "@testing-library/react";

jest.mock("@shared/utils", () => ({
  cx: (...args: any[]) => args.filter(Boolean).join(" "),
}));

describe("VimeoEmbed", () => {
  describe("Rendering", () => {
    it("renders iframe with correct src for standard vimeo link", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789" />);
      const iframe = screen.getByTitle("Embedded vimeo");
      expect(iframe).toBeInTheDocument();
      expect(iframe.getAttribute("src")).toContain(
        "player.vimeo.com/video/123456789"
      );
    });

    it("renders null when link is empty", () => {
      const { container } = render(<VimeoEmbed link="" />);
      expect(container.innerHTML).toBe("");
    });

    it("renders null when link is undefined", () => {
      const { container } = render(<VimeoEmbed link={undefined} />);
      expect(container.innerHTML).toBe("");
    });

    it("renders null when link has no valid vimeo id", () => {
      const { container } = render(
        <VimeoEmbed link="https://example.com/notavideo" />
      );
      expect(container.innerHTML).toBe("");
    });

    it("applies containerClassName", () => {
      const { container } = render(
        <VimeoEmbed
          link="https://vimeo.com/123456789"
          containerClassName="custom"
        />
      );
      expect(container.firstElementChild?.className).toContain("custom");
    });

    it("sets autoplay=1 when autoplay is true", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789" autoplay={true} />);
      const iframe = screen.getByTitle("Embedded vimeo");
      expect(iframe.getAttribute("src")).toContain("autoplay=1");
    });

    it("sets autoplay=0 when autoplay is false", () => {
      render(
        <VimeoEmbed link="https://vimeo.com/123456789" autoplay={false} />
      );
      const iframe = screen.getByTitle("Embedded vimeo");
      expect(iframe.getAttribute("src")).toContain("autoplay=0");
    });

    it("has correct iframe attributes", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789" />);
      const iframe = screen.getByTitle("Embedded vimeo");
      expect(iframe).toHaveAttribute(
        "allow",
        "autoplay; fullscreen; picture-in-picture"
      );
      expect(iframe).toHaveAttribute(
        "referrerPolicy",
        "strict-origin-when-cross-origin"
      );
    });
  });

  describe("extractVimeoIdandHash", () => {
    it("extracts id from standard vimeo.com URL", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789" />);
      expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
        "/video/123456789"
      );
    });

    it("extracts id and hash from vimeo URL with hash path", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789/abc123def" />);
      const src = screen.getByTitle("Embedded vimeo").getAttribute("src")!;
      expect(src).toContain("/video/123456789");
      expect(src).toContain("h=abc123def");
    });

    it("extracts id from player.vimeo.com URL", () => {
      render(<VimeoEmbed link="https://player.vimeo.com/video/987654321" />);
      expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
        "/video/987654321"
      );
    });

    it("strips query params from link before extracting", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789?autoplay=1" />);
      expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
        "/video/123456789"
      );
    });

    it("strips trailing slashes from link", () => {
      render(<VimeoEmbed link="https://vimeo.com/123456789/" />);
      expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
        "/video/123456789"
      );
    });

    it("handles vimeo URL with nested path", () => {
      render(
        <VimeoEmbed link="https://vimeo.com/channels/staffpicks/123456789" />
      );
      expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
        "/video/123456789"
      );
    });
  });

  describe("debug mode", () => {
    it("logs to console when debug is true", () => {
      const consoleSpy = jest.spyOn(console, "log").mockImplementation();
      render(<VimeoEmbed link="https://vimeo.com/123456789" debug={true} />);
      expect(consoleSpy).toHaveBeenCalledWith(
        "[VimeoEmbed] href:",
        "https://vimeo.com/123456789",
        "id:",
        expect.any(String)
      );
      consoleSpy.mockRestore();
    });

    it("does not log when debug is false", () => {
      const consoleSpy = jest.spyOn(console, "log").mockImplementation();
      render(<VimeoEmbed link="https://vimeo.com/123456789" />);
      expect(consoleSpy).not.toHaveBeenCalled();
      consoleSpy.mockRestore();
    });
  });
});
