import axiosClient from "./axiosClient";
import {
  User,
  AiSettings,
  TwoFactorSetup,
  TwoFactorStatus,
  TwoFactorEnableResponse,
  TwoFactorDisableResponse,
  TwoFactorVerifyResponse,
} from "../types/user";

export const userApi = {
  getProfile: async (): Promise<User> => {
    const { data } = await axiosClient.get<User>("/user");
    return data;
  },

  updateProfile: async (
    profile: Omit<Partial<User>, 'show_edit_date' | 'colored_icons' | 'floating_toolbar_enabled' | 'is_public_profile'> & {
      currentPassword?: string;
      newPassword?: string;
      accent_color?: string;
      show_edit_date?: boolean;
      colored_icons?: boolean;
      floating_toolbar_enabled?: boolean;
      is_public_profile?: boolean;
    }
  ) => {
    const { data } = await axiosClient.put("/user/profile", profile);
    return data;
  },

  uploadAvatar: async (file: File) => {
    const formData = new FormData();
    formData.append("avatar", file);

    const { data } = await axiosClient.post("/user/avatar", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    });
    return data;
  },

  deleteAvatar: async () => {
    await axiosClient.delete("/user/avatar");
  },

  deleteAccount: async (password: string) => {
    const { data } = await axiosClient.delete("/user/delete-account", {
      data: { password },
    });
    return data;
  },

  getAiSettings: async (): Promise<AiSettings> => {
    const { data } = await axiosClient.get<AiSettings>("/user/ai-settings");
    return data;
  },

  updateAiSettings: async (settings: Partial<AiSettings>) => {
    const { data } = await axiosClient.put("/user/ai-settings", settings);
    return data;
  },

  // 2FA методы
  get2FAStatus: async (): Promise<TwoFactorStatus> => {
    const { data } = await axiosClient.get<TwoFactorStatus>("/user/2fa/status");
    return data;
  },

  setup2FA: async (): Promise<TwoFactorSetup> => {
    const { data } = await axiosClient.get<TwoFactorSetup>("/user/2fa/setup");
    return data;
  },

  enable2FA: async (
    secret: string,
    token: string
  ): Promise<TwoFactorEnableResponse> => {
    const { data } = await axiosClient.post<TwoFactorEnableResponse>(
      "/user/2fa/enable",
      { secret, token }
    );
    return data;
  },

  disable2FA: async (password: string): Promise<TwoFactorDisableResponse> => {
    const { data } = await axiosClient.post<TwoFactorDisableResponse>(
      "/user/2fa/disable",
      { password }
    );
    return data;
  },

  verify2FA: async (
    username: string,
    token: string
  ): Promise<TwoFactorVerifyResponse> => {
    const { data } = await axiosClient.post<TwoFactorVerifyResponse>(
      "/user/2fa/verify",
      { username, token }
    );
    return data;
  },

  generateBackupCodes: async (): Promise<{ success: boolean; backupCodes: string[] }> => {
    const { data } = await axiosClient.post<{ success: boolean; backupCodes: string[] }>(
      "/user/2fa/backup-codes"
    );
    return data;
  },

  // Публичные методы (не требуют аутентификации)
  getPublicProfile: async (username: string): Promise<{ username: string; avatar?: string }> => {
    const { data } = await axiosClient.get<{ username: string; avatar?: string }>(`/public/user/${username}`);
    return data;
  },
};
