import request from '@/utils/request';

export function loginByPassword(username: string, password: string) {
  return request.post('/api/admin/auth/login', { username, password });
}

export function getAdminProfile() {
  return request.get('/api/admin/auth/profile');
}

export function changeAdminPassword(data: { oldPassword: string; newPassword: string }) {
  return request.put('/api/admin/auth/password', data);
}

// 商家管理
export function getMerchantList(params: any) {
  return request.get('/api/admin/merchants', { params });
}

export function getMerchantDetail(id: number) {
  return request.get(`/api/admin/merchants/${id}`);
}

export function approveMerchant(id: number) {
  return request.put(`/api/admin/merchants/${id}/approve`);
}

export function rejectMerchant(id: number, reason: string) {
  return request.put(`/api/admin/merchants/${id}/reject`, { reason });
}

export function freezeMerchant(id: number) {
  return request.put(`/api/admin/merchants/${id}/freeze`);
}

export function unfreezeMerchant(id: number) {
  return request.put(`/api/admin/merchants/${id}/unfreeze`);
}

// 用户管理
export function getUserList(params: any) {
  return request.get('/api/admin/users', { params });
}

export function freezeUser(id: number) {
  return request.put(`/api/admin/users/${id}/freeze`);
}

export function unfreezeUser(id: number) {
  return request.put(`/api/admin/users/${id}/unfreeze`);
}

// 订单管理
export function getOrderList(params: any) {
  return request.get('/api/admin/orders', { params });
}

export function getOrderDetail(id: number) {
  return request.get(`/api/admin/orders/${id}`);
}

// 统计数据
export function getPlatformStatistics() {
  return request.get('/api/admin/finance/reports/overview');
}

export function getOrderTrend(params: { startDate: string; endDate: string }) {
  return request.get('/api/admin/finance/reports/trend', { params });
}

// 管理员管理
export interface Admin {
  id: number;
  username: string;
  name: string;
  phone?: string;
  email?: string;
  role: 'super_admin' | 'admin' | 'operator';
  status: 'active' | 'frozen';
  lastLoginAt?: string;
  lastLoginIp?: string;
  createdAt: string;
  updatedAt: string;
}

export interface CreateAdminParams {
  username: string;
  password: string;
  name: string;
  phone?: string;
  email?: string;
  role: 'super_admin' | 'admin' | 'operator';
}

export interface UpdateAdminParams {
  name?: string;
  phone?: string;
  email?: string;
  role?: 'super_admin' | 'admin' | 'operator';
  status?: 'active' | 'frozen';
}

export interface QueryAdminParams {
  username?: string;
  name?: string;
  role?: 'super_admin' | 'admin' | 'operator';
  status?: 'active' | 'frozen';
  page?: number;
  pageSize?: number;
}

export function getAdminList(params: QueryAdminParams) {
  return request.get('/api/admin/admins', { params });
}

export function getAdminById(id: number) {
  return request.get(`/api/admin/admins/${id}`);
}

export function createAdmin(data: CreateAdminParams) {
  return request.post('/api/admin/admins', data);
}

export function updateAdmin(id: number, data: UpdateAdminParams) {
  return request.put(`/api/admin/admins/${id}`, data);
}

export function updateAdminPassword(id: number, password: string) {
  return request.put(`/api/admin/admins/${id}/password`, { password });
}

export function toggleAdminStatus(id: number) {
  return request.put(`/api/admin/admins/${id}/toggle-status`);
}

export function deleteAdmin(id: number) {
  return request.delete(`/api/admin/admins/${id}`);
}
