import { post, type PageResult, type PageParams } from '@/lib/request'
import { USE_MOCK, delay, paginate, mockId, mockResponse } from '@/mock'
import { mockWikis, mockWikiClasses, type PlantWiki, type PlantWikiClass } from '@/mock/plant/wiki'
import { mockBanners, type Banner } from '@/mock/plant/banner'
import { mockTopics, mockPosts, type Topic, type Post } from '@/mock/plant/community'
import { mockExchangeItems, mockExchangeOrders, type ExchangeItem, type ExchangeOrder } from '@/mock/plant/exchange'

export type { PlantWiki, PlantWikiClass, Banner, Topic, Post, ExchangeItem, ExchangeOrder }

type R<T> = { code: number; data: T; msg: string }

// ==================== Wiki ====================
export async function getWikiList(data: PageParams & { classId?: string }) {
  if (USE_MOCK) {
    await delay(); let f = [...mockWikis]
    if (data.classId) f = f.filter(w => w.classId === data.classId)
    if (data.keyword) f = f.filter(w => w.title.includes(data.keyword!))
    return mockResponse(paginate(f, data.current, data.pageSize))
  }
  return post<R<PageResult<PlantWiki>>>('/plant/wiki/list', data)
}
export async function saveWiki(data: Partial<PlantWiki>) {
  if (USE_MOCK) {
    await delay()
    mockWikis.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as PlantWiki)
    return mockResponse(null)
  }
  return post<R<null>>('/plant/wiki/save', data)
}
export async function updateWiki(data: Partial<PlantWiki>) {
  if (USE_MOCK) { await delay(); const i = mockWikis.findIndex(w => w.id === data.id); if (i >= 0) Object.assign(mockWikis[i], data); return mockResponse(null) }
  return post<R<null>>('/plant/wiki/update', data)
}
export async function deleteWiki(id: string) {
  if (USE_MOCK) { await delay(); const i = mockWikis.findIndex(w => w.id === id); if (i >= 0) mockWikis.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/wiki/delete', { id })
}

// ==================== Wiki Class ====================
export async function getWikiClassList() {
  if (USE_MOCK) { await delay(); return mockResponse(mockWikiClasses) }
  return post<R<PlantWikiClass[]>>('/plant/wikiClass/list', {})
}
export async function saveWikiClass(data: Partial<PlantWikiClass>) {
  if (USE_MOCK) {
    await delay()
    mockWikiClasses.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as PlantWikiClass)
    return mockResponse(null)
  }
  return post<R<null>>('/plant/wikiClass/save', data)
}
export async function deleteWikiClass(id: string) {
  if (USE_MOCK) { await delay(); const i = mockWikiClasses.findIndex(c => c.id === id); if (i >= 0) mockWikiClasses.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/wikiClass/delete', { id })
}

// ==================== Banner ====================
export async function getBannerList(data: PageParams & { title?: string; isActive?: number }) {
  if (USE_MOCK) {
    await delay(); let f = [...mockBanners]
    if (data.keyword) f = f.filter(b => b.title.includes(data.keyword!))
    return mockResponse(paginate(f, data.current, data.pageSize))
  }
  return post<R<PageResult<Banner>>>('/plant/banner/list', data)
}
export async function saveBanner(data: Partial<Banner>) {
  if (USE_MOCK) { await delay(); mockBanners.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as Banner); return mockResponse(null) }
  return post<R<null>>('/plant/banner/save', data)
}
export async function updateBanner(data: Partial<Banner>) {
  if (USE_MOCK) { await delay(); const i = mockBanners.findIndex(b => b.id === data.id); if (i >= 0) Object.assign(mockBanners[i], data); return mockResponse(null) }
  return post<R<null>>('/plant/banner/update', data)
}
export async function deleteBanner(id: string) {
  if (USE_MOCK) { await delay(); const i = mockBanners.findIndex(b => b.id === id); if (i >= 0) mockBanners.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/banner/delete', { id })
}

// ==================== Topic ====================
export async function getTopicList(data: PageParams) {
  if (USE_MOCK) {
    await delay(); let f = [...mockTopics]
    if (data.keyword) f = f.filter(t => t.title.includes(data.keyword!))
    return mockResponse(paginate(f, data.current, data.pageSize))
  }
  return post<R<PageResult<Topic>>>('/plant/topic/list', data)
}
export async function saveTopic(data: Partial<Topic>) {
  if (USE_MOCK) { await delay(); mockTopics.push({ ...data, id: mockId(), postCount: 0, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as Topic); return mockResponse(null) }
  return post<R<null>>('/plant/topic/save', data)
}
export async function deleteTopic(id: string) {
  if (USE_MOCK) { await delay(); const i = mockTopics.findIndex(t => t.id === id); if (i >= 0) mockTopics.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/topic/delete', { id })
}

// ==================== Post ====================
export async function getPostList(data: PageParams & { topicId?: string }) {
  if (USE_MOCK) {
    await delay(); let f = [...mockPosts]
    if (data.topicId) f = f.filter(p => p.topicId === data.topicId)
    if (data.keyword) f = f.filter(p => p.title.includes(data.keyword!))
    return mockResponse(paginate(f, data.current, data.pageSize))
  }
  return post<R<PageResult<Post>>>('/plant/post/list', data)
}
export async function deletePost(id: string) {
  if (USE_MOCK) { await delay(); const i = mockPosts.findIndex(p => p.id === id); if (i >= 0) mockPosts.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/post/delete', { id })
}

// ==================== Exchange ====================
export async function getExchangeItemList(data: PageParams) {
  if (USE_MOCK) {
    await delay(); let f = [...mockExchangeItems]
    if (data.keyword) f = f.filter(e => e.name.includes(data.keyword!))
    return mockResponse(paginate(f, data.current, data.pageSize))
  }
  return post<R<PageResult<ExchangeItem>>>('/plant/exchange/itemList', data)
}
export async function saveExchangeItem(data: Partial<ExchangeItem>) {
  if (USE_MOCK) { await delay(); mockExchangeItems.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as ExchangeItem); return mockResponse(null) }
  return post<R<null>>('/plant/exchange/saveItem', data)
}
export async function deleteExchangeItem(id: string) {
  if (USE_MOCK) { await delay(); const i = mockExchangeItems.findIndex(e => e.id === id); if (i >= 0) mockExchangeItems.splice(i, 1); return mockResponse(null) }
  return post<R<null>>('/plant/exchange/deleteItem', { id })
}
export async function getExchangeOrderList(data: PageParams) {
  if (USE_MOCK) { await delay(); return mockResponse(paginate([...mockExchangeOrders], data.current, data.pageSize)) }
  return post<R<PageResult<ExchangeOrder>>>('/plant/exchange/orderList', data)
}
