/**
 * @description Banner 管理 API 接口
 * @filename banner-manage.ts
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-28
 */

import { api } from '@/apis';
import type { ResultDomain, Banner, PageParam } from '@/types';

/**
 * Banner 管理 API 服务
 */
export const bannerApi = {
  /**
   * 获取横幅列表
   * @param filter 筛选条件
   * @returns Promise<ResultDomain<Banner>>
   */
  async getBannerList(filter?: Partial<Banner>): Promise<ResultDomain<Banner>> {
    const response = await api.get<Banner>('/news/banners/list', filter);
    return response.data;
  },

  /**
   * 获取横幅分页列表
   * @param pageParam 分页参数
   * @param filter 筛选条件
   * @returns Promise<ResultDomain<Banner>>
   */
  async getBannerPage(pageParam: PageParam, filter?: Partial<Banner>): Promise<ResultDomain<Banner>> {
    const response = await api.post<Banner>('/news/banners/banner/page', {
      pageParam,
      filter,
    });
    return response.data;
  },

  /**
   * 创建横幅
   * @param banner 横幅信息
   * @returns Promise<ResultDomain<Banner>>
   */
  async createBanner(banner: Banner): Promise<ResultDomain<Banner>> {
    const response = await api.post<Banner>('/news/banners/banner', banner);
    return response.data;
  },

  /**
   * 更新横幅
   * @param banner 横幅信息
   * @returns Promise<ResultDomain<Banner>>
   */
  async updateBanner(banner: Banner): Promise<ResultDomain<Banner>> {
    const response = await api.put<Banner>('/news/banners/banner', banner);
    return response.data;
  },

  /**
   * 删除横幅
   * @param banner 横幅信息（包含 bannerID）
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteBanner(banner: Banner): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>('/news/banners/banner', {
      data: banner
    });
    return response.data;
  },

  /**
   * 根据 ID 删除横幅
   * @param bannerID 横幅ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteBannerById(bannerID: string): Promise<ResultDomain<boolean>> {
    return this.deleteBanner({ id: bannerID });
  },

  /**
   * 更新横幅状态
   * @param bannerID 横幅ID
   * @param status 状态值（0禁用 1启用）
   * @returns Promise<ResultDomain<Banner>>
   */
  async updateBannerStatus(bannerID: string, status: number): Promise<ResultDomain<Banner>> {
    const response = await api.put<Banner>('/news/banners/banner/status', {
      id: bannerID,
      status,
    });
    return response.data;
  },

  /**
   * 启用横幅
   * @param bannerID 横幅ID
   * @returns Promise<ResultDomain<Banner>>
   */
  async enableBanner(bannerID: string): Promise<ResultDomain<Banner>> {
    return this.updateBannerStatus(bannerID, 1);
  },

  /**
   * 禁用横幅
   * @param bannerID 横幅ID
   * @returns Promise<ResultDomain<Banner>>
   */
  async disableBanner(bannerID: string): Promise<ResultDomain<Banner>> {
    return this.updateBannerStatus(bannerID, 0);
  },

  /**
   * 获取首页横幅列表
   * @returns Promise<ResultDomain<Banner>>
   */
  async getHomeBannerList(): Promise<ResultDomain<Banner>> {
    const response = await api.get<Banner>('/news/banners/home');
    return response.data;
  },
};

export default bannerApi;

