import { DotsHorizontalIcon } from '@radix-ui/react-icons'
import { type Row } from '@tanstack/react-table'
import { Trash2, Eye, EyeOff } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'

import { useReferralCodes } from './referral-codes-provider'
import { type Referralcode } from '@/types/referral-codes.types'

interface Props {
  row: Row<Referralcode>
}

export function DataTableRowActions({ row }: Props) {
  const { setOpen, setCurrentRow } = useReferralCodes()

  const referralCode = row.original

  const handleChangeState = () => {
    setCurrentRow(referralCode)
    setOpen('update')
  }

  const handleView = () => {
    setCurrentRow(referralCode)
    setOpen('view')
  }

  const handleDelete = () => {
    setCurrentRow(referralCode)
    setOpen('delete')
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant='ghost' className='flex h-8 w-8 p-0'>
          <DotsHorizontalIcon className='h-4 w-4' />
        </Button>
      </DropdownMenuTrigger>

      <DropdownMenuContent align='end'>
        <DropdownMenuItem onClick={handleView}>
          مشاهده
          <DropdownMenuShortcut>
            <Eye size={16} />
          </DropdownMenuShortcut>
        </DropdownMenuItem>

        <DropdownMenuItem onClick={handleChangeState}>
          تغییر وضعیت
          <DropdownMenuShortcut>
            <EyeOff size={16} />
          </DropdownMenuShortcut>
        </DropdownMenuItem>

        <DropdownMenuSeparator />

        <DropdownMenuItem onClick={handleDelete} className='text-red-600'>
          حذف
          <DropdownMenuShortcut>
            <Trash2 size={16} color='red' />
          </DropdownMenuShortcut>
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}
