import { request } from '.'

// 定义商家相关类型
export interface Merchant {
  id: number
  business_name: string
  business_license: string
  legal_representative: string
  contact_person: string
  contact_phone: string
  contact_email: string
  address: string
  business_scope: string
  status: string
  remark: string
  created_at: string
  updated_at: string
}

export interface MerchantQueryParams {
  page?: number
  limit?: number
  business_name?: string
  contact_person?: string
  contact_phone?: string
  status?: string
  start_date?: string
  end_date?: string
}

export interface MerchantCreateData {
  business_name: string
  business_license: string
  legal_representative: string
  contact_person: string
  contact_phone: string
  contact_email: string
  address: string
  business_scope: string
  status?: string
  remark?: string
}

export interface MerchantUpdateData {
  business_name?: string
  business_license?: string
  legal_representative?: string
  contact_person?: string
  contact_phone?: string
  contact_email?: string
  address?: string
  business_scope?: string
  status?: string
  remark?: string
}

// 获取商家列表
export const getMerchants = (params?: MerchantQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { merchants: Merchant[]; pagination: any } }>('/merchants', { params })

// 获取商家详情
export const getMerchant = (id: number) =>
  request.get<{ success: boolean; code: number; message: string; data: { merchant: Merchant } }>(`/merchants/${id}`)

// 创建商家
export const createMerchant = (data: MerchantCreateData) =>
  request.post<{ success: boolean; code: number; message: string; data: { merchant: Merchant } }>('/merchants', data)

// 更新商家
export const updateMerchant = (id: number, data: MerchantUpdateData) =>
  request.put<{ success: boolean; code: number; message: string; data: { merchant: Merchant } }>(`/merchants/${id}`, data)

// 删除商家
export const deleteMerchant = (id: number) =>
  request.delete<{ success: boolean; code: number; message: string }>(`/merchants/${id}`)

// 更新商家状态
export const updateMerchantStatus = (id: number, status: string) =>
  request.put<{ success: boolean; code: number; message: string }>(`/merchants/${id}/status`, { status })

// 审核通过商家
export const approveMerchant = (id: number) =>
  request.put<{ success: boolean; code: number; message: string }>(`/merchants/${id}/approve`)

// 审核拒绝商家
export const rejectMerchant = (id: number, reason: string) =>
  request.put<{ success: boolean; code: number; message: string }>(`/merchants/${id}/reject`, { reason })

// 禁用商家
export const disableMerchant = (id: number) =>
  request.put<{ success: boolean; code: number; message: string }>(`/merchants/${id}/disable`)

// 启用商家
export const enableMerchant = (id: number) =>
  request.put<{ success: boolean; code: number; message: string }>(`/merchants/${id}/enable`)

export default {
  getMerchants,
  getMerchant,
  createMerchant,
  updateMerchant,
  deleteMerchant,
  updateMerchantStatus,
  approveMerchant,
  rejectMerchant,
  disableMerchant,
  enableMerchant
}