/**
 * Copyright (c) 2023-present Pi Dash Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { useCallback } from "react";
import { useTheme } from "next-themes";
import { Calendar, Earth, Languages, Palette } from "lucide-react";
// pi dash imports
import { useTranslation } from "@pi-dash/i18n";
import { setToast, TOAST_TYPE } from "@pi-dash/propel/toast";
import type { EStartOfTheWeek, TUserProfile } from "@pi-dash/types";
// components
import type { TPowerKCommandConfig } from "@/components/power-k/core/types";
// hooks
import { useUser, useUserProfile } from "@/hooks/store/user";

/**
 * Preferences commands - Preferences related commands
 */
export const usePowerKPreferencesCommands = (): TPowerKCommandConfig[] => {
  // store hooks
  const { setTheme } = useTheme();
  const { updateCurrentUser } = useUser();
  const { updateUserProfile, updateUserTheme } = useUserProfile();
  // translation
  const { t } = useTranslation();

  const handleUpdateTheme = useCallback(
    async (newTheme: string) => {
      setTheme(newTheme);
      return updateUserTheme({ theme: newTheme })
        .then(() => {
          setToast({
            type: TOAST_TYPE.SUCCESS,
            title: "Theme updated",
            message: "Reloading to apply changes...",
          });
          // reload the page after showing the toast
          window.location.reload();
          return;
        })
        .catch(() => {
          setToast({
            type: TOAST_TYPE.ERROR,
            title: t("Error!"),
            message: t("Failed to update theme. Please try again."),
          });
          return;
        });
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [setTheme, updateUserTheme]
  );

  const handleUpdateTimezone = useCallback(
    (value: string) => {
      updateCurrentUser({ user_timezone: value })
        .then(() => {
          setToast({
            type: TOAST_TYPE.SUCCESS,
            title: t("Success!"),
            message: t("Timezone updated successfully."),
          });
          return;
        })
        .catch(() => {
          setToast({
            type: TOAST_TYPE.ERROR,
            title: t("Error!"),
            message: t("Failed to update timezone. Please try again."),
          });
          return;
        });
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [updateCurrentUser]
  );

  const handleUpdateUserProfile = useCallback(
    (payload: Partial<TUserProfile>) => {
      updateUserProfile(payload)
        .then(() => {
          setToast({
            type: TOAST_TYPE.SUCCESS,
            title: t("Success!"),
            message: t("Preferences updated successfully."),
          });
          return;
        })
        .catch(() => {
          setToast({
            type: TOAST_TYPE.ERROR,
            title: t("Error!"),
            message: t("Failed to update preferences. Please try again."),
          });
          return;
        });
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [updateUserProfile]
  );

  return [
    {
      id: "update_interface_theme",
      group: "preferences",
      type: "change-page",
      page: "update-theme",
      i18n_title: "Change interface theme",
      icon: Palette,
      onSelect: (data) => {
        const theme = data as string;
        void handleUpdateTheme(theme);
      },
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
    {
      id: "update_timezone",
      group: "preferences",
      page: "update-timezone",
      type: "change-page",
      i18n_title: "Change timezone",
      icon: Earth,
      onSelect: (data) => {
        const timezone = data as string;
        handleUpdateTimezone(timezone);
      },
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
    {
      id: "update_start_of_week",
      group: "preferences",
      page: "update-start-of-week",
      type: "change-page",
      i18n_title: "Change first day of week",
      icon: Calendar,
      onSelect: (data) => {
        const startOfWeek = data as EStartOfTheWeek;
        handleUpdateUserProfile({ start_of_the_week: startOfWeek });
      },
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
    {
      id: "update_interface_language",
      group: "preferences",
      page: "update-language",
      type: "change-page",
      i18n_title: "Change interface language",
      icon: Languages,
      onSelect: (data) => {
        const language = data as string;
        handleUpdateUserProfile({ language });
      },
      isEnabled: () => true,
      isVisible: () => true,
      closeOnSelect: true,
    },
  ];
};
