import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { SettingsOptionCardContent } from '@/settings/components/SettingsOptionCardContent';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import styled from '@emotion/styled';
import { useRecoilState } from 'recoil';
import { IconLink, Toggle, Card } from 'twenty-ui';
import { useUpdateWorkspaceMutation } from '~/generated/graphql';

const StyledToggle = styled(Toggle)`
  margin-left: auto;
`;

export const SettingsSecurityOptionsList = () => {
  const { enqueueSnackBar } = useSnackBar();

  const [currentWorkspace, setCurrentWorkspace] = useRecoilState(
    currentWorkspaceState,
  );

  const [updateWorkspace] = useUpdateWorkspaceMutation();

  const handleChange = async (value: boolean) => {
    try {
      if (!currentWorkspace?.id) {
        throw new Error('User is not logged in');
      }
      await updateWorkspace({
        variables: {
          input: {
            isPublicInviteLinkEnabled: value,
          },
        },
      });
      setCurrentWorkspace({
        ...currentWorkspace,
        isPublicInviteLinkEnabled: value,
      });
    } catch (err: any) {
      enqueueSnackBar(err?.message, {
        variant: SnackBarVariant.Error,
      });
    }
  };

  return (
    <Card>
      <SettingsOptionCardContent
        Icon={IconLink}
        title="Invite by Link"
        description="Allow the invitation of new users by sharing an invite link."
        onClick={() =>
          handleChange(!currentWorkspace?.isPublicInviteLinkEnabled)
        }
      >
        <StyledToggle value={currentWorkspace?.isPublicInviteLinkEnabled} />
      </SettingsOptionCardContent>
    </Card>
  );
};
