import client from '../client';

// ---------------------------------------------------------------------------
// 轮播图类型
// ---------------------------------------------------------------------------

export interface BannerItem {
  id: string;
  tenant_id: string;
  media_item_id: string;
  title?: string;
  subtitle?: string;
  link_type?: string;
  link_target?: string;
  sort_order: number;
  status: string;
  start_time?: string;
  end_time?: string;
  image_url?: string;
  thumbnail_url?: string;
  media_deleted: boolean;
  created_at: string;
  updated_at: string;
  created_by?: string;
  updated_by?: string;
  version: number;
}

export interface CreateBannerReq {
  media_item_id: string;
  title?: string;
  subtitle?: string;
  link_type?: string;
  link_target?: string;
  sort_order?: number;
  status?: string;
  start_time?: string;
  end_time?: string;
}

export interface UpdateBannerReq {
  media_item_id?: string;
  title?: string;
  subtitle?: string;
  link_type?: string;
  link_target?: string;
  sort_order?: number;
  status?: string;
  start_time?: string;
  end_time?: string;
  version: number;
}

export interface SortBannerReq {
  items: Array<{ id: string; sort_order: number }>;
}

// ---------------------------------------------------------------------------
// 轮播图 API
// ---------------------------------------------------------------------------

export const bannerApi = {
  /** 获取轮播图列表（可按状态筛选） */
  list: async (status?: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: BannerItem[];
    }>('/health/banners', { params: status ? { status } : undefined });
    return data.data;
  },

  /** 获取单个轮播图 */
  get: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: BannerItem;
    }>(`/health/banners/${id}`);
    return data.data;
  },

  /** 创建轮播图 */
  create: async (req: CreateBannerReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: BannerItem;
    }>('/health/banners', req);
    return data.data;
  },

  /** 更新轮播图 */
  update: async (id: string, req: UpdateBannerReq) => {
    const { data } = await client.put<{
      success: boolean;
      data: BannerItem;
    }>(`/health/banners/${id}`, req);
    return data.data;
  },

  /** 删除轮播图 */
  delete: async (id: string, version: number) => {
    const { data } = await client.delete<{
      success: boolean;
      data: null;
    }>(`/health/banners/${id}`, { data: { version } });
    return data.data;
  },

  /** 轮播图排序 */
  sort: async (req: SortBannerReq) => {
    const { data } = await client.put<{
      success: boolean;
      data: null;
    }>('/health/banners/sort', req);
    return data.data;
  },
};
