import { View, Text } from "react-native";
import React, { useGlobal } from "reactn";
import AppActionSheet from "../AppActionSheet";
import BorderGradientButton from "../BorderGradientButton";
import GradientButton from "../GradientButton";
import { Palette } from "../../styles";
import { FONT_FAMILY } from "../../styles/Fonts";
import { SheetManager } from "react-native-actions-sheet";
import alert from "../Alert";
import firebase, { usersRef } from "../../config/firebase";
import { reset } from "../../navigation/NavigationService";
import { Routes } from "../../navigation";

const DeleteAccountModal = () => {
  const [currentUID] = useGlobal("currentUID");
  const [, setTooltip] = useGlobal("_tooltip");

  const onClose = async () => {
    await SheetManager.hide("DeleteAccount");
  };

  const confirmDelete = () => {
    alert(
      "Êtes-vous sûr ?",
      "Cette action supprimera votre profil.",
      [
        { text: "Annuler", style: "cancel", onPress: () => {} },
        {
          text: "Confirmer",
          onPress: () => {
            alert(
              "Confirmation",
              "Dernière vérification: supprimer définitivement votre profil ?",
              [
                { text: "Non", style: "cancel", onPress: () => {} },
                {
                  text: "Oui, supprimer",
                  onPress: async () => {
                    try {
                      if (!currentUID) return;
                      await usersRef.doc(currentUID).delete();
                      // Sign out and reset navigation to Login
                      await firebase.auth().signOut();
                      reset({ index: 0, routes: [{ name: Routes.Login }] });
                      setTooltip({ type: "success", text: "Profil supprimé" });
                    } catch (e) {
                      setTooltip({
                        type: "error",
                        text: e?.message || "Suppression impossible",
                      });
                    } finally {
                      onClose();
                    }
                  },
                },
              ],
              { cancelable: true },
            );
          },
        },
      ],
      { cancelable: true },
    );
  };

  return (
    <AppActionSheet id="DeleteAccount">
      <View style={{ gap: 20 }}>
        <View style={{ gap: 2 }}>
          <Text
            style={{
              fontSize: 20,
              color: Palette.white,
              fontFamily: FONT_FAMILY.HelveticaNeueMedium,
              textAlign: "center",
            }}
          >
            Supprimer mon compte
          </Text>
          <Text
            style={{
              fontSize: 16,
              color: Palette.white,
              fontFamily: FONT_FAMILY.HelveticaNeueRegular,
              textAlign: "center",
            }}
          >
            Es-tu sur de vouloir supprimer ton compte?{"\n"}Attention, cette
            action est irréversible!
          </Text>
        </View>
        <View style={{ width: "80%", alignSelf: "center", gap: 12 }}>
          <BorderGradientButton title="Oui, supprimer" onPress={confirmDelete} />
          <GradientButton title="Non, annuler" onPress={onClose} />
        </View>
      </View>
    </AppActionSheet>
  );
};

export default DeleteAccountModal;
