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


const buttonText = "Molten Button";


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

		const button = screen.getByTestId("mattrixwv-molten-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-orange-600", "hover:bg-orange-700", "active:bg-orange-800");
		expect(button).toHaveClass("border-orange-600", "hover:border-orange-700", "active:border-orange-800");
		expect(button).toHaveClass("text-white");
	});

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		const button = screen.getByTestId("mattrixwv-molten-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-orange-600", "hover:bg-orange-700", "active:bg-orange-800");
		expect(button).toHaveClass("border", "border-orange-600", "hover:border-orange-700", "active:border-orange-800");
		expect(button).toHaveClass("text-white");
	});
	it("Renders with outline variant", () => {
		render(<MoltenButton variant="outline">{buttonText}</MoltenButton>);

		const button = screen.getByTestId("mattrixwv-molten-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border", "border-orange-600", "hover:border-orange-700", "active:border-orange-800");
		expect(button).toHaveClass("text-orange-600", "hover:text-orange-700", "active:text-orange-800");
	});
	it("Renders with outline-ghost variant", () => {
		render(<MoltenButton variant="outline-ghost">{buttonText}</MoltenButton>);

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

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

		const button = screen.getByTestId("mattrixwv-molten-button");
		expect(button).toBeInTheDocument();
		expect(button).toHaveClass("bg-transparent");
		expect(button).toHaveClass("border-none");
		expect(button).toHaveClass("text-orange-600", "hover:text-orange-700", "active:text-orange-800");
	});
});

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

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

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

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

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

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