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


const buttonText = "Primary Button";


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

		const button = screen.getByTestId("mattrixwv-primary-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-primary", "active:bg-primary-dark");
		expect(button).toHaveClass("border-primary", "active:border-primary-dark");
		expect(button).toHaveClass("text-white");
	});

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

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

		//Check button requirements
		const button = screen.getByTestId("mattrixwv-primary-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass(customClass);
	});

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		const button = screen.getByTestId("mattrixwv-primary-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-primary-light/80");
		expect(button).toHaveClass("border-primary-light/80");
		expect(button).toHaveClass("text-white");
	});
	it("Renders with custom className when disabled", () => {
		const customClass = "custom-primary-button-class";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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