import Button from "@/components/button";
import Sheet from "@/components/sheet/sheet";
import { useAuth } from "@/contexts/auth-context";
import { useSheet } from "@/hooks/use-sheet";
import { Stack, Text } from "@mantine/core";
import useUnenrollFreeAgent from "../../hooks/use-unenroll-free-agent";

const UnenrollFreeAgent = ({ tournamentId }: {tournamentId: string} ) => {
  const { open, isOpen, toggle } = useSheet();
  const { user } = useAuth();

  const { mutate: unenrollFreeAgent, isPending: isEnrolling } = useUnenrollFreeAgent();
  const handleUnenroll = () => {
    unenrollFreeAgent({ playerId: user!.id, tournamentId }, {
      onSuccess: () => {
        toggle();
      }
    });
  }

  return (
    <>
      <Button variant="subtle" size="sm" onClick={open}>
        Unenroll
      </Button>

      <Sheet title="Are you sure?" opened={isOpen} onChange={toggle}>
        <Stack gap="xs">
          <Text size="md">
            This will remove you from the free agent list.
          </Text>
          <Button onClick={handleUnenroll}>Confirm</Button>
          <Button variant="subtle" color="red" onClick={toggle}>Cancel</Button>
        </Stack>
      </Sheet>
    </>
  );
};

export default UnenrollFreeAgent;
