import * as React from "react"

import { cn } from "@/lib/utils"
import { Input } from "./input"
import { Button } from "./button"
import { X } from "lucide-react"


const Search = () => {
  return (
    <div className='mb-10 flex flex-col gap-2'>
      <div className=' flex flex-col md:flex-row gap-2 justify-end md:items-center'>
        <h3 className='uppercase md:text-sm text-xs text-left'>Search by category</h3>
        <div className='flex gap-2 w-fit justify-end'>
          <select className='max-w-32 w-full outline-none focus-visible:ring-2 focus-visible:ring-offset-2 ring-offset-background ring-primary text-black px-3 py-2 h-10 rounded-lg' >
            <option value="all">All</option>
          </select>
          <Input className='w-72' placeholder='Search' />
        </div>
      </div>
      <div className=' flex flex-row gap-2 justify-end items-center'>
        <h3 className=' md:text-sm text-xs '>Active Filters</h3>
        <div className='border border-gray-500 bg-gray-500 px-2 py-1 rounded-2xl flex justify-between items-center gap-3 uppercase text-xs'>
          <span className='text-white'>CAT:All</span>
          <span className='text-white'><X className='size-3' /></span>
        </div>
        <Button className='h-auto py-1 text-xs font-semibold uppercase'>Clear Filters</Button>
      </div>
    </div>
  )
}

const Table = React.forwardRef<
  HTMLTableElement,
  React.HTMLAttributes<HTMLTableElement>
>(({ className, ...props }, ref) => (
  <div className="relative w-full overflow-auto">
    <Search />
    <table
      ref={ref}
      className={cn("w-full border-none caption-bottom text-sm border-separate border-spacing-y-5", className)}
      {...props}
    />
  </div>
))
Table.displayName = "Table"

const TableHeader = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />
))
TableHeader.displayName = "TableHeader"

const TableBody = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <tbody
    ref={ref}
    className={cn("[&_tr:last-child]:border-0", className)}
    {...props}
  />
))
TableBody.displayName = "TableBody"

const TableFooter = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <tfoot
    ref={ref}
    className={cn(
      "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
      className
    )}
    {...props}
  />
))
TableFooter.displayName = "TableFooter"

const TableHeadRow = React.forwardRef<
  HTMLTableRowElement,
  React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => (
  <tr
    ref={ref}
    className={cn(
      "border-b m-5 rounded-lg text-white transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
      className
    )}
    {...props}
  />
))
const TableRow = React.forwardRef<
  HTMLTableRowElement,
  React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => (
  <tr
    ref={ref}
    className={cn(
      "border-b m-5 bg-white rounded-lg text-black transition-colors hover:text-white hover:bg-muted/50 data-[state=selected]:bg-muted",
      className
    )}
    {...props}
  />
))
TableRow.displayName = "TableRow"

const TableHead = React.forwardRef<
  HTMLTableCellElement,
  React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
  <th
    ref={ref}
    className={cn(
      "h-12 px-4 text-left align-middle font-medium [&:has([role=checkbox])]:pr-0",
      className
    )}
    {...props}
  />
))
TableHead.displayName = "TableHead"

const TableCell = React.forwardRef<
  HTMLTableCellElement,
  React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
  <td
    ref={ref}
    className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)}
    {...props}
  />
))
TableCell.displayName = "TableCell"

const TableCaption = React.forwardRef<
  HTMLTableCaptionElement,
  React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => (
  <caption
    ref={ref}
    className={cn("mt-4 text-sm text-muted-foreground", className)}
    {...props}
  />
))
TableCaption.displayName = "TableCaption"

export {
  Table,
  TableHeader,
  TableBody,
  TableFooter,
  TableHead,
  TableRow,
  TableCell,
  TableCaption,
  TableHeadRow
}
