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

/**
 * 轮播图管理 API
 *
 * 后端契约：com.snack.server.module.banner.controller.BannerAdminController
 *   GET    /api/admin/banner/page                    → Page<BannerVO>
 *   GET    /api/admin/banner/{id}                    → BannerVO
 *   POST   /api/admin/banner                         → Long
 *   PUT    /api/admin/banner                         → Void
 *   DELETE /api/admin/banner/{id}                    → Void
 *   PUT    /api/admin/banner/{id}/status/{status}    → Void
 *   PUT    /api/admin/banner/{id}/sort/{sort}        → Void
 */

export type BannerLinkType = 0 | 1 | 2 | 3
export type BannerStatus = 0 | 1

export interface BannerItem {
  id: number
  title: string
  image: string
  linkType: BannerLinkType
  linkValue: string
  sort: number
  status: BannerStatus
  startTime: string
  endTime: string
  createTime: string
  updateTime: string
}

export interface BannerSavePayload {
  id?: number
  image: string
  title?: string
  linkType?: BannerLinkType
  linkValue?: string
  sort?: number
  status?: BannerStatus
  startTime?: string
  endTime?: string
}

export interface BannerPageQuery extends PageParams {
  keyword?: string
  status?: BannerStatus
}

export function getBannerPageApi(params: BannerPageQuery) {
  return request<PageResult<BannerItem>>({
    url: '/api/admin/banner/page',
    method: 'GET',
    params
  })
}

export function getBannerDetailApi(id: number) {
  return request<BannerItem>({
    url: `/api/admin/banner/${id}`,
    method: 'GET'
  })
}

export function createBannerApi(payload: BannerSavePayload) {
  return request<number>({
    url: '/api/admin/banner',
    method: 'POST',
    data: payload
  })
}

export function updateBannerApi(payload: BannerSavePayload) {
  return request<void>({
    url: '/api/admin/banner',
    method: 'PUT',
    data: payload
  })
}

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

export function updateBannerStatusApi(id: number, status: BannerStatus) {
  return request<void>({
    url: `/api/admin/banner/${id}/status/${status}`,
    method: 'PUT'
  })
}

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