"use client"

import { useState } from "react"
import { Row } from "@tanstack/react-table"
import { MoreHorizontal, Pencil, Trash, Undo } from "lucide-react"
import { Button } from "@/components/ui/button"
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Hero } from "@/types/hero"
import { heroService } from "@/services/heroService"
import { toast } from "sonner"
import { HeroDialog } from "./hero-dialog"
import Swal from "sweetalert2"

interface DataTableRowActionsProps<TData> {
    row: Row<TData>
    onSuccess: () => void
}

export function HeroRowActions<TData extends Hero>({
    row,
    onSuccess,
}: DataTableRowActionsProps<TData>) {
    const hero = row.original
    const [showEditDialog, setShowEditDialog] = useState(false)

    const handleDelete = async () => {
        const result = await Swal.fire({
            title: "Emin misiniz?",
            text: "Bu hero silinecek! (Geri alabilirsiniz)",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "Evet, sil",
            cancelButtonText: "İptal",
        })

        if (result.isConfirmed) {
            try {
                await heroService.deleteHero(hero.ID)
                toast.success("Hero başarıyla silindi")
                onSuccess()
            } catch {
                toast.error("Silme işlemi başarısız")
            }
        }
    }

    const handleRestore = async () => {
        const result = await Swal.fire({
            title: "Geri yüklemek istiyor musunuz?",
            text: "Bu hero tekrar aktif listeye alınacak.",
            icon: "question",
            showCancelButton: true,
            confirmButtonText: "Evet, geri yükle",
            cancelButtonText: "İptal",
        })

        if (result.isConfirmed) {
            try {
                await heroService.restoreHero(hero.ID)
                toast.success("Hero başarıyla geri yüklendi")
                onSuccess()
            } catch {
                toast.error("Geri yükleme işlemi başarısız")
            }
        }
    }

    const isDeleted = !!hero.DeletedAt

    return (
        <>
            <DropdownMenu>
                <DropdownMenuTrigger asChild>
                    <Button variant="ghost" className="h-8 w-8 p-0">
                        <span className="sr-only">Menüyü aç</span>
                        <MoreHorizontal className="h-4 w-4" />
                    </Button>
                </DropdownMenuTrigger>
                <DropdownMenuContent align="end">
                    <DropdownMenuLabel>İşlemler</DropdownMenuLabel>
                    <DropdownMenuItem
                        onClick={() => navigator.clipboard.writeText(hero.ID.toString())}
                    >
                        Hero ID Kopyala
                    </DropdownMenuItem>
                    <DropdownMenuSeparator />

                    {!isDeleted && (
                        <>
                            <DropdownMenuItem onClick={() => setShowEditDialog(true)}>
                                <Pencil className="mr-2 h-3.5 w-3.5" /> Düzenle
                            </DropdownMenuItem>
                            <DropdownMenuItem onClick={handleDelete} className="text-red-600">
                                <Trash className="mr-2 h-3.5 w-3.5" /> Sil
                            </DropdownMenuItem>
                        </>
                    )}

                    {isDeleted && (
                        <DropdownMenuItem onClick={handleRestore} className="text-green-600">
                            <Undo className="mr-2 h-3.5 w-3.5" /> Geri Yükle
                        </DropdownMenuItem>
                    )}
                </DropdownMenuContent>
            </DropdownMenu>

            <HeroDialog
                open={showEditDialog}
                onOpenChange={setShowEditDialog}
                hero={hero}
                onSuccess={onSuccess}
            />
        </>
    )
}
