import { request } from '@/utils/request'
import type { PageParams, PageResult } from '@/types/api'

/**
 * 用户（C 端）管理 API（管理端调用）
 *
 * 后端契约：com.snack.server.module.user.controller.UserAdminController
 *   GET    /api/admin/user/page                       → Page<UserVO>
 *   GET    /api/admin/user/{id}                       → UserVO
 *   POST   /api/admin/user                            → Long
 *   PUT    /api/admin/user                            → Void
 *   DELETE /api/admin/user/{id}                       → Void
 *   PUT    /api/admin/user/{id}/password/reset?newPassword=  → Void
 *   PUT    /api/admin/user/{id}/status/{status}       → Void
 *   GET    /api/admin/user/{id}/orders?current&size   → Page<OrderDetailVO>
 */

export interface UserItem {
  id: number
  username: string
  nickname: string
  avatar: string
  phone: string
  gender: 0 | 1 | 2
  birthday: string
  status: 0 | 1
  lastLoginTime: string
  lastLoginIp: string
  createTime: string
}

export interface UserSavePayload {
  id?: number
  username: string
  /** 创建必填；更新留空 = 不修改 */
  password?: string
  nickname?: string
  avatar?: string
  phone?: string
  gender?: 0 | 1 | 2
  birthday?: string
  status?: 0 | 1
}

export interface UserPageQuery extends PageParams {
  keyword?: string
  status?: 0 | 1
}

/** 历史订单（详情弹窗里展示） */
export interface UserOrderItem {
  id: number
  orderNo: string
  userId: number
  totalAmount: number
  freightAmount: number
  discountAmount: number
  couponAmount: number
  payAmount: number
  status: 0 | 1 | 2 | 3 | 4 | 5
  statusText: string
  statusTagType: string
  receiverName: string
  receiverPhone: string
  receiverAddress: string
  trackingCompany: string
  trackingNo: string
  payTime: string
  deliverTime: string
  receiveTime: string
  createTime: string
  payChannel: string
  payChannelText: string
}

export function getUserPageApi(params: UserPageQuery) {
  return request<PageResult<UserItem>>({
    url: '/api/admin/user/page',
    method: 'GET',
    params
  })
}

export function getUserDetailApi(id: number) {
  return request<UserItem>({
    url: `/api/admin/user/${id}`,
    method: 'GET'
  })
}

export function createUserApi(payload: UserSavePayload) {
  return request<number>({
    url: '/api/admin/user',
    method: 'POST',
    data: payload
  })
}

export function updateUserApi(payload: UserSavePayload) {
  return request<void>({
    url: '/api/admin/user',
    method: 'PUT',
    data: payload
  })
}

export function deleteUserApi(id: number) {
  return request<void>({
    url: `/api/admin/user/${id}`,
    method: 'DELETE'
  })
}

export function updateUserStatusApi(id: number, status: 0 | 1) {
  return request<void>({
    url: `/api/admin/user/${id}/status/${status}`,
    method: 'PUT'
  })
}

export function resetUserPasswordApi(id: number, newPassword: string) {
  return request<void>({
    url: `/api/admin/user/${id}/password/reset`,
    method: 'PUT',
    params: { newPassword }
  })
}

export function getUserOrdersApi(id: number, params: { current: number; size: number }) {
  return request<PageResult<UserOrderItem>>({
    url: `/api/admin/user/${id}/orders`,
    method: 'GET',
    params
  })
}
