/**
 * 客服管理服务层
 * 可先使用 mock 数据，后续替换为实际 API
 */

export type ServiceItem = {
  id: number
  name: string
  account: string
  avatar: string
  status: number
  created_at: string
}

export type ScriptItem = {
  id: number
  title: string
  content: string
  updated_at: string
}

export type MessageItem = {
  id: number
  user: string
  contact: string
  content: string
  status: number
  created_at: string
}

export type AutoReplyItem = {
  id: number
  keyword: string
  reply: string
  status: number
  updated_at: string
}

export type ServiceConfig = {
  workTime: string
  autoReply: number
  welcomeText: string
}

export const getServiceList = (params: any = {}): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      let items: ServiceItem[] = [
        { id: 1, name: '张客服', account: 'service01', avatar: '/static/user/avatar-1.png', status: 1, created_at: '2026-01-28 10:30:00' },
        { id: 2, name: '李客服', account: 'service02', avatar: '/static/user/avatar-2.png', status: 1, created_at: '2026-01-27 09:15:00' },
        { id: 3, name: '王客服', account: 'service03', avatar: '/static/user/avatar-3.png', status: 0, created_at: '2026-01-26 18:20:00' }
      ]

      if (params.keyword) {
        items = items.filter(item =>
          item.name.includes(params.keyword) || item.account.includes(params.keyword)
        )
      }

      if (params.status !== undefined && params.status !== null) {
        items = items.filter(item => item.status === params.status)
      }

      const total = items.length
      const start = ((params.page || 1) - 1) * (params.limit || 10)
      const end = start + (params.limit || 10)

      resolve({
        items: items.slice(start, end),
        total
      })
    }, 300)
  })
}

export const deleteService = (id: number): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '删除成功' }), 300)
  })
}

export const batchDeleteService = (ids: number[]): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '批量删除成功' }), 300)
  })
}

export const batchUpdateServiceStatus = (ids: number[], status: number): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '状态更新成功' }), 300)
  })
}

export const getScriptList = (params: any = {}): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      let items: ScriptItem[] = [
        { id: 1, title: '欢迎语', content: '您好，欢迎咨询，我们将尽快为您服务。', updated_at: '2026-01-28 10:30:00' },
        { id: 2, title: '退款说明', content: '退款将在 1-3 个工作日内原路返回。', updated_at: '2026-01-27 11:12:00' }
      ]

      if (params.title) {
        items = items.filter(item => item.title.includes(params.title))
      }

      const total = items.length
      const start = ((params.page || 1) - 1) * (params.limit || 10)
      const end = start + (params.limit || 10)

      resolve({
        items: items.slice(start, end),
        total
      })
    }, 300)
  })
}

export const saveScript = (data: any): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '保存成功' }), 300)
  })
}

export const deleteScript = (id: number): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '删除成功' }), 300)
  })
}

export const getMessageList = (params: any = {}): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      let items: MessageItem[] = [
        { id: 1, user: '小王', contact: '138****1122', content: '订单什么时候发货？', status: 0, created_at: '2026-01-28 09:10:00' },
        { id: 2, user: '小李', contact: '微信: li***', content: '能否开票？', status: 1, created_at: '2026-01-27 16:45:00' }
      ]

      if (params.keyword) {
        items = items.filter(item =>
          item.user.includes(params.keyword) || item.content.includes(params.keyword)
        )
      }

      if (params.status !== undefined && params.status !== null) {
        items = items.filter(item => item.status === params.status)
      }

      const total = items.length
      const start = ((params.page || 1) - 1) * (params.limit || 10)
      const end = start + (params.limit || 10)

      resolve({
        items: items.slice(start, end),
        total
      })
    }, 300)
  })
}

export const replyMessage = (id: number, data: any): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '回复成功' }), 300)
  })
}

export const deleteMessage = (id: number): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '删除成功' }), 300)
  })
}

export const batchReplyMessage = (ids: number[]): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '批量标记成功' }), 300)
  })
}

export const batchDeleteMessage = (ids: number[]): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '批量删除成功' }), 300)
  })
}

export const getAutoReplyList = (params: any = {}): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      let items: AutoReplyItem[] = [
        { id: 1, keyword: '退货', reply: '退货请联系在线客服处理。', status: 1, updated_at: '2026-01-28 08:30:00' },
        { id: 2, keyword: '物流', reply: '物流信息可在订单详情查看。', status: 1, updated_at: '2026-01-27 12:20:00' }
      ]

      if (params.keyword) {
        items = items.filter(item => item.keyword.includes(params.keyword) || item.reply.includes(params.keyword))
      }

      if (params.status !== undefined && params.status !== null) {
        items = items.filter(item => item.status === params.status)
      }

      const total = items.length
      const start = ((params.page || 1) - 1) * (params.limit || 10)
      const end = start + (params.limit || 10)

      resolve({
        items: items.slice(start, end),
        total
      })
    }, 300)
  })
}

export const saveAutoReply = (data: any): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '保存成功' }), 300)
  })
}

export const deleteAutoReply = (id: number): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '删除成功' }), 300)
  })
}

export const batchDeleteAutoReply = (ids: number[]): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '批量删除成功' }), 300)
  })
}

export const getServiceConfig = (): Promise<ServiceConfig> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        workTime: '09:00-18:00',
        autoReply: 1,
        welcomeText: '您好，欢迎咨询，我们将尽快为您服务。'
      })
    }, 300)
  })
}

export const saveServiceConfig = (data: any): Promise<any> => {
  return new Promise((resolve) => {
    setTimeout(() => resolve({ success: true, message: '保存成功' }), 300)
  })
}
