import { post, type PageResult, type PageParams } from '@/lib/request'
import { USE_MOCK, delay, paginate, mockId, mockResponse } from '@/mock'
import { mockRadioCategories, mockRadioChannels, mockRadioPrograms } from '@/mock/radio/channels'
import { mockSubscriptions } from '@/mock/radio/subscriptions'
import type { RadioCategory, RadioChannel, RadioProgram, RadioSubscription } from '@/mock/radio/channels'

export type { RadioCategory, RadioChannel, RadioProgram, RadioSubscription }

// ==================== Category ====================
export async function getRadioCategoryList() {
  if (USE_MOCK) { await delay(); return mockResponse([...mockRadioCategories]) }
  return post<{ code: number; data: RadioCategory[]; msg: string }>('/radio/category/list', {})
}
export async function saveRadioCategory(data: Partial<RadioCategory>) {
  if (USE_MOCK) { await delay(); mockRadioCategories.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as RadioCategory); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/category/save', data)
}
export async function deleteRadioCategory(id: string) {
  if (USE_MOCK) { await delay(); const i = mockRadioCategories.findIndex(c => c.id === id); if (i >= 0) mockRadioCategories.splice(i, 1); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/category/delete', { id })
}

// ==================== Channel ====================
export async function getRadioChannelList(data: PageParams & { categoryId?: string }) {
  if (USE_MOCK) { await delay(); let f = [...mockRadioChannels]; if (data.categoryId) f = f.filter(c => c.categoryId === data.categoryId); if (data.keyword) f = f.filter(c => c.name.includes(data.keyword!)); return mockResponse(paginate(f, data.current, data.pageSize)) }
  return post<{ code: number; data: PageResult<RadioChannel>; msg: string }>('/radio/channel/list', data)
}
export async function saveRadioChannel(data: Partial<RadioChannel>) {
  if (USE_MOCK) { await delay(); mockRadioChannels.push({ ...data, id: mockId(), createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as RadioChannel); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/channel/save', data)
}
export async function updateRadioChannel(data: Partial<RadioChannel>) {
  if (USE_MOCK) { await delay(); const i = mockRadioChannels.findIndex(c => c.id === data.id); if (i >= 0) Object.assign(mockRadioChannels[i], data); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/channel/update', data)
}
export async function deleteRadioChannel(id: string) {
  if (USE_MOCK) { await delay(); const i = mockRadioChannels.findIndex(c => c.id === id); if (i >= 0) mockRadioChannels.splice(i, 1); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/channel/delete', { id })
}

// ==================== Program ====================
export async function getRadioProgramList(data: PageParams & { channelId?: string }) {
  if (USE_MOCK) { await delay(); let f = [...mockRadioPrograms]; if (data.channelId) f = f.filter(p => p.channelId === data.channelId); if (data.keyword) f = f.filter(p => p.title.includes(data.keyword!)); return mockResponse(paginate(f, data.current, data.pageSize)) }
  return post<{ code: number; data: PageResult<RadioProgram>; msg: string }>('/radio/program/list', data)
}
export async function saveRadioProgram(data: Partial<RadioProgram>) {
  if (USE_MOCK) { await delay(); mockRadioPrograms.push({ ...data, id: mockId(), audioStatus: 0, duration: 0, playCount: 0, likeCount: 0, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } as RadioProgram); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/program/save', data)
}
export async function updateRadioProgram(data: Partial<RadioProgram>) {
  if (USE_MOCK) { await delay(); const i = mockRadioPrograms.findIndex(p => p.id === data.id); if (i >= 0) Object.assign(mockRadioPrograms[i], data); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/program/update', data)
}
export async function deleteRadioProgram(id: string) {
  if (USE_MOCK) { await delay(); const i = mockRadioPrograms.findIndex(p => p.id === id); if (i >= 0) mockRadioPrograms.splice(i, 1); return mockResponse(null) }
  return post<{ code: number; data: null; msg: string }>('/radio/program/delete', { id })
}

// ==================== Subscription ====================
export async function getSubscriptionList(data: PageParams & { channelId?: string; status?: number }) {
  if (USE_MOCK) { await delay(); let f = [...mockSubscriptions]; if (data.channelId) f = f.filter(s => s.channelId === data.channelId); if (data.status !== undefined) f = f.filter(s => s.status === data.status); return mockResponse(paginate(f, data.current, data.pageSize)) }
  return post<{ code: number; data: PageResult<RadioSubscription>; msg: string }>('/radio/subscription/list', data)
}
