import { request } from '@/utils/request'
import type { PageResult, PageParams } from '@/types/api'

/**
 * 抢购管理（管理端）
 *
 * 后端契约：com.snack.server.module.seckill.controller.SeckillAdminController
 *   GET    /api/admin/seckill/page                  → Page<SeckillActivity>
 *   GET    /api/admin/seckill/{id}                  → SeckillActivityDetail
 *   POST   /api/admin/seckill                       → Long
 *   PUT    /api/admin/seckill                       → Void
 *   DELETE /api/admin/seckill/{id}                  → Void
 *   PUT    /api/admin/seckill/{id}/end              → Void  强制结束
 *   POST   /api/admin/seckill/{id}/warmup           → Void  预热
 *   POST   /api/admin/seckill/product               → Long  添加活动商品
 *   DELETE /api/admin/seckill/product/{id}          → Void  移除活动商品
 *
 * 其中 POST/PUT/DELETE 与"商品"子操作需要 super_admin 角色
 */

export interface SeckillActivity {
  id: number
  name: string
  cover?: string
  startTime: string
  endTime: string
  status: 0 | 1 | 2
  description?: string
  createTime: string
}

export interface SeckillActivityDetail extends SeckillActivity {
  products: SeckillProductItem[]
}

export interface SeckillActivitySavePayload {
  id?: number
  name: string
  cover?: string
  startTime: string
  endTime: string
  status?: 0 | 1 | 2
  description?: string
}

export interface SeckillProductItem {
  id: number
  activityId: number
  productId: number
  productName: string
  productImage: string
  skuId: number
  skuName: string
  originPrice: number
  seckillPrice: number
  discount: number
  seckillStock: number
  remainStock: number
  soldPercent: number
  perLimit: number
  sort: number
  sales: number
}

export interface SeckillProductAddPayload {
  activityId: number
  productId: number
  skuId: number
  originPrice: number
  seckillPrice: number
  seckillStock: number
  perLimit: number
  sort?: number
}

export function getSeckillPageApi(params: PageParams & { status?: 0 | 1 | 2 }) {
  return request<PageResult<SeckillActivity>>({
    url: '/api/admin/seckill/page',
    method: 'GET',
    params
  })
}

export function getSeckillDetailApi(id: number) {
  return request<SeckillActivityDetail>({
    url: `/api/admin/seckill/${id}`,
    method: 'GET'
  })
}

export function createSeckillApi(data: SeckillActivitySavePayload) {
  return request<number>({
    url: '/api/admin/seckill',
    method: 'POST',
    data
  })
}

export function updateSeckillApi(data: SeckillActivitySavePayload) {
  return request<void>({
    url: '/api/admin/seckill',
    method: 'PUT',
    data
  })
}

export function deleteSeckillApi(id: number) {
  return request<void>({
    url: `/api/admin/seckill/${id}`,
    method: 'DELETE'
  })
}

export function endSeckillApi(id: number) {
  return request<void>({
    url: `/api/admin/seckill/${id}/end`,
    method: 'PUT'
  })
}

export function warmupSeckillApi(id: number) {
  return request<void>({
    url: `/api/admin/seckill/${id}/warmup`,
    method: 'POST'
  })
}

export function addSeckillProductApi(data: SeckillProductAddPayload) {
  return request<number>({
    url: '/api/admin/seckill/product',
    method: 'POST',
    data
  })
}

export function removeSeckillProductApi(id: number) {
  return request<void>({
    url: `/api/admin/seckill/product/${id}`,
    method: 'DELETE'
  })
}
