import { request } from '.'

// 定义花卉相关类型
export interface Flower {
  id: number
  name: string
  type: string
  variety: string
  price: number
  stock: number
  image: string
  description: string
  merchantId: number
  merchantName: string
  status: string
  createdAt: string
  updatedAt: string
}

export interface FlowerQueryParams {
  page?: number
  limit?: number
  keyword?: string
  type?: string
  status?: string
  merchantId?: number
  startDate?: string
  endDate?: string
}

export interface FlowerCreateData {
  name: string
  type: string
  variety: string
  price: number
  stock: number
  image: string
  description: string
  merchantId: number
  status?: string
}

export interface FlowerUpdateData {
  name?: string
  type?: string
  variety?: string
  price?: number
  stock?: number
  image?: string
  description?: string
  merchantId?: number
  status?: string
}

export interface FlowerSale {
  id: number
  flowerId: number
  flowerName: string
  buyerId: number
  buyerName: string
  quantity: number
  price: number
  totalAmount: number
  status: string
  saleTime: string
  createdAt: string
}

export interface Merchant {
  id: number
  name: string
  contact: string
  phone: string
  address: string
  status: string
  createdAt: string
}

// 获取花卉列表
export const getFlowers = (params?: FlowerQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { flowers: Flower[]; pagination: any } }>('/flowers', { params })

// 获取花卉详情
export const getFlower = (id: number) =>
  request.get<{ success: boolean; code: number; message: string; data: { flower: Flower } }>(`/flowers/${id}`)

// 创建花卉
export const createFlower = (data: FlowerCreateData) =>
  request.post<{ success: boolean; code: number; message: string; data: { flower: Flower } }>('/flowers', data)

// 更新花卉
export const updateFlower = (id: number, data: FlowerUpdateData) =>
  request.put<{ success: boolean; code: number; message: string; data: { flower: Flower } }>(`/flowers/${id}`, data)

// 删除花卉
export const deleteFlower = (id: number) =>
  request.delete<{ success: boolean; code: number; message: string }>(`/flowers/${id}`)

// 获取花卉销售记录
export const getFlowerSales = (params?: any) =>
  request.get<{ success: boolean; code: number; message: string; data: { sales: FlowerSale[]; pagination: any } }>('/flower-sales', { params })

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

export default {
  getFlowers,
  getFlower,
  createFlower,
  updateFlower,
  deleteFlower,
  getFlowerSales,
  getMerchants
}