import SecondaryButton from "$/component/button/SecondaryButton";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";


const buttonText = "Secondary Button";


describe("SecondaryButton Component", () => {
	it("Renders with defaults", () => {
		render(<SecondaryButton>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		//Check button requirements
		expect(button).toBeInTheDocument();
		expect(button).toHaveTextContent(buttonText);
		//Classes
		expect(button).toHaveClass("rounded-lg");
		expect(button).toHaveClass("px-4", "py-2");
		expect(button).toHaveClass("border");
		expect(button).toHaveClass("cursor-pointer");
		//Colors
		expect(button).toHaveClass("bg-secondary", "active:bg-secondary-dark");
		expect(button).toHaveClass("border-secondary", "active:border-secondary-dark");
		expect(button).toHaveClass("text-white");
	});

	it("Renders with custom className", () => {
		const customClass = "custom-secondary-button-class";

		render(<SecondaryButton className={customClass}>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass(customClass);
	});

	it("Renders with aria-label", () => {
		const ariaLabel = "Secondary Button Aria Label";

		render(<SecondaryButton aria-label={ariaLabel}>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveAttribute("aria-label", ariaLabel);
	});

	it("Renders with onClick handler", async () => {
		const handleClick = vi.fn();

		render(<SecondaryButton onClick={handleClick}>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();

		await userEvent.click(button);
		expect(handleClick).toHaveBeenCalledTimes(1);
	});

	describe("SecondaryButton Component Rounding", () => {
		it("Renders with small rounding", () => {
			render(<SecondaryButton rounding="sm">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("rounded-sm");
		});
		it("Renders with medium rounding", () => {
			render(<SecondaryButton rounding="md">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("rounded-md");
		});
		it("Renders with large rounding", () => {
			render(<SecondaryButton rounding="lg">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("rounded-lg");
		});
		it("Renders with full rounding", () => {
			render(<SecondaryButton rounding="full">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("rounded-full");
		});
	});

	describe("SecondaryButton Component Size and Shape", () => {
		it("Renders with xs square", () => {
			render(<SecondaryButton size="xs" shape="square">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("p-0");
		});
		it("Renders with sm square", () => {
			render(<SecondaryButton size="sm" shape="square">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("p-1");
		});
		it("Renders with md square", () => {
			render(<SecondaryButton size="md" shape="square">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("p-2");
		});
		it("Renders with lg square", () => {
			render(<SecondaryButton size="lg" shape="square">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("p-3");
		});
		it("Renders with xl square", () => {
			render(<SecondaryButton size="xl" shape="square">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("p-4");
		});
		it("Renders with xs rectangle", () => {
			render(<SecondaryButton size="xs" shape="rectangle">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("px-1", "py-0");
		});
		it("Renders with sm rectangle", () => {
			render(<SecondaryButton size="sm" shape="rectangle">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("px-2", "py-1");
		});
		it("Renders with md rectangle", () => {
			render(<SecondaryButton size="md" shape="rectangle">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("px-4", "py-2");
		});
		it("Renders with lg rectangle", () => {
			render(<SecondaryButton size="lg" shape="rectangle">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("px-6", "py-3");
		});
		it("Renders with xl rectangle", () => {
			render(<SecondaryButton size="xl" shape="rectangle">{buttonText}</SecondaryButton>);

			const button = screen.getByTestId("mattrixwv-secondary-button");
			expect(button).toBeInTheDocument();
			expect(button).toHaveClass("px-8", "py-4");
		});
	});
});

describe("SecondaryButton Component Disabled", () => {
	it("Render with defaults when disabled", () => {
		render(<SecondaryButton disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		//Check button requirements
		expect(button).toBeInTheDocument();
		expect(button).toHaveTextContent(buttonText);
		//Classes
		expect(button).toHaveClass("rounded-lg");
		expect(button).toHaveClass("px-4", "py-2");
		expect(button).toHaveClass("border");
		//Disabled state
		expect(button).not.toHaveClass("cursor-pointer");
		expect(button).toHaveClass("bg-secondary-light/80");
		expect(button).toHaveClass("border-secondary-light/80");
		expect(button).toHaveClass("text-white");
	});
	it("Renders with custom className when disabled", () => {
		const customClass = "custom-secondary-button-class";

		render(<SecondaryButton className={customClass} disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass(customClass);
	});
	it("Renders with aria-label when disabled", () => {
		const ariaLabel = "Secondary Button Aria Label";

		render(<SecondaryButton aria-label={ariaLabel} disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveAttribute("aria-label", ariaLabel);
	});
	it("Does not trigger onClick handler when disabled", async () => {
		const handleClick = vi.fn();

		render(<SecondaryButton onClick={handleClick} disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();

		await userEvent.click(button);
		expect(handleClick).toHaveBeenCalledTimes(0);
	});
});

describe("SecondaryButton Component Variants", () => {
	it("Renders with standard variant", () => {
		render(<SecondaryButton variant="standard">{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-secondary", "active:bg-secondary-dark");
		expect(button).toHaveClass("border", "border-secondary", "active:border-secondary-dark");
		expect(button).toHaveClass("text-white");
	});
	it("Renders with outline variant", () => {
		render(<SecondaryButton variant="outline">{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border", "border-secondary", "active:border-secondary-dark");
		expect(button).toHaveClass("text-secondary", "active:text-secondary-dark");
	});
	it("Renders with outline-ghost variant", () => {
		render(<SecondaryButton variant="outline-ghost">{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent", "hover:bg-secondary", "active:bg-secondary-dark");
		expect(button).toHaveClass("border", "border-secondary", "active:border-secondary-dark");
		expect(button).toHaveClass("text-secondary", "hover:text-white", "active:text-white");
	});
	it("Renders with ghost variant", () => {
		render(<SecondaryButton variant="ghost">{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent", "hover:bg-secondary", "active:bg-secondary-dark");
		expect(button).toHaveClass("border-none");
		expect(button).toHaveClass("text-secondary", "hover:text-white", "active:text-white");
	});
	it("Renders with icon variant", () => {
		render(<SecondaryButton variant="icon">{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border-none");
		expect(button).toHaveClass("text-secondary", "active:text-secondary-dark");
	});
});

describe("SecondaryButton Component Variants Disabled", () => {
	it("Renders with standard variant when disabled", () => {
		render(<SecondaryButton variant="standard" disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-secondary-light/80");
		expect(button).toHaveClass("border", "border-secondary-light/80");
		expect(button).toHaveClass("text-white");
	});
	it("Renders with outline variant when disabled", () => {
		render(<SecondaryButton variant="outline" disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border", "border-secondary-light/80");
		expect(button).toHaveClass("text-secondary-light/80");
	});
	it("Renders with outline-ghost variant when disabled", () => {
		render(<SecondaryButton variant="outline-ghost" disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border", "border-secondary-light/80");
		expect(button).toHaveClass("text-secondary-light/80");
	});
	it("Renders with ghost variant when disabled", () => {
		render(<SecondaryButton variant="ghost" disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border-none");
		expect(button).toHaveClass("text-secondary-light/80");
	});
	it("Renders with icon variant when disabled", () => {
		render(<SecondaryButton variant="icon" disabled>{buttonText}</SecondaryButton>);

		const button = screen.getByTestId("mattrixwv-secondary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border-none");
		expect(button).toHaveClass("text-secondary-light/80");
	});
});
