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

/**
 * 管理员（后台账号）管理 API
 *
 * 后端契约：com.snack.server.module.admin.controller.AdminController
 *   GET    /api/admin/page              → Page<AdminVO>     分页查询
 *   GET    /api/admin/{id}              → AdminVO           详情
 *   POST   /api/admin                   → Long              创建（super_admin）
 *   PUT    /api/admin                   → Void              更新（super_admin）
 *   DELETE /api/admin/{id}              → Void              删除（super_admin）
 *   PUT    /api/admin/{id}/password/reset?newPassword=xxx → Void 重置密码（super_admin）
 *   PUT    /api/admin/{id}/status/{status}              → Void 启停（super_admin）
 *   PUT    /api/admin/password                           → Void 改自己的密码（任意已登录）
 *   PUT    /api/admin/profile                            → Void 更新自己的资料（任意已登录）
 */

export interface AdminItem {
  id: number
  username: string
  nickname: string
  avatar: string
  email: string
  phone: string
  role: 'super_admin' | 'admin' | 'operator'
  status: 0 | 1
  lastLoginTime: string
  lastLoginIp: string
  createTime: string
}

/** 创建/更新提交 */
export interface AdminSavePayload {
  id?: number
  username: string
  /** 创建必填；更新留空表示不修改密码 */
  password?: string
  nickname?: string
  avatar?: string
  email?: string
  phone?: string
  role: AdminItem['role']
  status?: 0 | 1
}

/** 分页查询 */
export interface AdminPageQuery extends PageParams {
  keyword?: string
  role?: string
  status?: 0 | 1
}

export function getAdminPageApi(params: AdminPageQuery) {
  return request<PageResult<AdminItem>>({
    url: '/api/admin/page',
    method: 'GET',
    params
  })
}

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

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

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

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

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

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

/** 更新自己的个人资料（昵称/头像/邮箱/手机号） */
export interface AdminProfilePayload {
  nickname?: string
  avatar?: string
  email?: string
  phone?: string
}

export function updateAdminProfileApi(payload: AdminProfilePayload) {
  return request<void>({
    url: '/api/admin/profile',
    method: 'PUT',
    data: payload
  })
}

/** 修改自己的密码 */
export interface ChangePasswordPayload {
  oldPassword: string
  newPassword: string
}

export function changePasswordApi(payload: ChangePasswordPayload) {
  return request<void>({
    url: '/api/admin/password',
    method: 'PUT',
    data: payload
  })
}
