"use client";

import { cva, type VariantProps } from "class-variance-authority";
import { motion } from "motion/react";
import { Tabs as TabsPrimitive } from "radix-ui";
import type * as React from "react";
import { AnimatedButton } from "./button";
import { cn } from "./lib/utils";
import { defaultVariantsCard } from "./styles/animations";

function Tabs({
	className,
	orientation = "horizontal",
	children,
	...props
}: React.ComponentProps<typeof TabsPrimitive.Root>) {
	return (
		<TabsPrimitive.Root
			data-slot="tabs"
			data-orientation={orientation}
			orientation={orientation}
			className={cn(
				"group/tabs flex gap-2 data-[orientation=horizontal]:flex-col",
				className,
			)}
			{...props}
		>
			{children}
		</TabsPrimitive.Root>
	);
}

const tabsListVariants = cva(
	cn(
		"rounded-full data-[variant=line]:rounded-none",
		"p-2 gap-1",
		"group/tabs-list text-main inline-flex w-fit items-center justify-center \
   group-data-[orientation=vertical]/tabs:h-fit group-data-[orientation=vertical]/tabs:flex-col",
		"ring-1 ring-main/25",
	),
	{
		variants: {
			colorTheme: {
				default: "bg-neutral-accent",
				neutral: "bg-neutral",
			},
			allowFlexCol: {
				true: "flex-col lg:flex-row lg:h-input-height max-lg:w-full",
				false: "flex-row",
			},
		},
		defaultVariants: {
			colorTheme: "default",
			allowFlexCol: true,
		},
	},
);

function TabsList({
	className,
	colorTheme = "default",
	allowFlexCol = true,
	children,
	...props
}: React.ComponentProps<typeof TabsPrimitive.List> &
	VariantProps<typeof tabsListVariants>) {
	return (
		<motion.div variants={defaultVariantsCard}>
			<TabsPrimitive.List
				data-slot="tabs-list"
				data-color-theme={colorTheme}
				className={cn(
					tabsListVariants({ colorTheme, allowFlexCol }),
					className,
				)}
				{...props}
			>
				{children}
			</TabsPrimitive.List>
		</motion.div>
	);
}

function TabsTrigger({
	className,
	children,
	...props
}: React.ComponentProps<typeof TabsPrimitive.Trigger>) {
	return (
		<TabsPrimitive.Trigger
			asChild
			data-slot="tabs-trigger"
			className={cn(
				"w-full lg:w-fit",
				"font-medium lg:h-full!",
				"focus-visible:border-main focus-visible:ring-main/50 focus-visible:outline-main text-main/60 \
         hover:text-main dark:text-main dark:hover:text-main relative inline-flex",
				"items-center justify-center gap-1.5 rounded-full border border-transparent px-3 py-1",
				"whitespace-nowrap group-data-[orientation=vertical]/tabs:w-full group-data-[orientation=vertical]/tabs:justify-start \
            focus-visible:ring-[3px] focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 \
             group-data-[variant=default]/tabs-list:data-[state=active]:shadow-sm group-data-[variant=line]/tabs-list:data-[state=active]:shadow-none \
              [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
				"group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-[state=active]:bg-transparent \
         dark:group-data-[variant=line]/tabs-list:data-[state=active]:border-transparent dark:group-data-[variant=line]/tabs-list:data-[state=active]:bg-transparent",
				"data-[state=active]:bg-neutral dark:data-[state=active]:text-main dark:data-[state=active]:border-input \
         dark:data-[state=active]:bg-input/30 data-[state=active]:text-main",
				// bg
				"after:bg-main after:absolute after:opacity-0 after:transition-opacity group-data-[orientation=horizontal]/tabs:after:inset-x-0 \
         group-data-[orientation=horizontal]/tabs:after:bottom-[-5px] group-data-[orientation=horizontal]/tabs:after:h-0.5 \
          group-data-[orientation=vertical]/tabs:after:inset-y-0 group-data-[orientation=vertical]/tabs:after:-right-1 \
           group-data-[orientation=vertical]/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-[state=active]:after:opacity-100",
				className,
			)}
			{...props}
		>
			<AnimatedButton variant="ghost">{children}</AnimatedButton>
		</TabsPrimitive.Trigger>
	);
}

function TabsContent({
	className,
	...props
}: React.ComponentProps<typeof TabsPrimitive.Content>) {
	return (
		<TabsPrimitive.Content
			data-slot="tabs-content"
			className={cn("flex-1 outline-none", className)}
			{...props}
		/>
	);
}

export { Tabs, TabsContent, TabsList, TabsTrigger, tabsListVariants };
