import http from "../utils/http";
import {
  LoginRequest,
  LoginResponse,
  RegisterRequest,
  UserInfo,
  UserDTO,
} from "@/types/user.ts";
import { UserAddress, UserAddressForm } from "@/types/address.ts";

/**
 * This is an example, please remove if not needed
 */
// export const getUserList = () => {
//   return http({
//     url: "/user/list",
//     method: "get",
//   });
// };
//
// export const insertUser = (data: { account: string; password: string }) => {
//   return http({
//     url: "/user/insert",
//     method: "post",
//     data,
//   });
// };

// 用户登录
export const userLogin = (data: LoginRequest) => {
  return http<LoginResponse>({
    url: "/user/login",
    method: "post",
    data,
  });
};

// 用户注册
export const userRegister = (data: RegisterRequest) => {
  return http({
    url: "/user/register",
    method: "post",
    data,
  });
};

// 获取用户信息
export const getUserProfile = (id: number) => {
  return http<UserInfo>({
    url: `/user/profile/${id}`,
    method: "get",
  });
};

// 获取用户列表（管理员）
export const getUserList = () => {
  return http<UserDTO[]>({
    url: "/user/admin",
    method: "get",
  });
};

// 更新用户信息
export const updateUserProfile = (data: Partial<UserInfo>) => {
  return http({
    url: "/user/update",
    method: "put",
    data,
  });
};

// 获取用户地址列表
export const getAddressList = (userId: number) => {
  return http<UserAddress[]>({
    url: "/user/address",
    method: "get",
    params: { userId },
  });
};

// 添加地址
export const addAddress = (data: UserAddressForm) => {
  return http({
    url: "/user/address",
    method: "post",
    data,
  });
};

// 更新地址
export const updateAddress = (data: UserAddressForm) => {
  return http({
    url: "/user/address",
    method: "put",
    data,
  });
};

// 删除地址
export const deleteAddress = (id: number, userId: number) => {
  return http({
    url: `/user/address/${id}`,
    method: "delete",
    params: { userId },
  });
};

// 设置默认地址
export const setDefaultAddress = (id: number, userId: number) => {
  return http({
    url: `/user/address/${id}/default`,
    method: "put",
    params: { userId },
  });
};
