import { archestraApiSdk, type archestraApiTypes } from "@shared";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { useFeature } from "@/lib/config/config.query";
import { handleApiError } from "@/lib/utils";

const {
  getTeamVaultFolder,
  setTeamVaultFolder,
  deleteTeamVaultFolder,
  checkTeamVaultFolderConnectivity,
  listTeamVaultFolderSecrets,
  getTeamVaultSecretKeys,
} = archestraApiSdk;

export type TeamVaultFolder =
  archestraApiTypes.GetTeamVaultFolderResponses["200"];
export type VaultSecretListItem =
  archestraApiTypes.ListTeamVaultFolderSecretsResponses["200"][number];

/**
 * Hook to get a team's Vault folder configuration
 */
export function useTeamVaultFolder(teamId: string | null) {
  const byosEnabled = useFeature("byosEnabled");
  return useQuery({
    queryKey: ["team-vault-folder", teamId],
    queryFn: async () => {
      if (!teamId) return null;
      const { data, error } = await getTeamVaultFolder({
        path: { teamId },
      });
      if (error) {
        handleApiError(error);
        return null;
      }
      return data;
    },
    enabled: byosEnabled && !!teamId,
    throwOnError: false, // Handle errors gracefully in the component instead of crashing
  });
}

/**
 * Hook to set or update a team's Vault folder path
 */
export function useSetTeamVaultFolder() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async ({
      teamId,
      vaultPath,
    }: {
      teamId: string;
      vaultPath: string;
    }) => {
      const { data, error } = await setTeamVaultFolder({
        path: { teamId },
        body: { vaultPath },
      });
      if (error) {
        handleApiError(error);
        return null;
      }
      return data;
    },
    onSuccess: (data, variables) => {
      if (!data) return;
      toast.success("Vault folder configured successfully");
      queryClient.invalidateQueries({
        queryKey: ["team-vault-folder", variables.teamId],
      });
    },
  });
}

/**
 * Hook to delete a team's Vault folder mapping
 */
export function useDeleteTeamVaultFolder() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async (teamId: string) => {
      const { data, error } = await deleteTeamVaultFolder({
        path: { teamId },
      });
      if (error) {
        handleApiError(error);
        return null;
      }
      return data;
    },
    onSuccess: (data, teamId) => {
      if (!data) return;
      toast.success("Vault folder removed successfully");
      queryClient.invalidateQueries({
        queryKey: ["team-vault-folder", teamId],
      });
    },
  });
}

/**
 * Hook to check connectivity to a team's Vault folder
 */
export function useCheckTeamVaultFolderConnectivity() {
  return useMutation({
    mutationFn: async ({
      teamId,
      vaultPath,
    }: {
      teamId: string;
      vaultPath?: string;
    }) => {
      const { data, error } = await checkTeamVaultFolderConnectivity({
        path: { teamId },
        body: { vaultPath },
      });
      if (error) {
        handleApiError(error);
        return null;
      }
      return data;
    },
  });
}

/**
 * Hook to list secrets in a team's Vault folder
 */
export function useTeamVaultFolderSecrets(teamId: string | null) {
  const byosEnabled = useFeature("byosEnabled");
  return useQuery({
    queryKey: ["team-vault-folder-secrets", teamId],
    queryFn: async () => {
      if (!teamId) return [];
      const { data, error } = await listTeamVaultFolderSecrets({
        path: { teamId },
      });
      if (error) {
        handleApiError(error);
        return null;
      }
      return data ?? [];
    },
    enabled: byosEnabled && !!teamId,
    throwOnError: false, // Handle errors gracefully in the component instead of crashing
  });
}

/**
 * Hook to get keys of a specific secret in a team's Vault folder
 */
export function useTeamVaultSecretKeys(
  teamId: string | null,
  secretPath: string | null,
) {
  const byosEnabled = useFeature("byosEnabled");
  return useQuery({
    queryKey: ["team-vault-secret-keys", teamId, secretPath],
    queryFn: async () => {
      if (!teamId || !secretPath) return { keys: [] };
      const { data, error } = await getTeamVaultSecretKeys({
        path: { teamId },
        body: { secretPath },
      });
      if (error) {
        handleApiError(error);
        return { keys: [] };
      }
      return data ?? { keys: [] };
    },
    enabled: byosEnabled && !!teamId && !!secretPath,
    retry: false, // Don't retry on 403/404 errors
    throwOnError: false, // Handle errors gracefully in the component instead of crashing
  });
}
