/**
 * 文件用途：公告模块 API 封装
 * 说明：统一对接平台/租户/应用端公告接口
 * 日期：2025-12-20
 */

import request from '@/utils/http'
import type {
  AnnouncementCommand,
  AnnouncementFormData,
  AnnouncementQueryParams,
  TenantAnnouncementDto
} from '@/types/announcement'
import type { PagedResult } from '@/types/common/response'

/** 发布/撤销命令体（仅包含 RowVersion） */
type RowVersionCommand = Pick<AnnouncementCommand, 'rowVersion'>

const withTenantHeader = (tenantId: string) => ({
  headers: {
    'X-Tenant-Id': tenantId
  }
})

const normalizeQueryParams = (params: AnnouncementQueryParams) => {
  const { dateFrom, dateTo, ...rest } = params
  return {
    ...rest,
    effectiveFrom: params.effectiveFrom ?? dateFrom,
    effectiveTo: params.effectiveTo ?? dateTo
  }
}

/**
 * 平台公告 API
 * 路由前缀：/api/platform/announcements
 */
export const platformAnnouncementApi = {
  /**
   * 创建平台公告
   * @param data 创建参数
   */
  create: (data: AnnouncementFormData) => {
    return request.post<TenantAnnouncementDto>({
      url: '/api/platform/announcements',
      data
    })
  },

  /**
   * 查询平台公告列表
   * @param params 查询参数
   */
  list: (params: AnnouncementQueryParams) => {
    return request.get<PagedResult<TenantAnnouncementDto>>({
      url: '/api/platform/announcements',
      params: normalizeQueryParams(params)
    })
  },

  /**
   * 获取平台公告详情
   * @param announcementId 公告ID
   */
  detail: (announcementId: string) => {
    return request.get<TenantAnnouncementDto>({
      url: `/api/platform/announcements/${announcementId}`
    })
  },

  /**
   * 更新平台公告（仅草稿）
   * @param announcementId 公告ID
   * @param data 更新参数
   */
  update: (announcementId: string, data: AnnouncementFormData) => {
    return request.put<TenantAnnouncementDto>({
      url: `/api/platform/announcements/${announcementId}`,
      data
    })
  },

  /**
   * 发布平台公告
   * @param announcementId 公告ID
   * @param command 命令体（包含 rowVersion）
   */
  publish: (announcementId: string, command: RowVersionCommand) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/platform/announcements/${announcementId}/publish`,
      data: command
    })
  },

  /**
   * 撤销平台公告
   * @param announcementId 公告ID
   * @param command 命令体（包含 rowVersion）
   */
  revoke: (announcementId: string, command: RowVersionCommand) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/platform/announcements/${announcementId}/revoke`,
      data: command
    })
  }
}

/**
 * 租户公告 API
 * 路由前缀：/api/admin/v1/tenants/{tenantId}/announcements
 */
export const tenantAnnouncementApi = {
  /**
   * 分页查询租户公告
   * @param tenantId 租户ID
   * @param params 查询参数
   */
  list: (tenantId: string, params: AnnouncementQueryParams) => {
    return request.get<PagedResult<TenantAnnouncementDto>>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements`,
      params: normalizeQueryParams(params),
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 获取租户公告详情
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   */
  detail: (tenantId: string, announcementId: string) => {
    return request.get<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}`,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 创建租户公告
   * @param tenantId 租户ID
   * @param data 创建参数
   */
  create: (tenantId: string, data: AnnouncementFormData) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements`,
      data,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 更新租户公告（仅草稿）
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   * @param data 更新参数
   */
  update: (tenantId: string, announcementId: string, data: AnnouncementFormData) => {
    return request.put<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}`,
      data,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 发布租户公告
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   * @param command 命令体（包含 rowVersion）
   */
  publish: (tenantId: string, announcementId: string, command: RowVersionCommand) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}/publish`,
      data: command,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 撤销租户公告
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   * @param command 命令体（包含 rowVersion）
   */
  revoke: (tenantId: string, announcementId: string, command: RowVersionCommand) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}/revoke`,
      data: command,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 删除租户公告
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   */
  delete: (tenantId: string, announcementId: string) => {
    return request.del<boolean>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}`,
      ...withTenantHeader(tenantId)
    })
  },

  /**
   * 标记公告已读（兼容旧路径）
   * @param tenantId 租户ID
   * @param announcementId 公告ID
   */
  markRead: (tenantId: string, announcementId: string) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/admin/v1/tenants/${tenantId}/announcements/${announcementId}/read`,
      ...withTenantHeader(tenantId)
    })
  }
}

/**
 * 应用端公告 API
 * 路由前缀：/api/app/announcements
 */
export const appAnnouncementApi = {
  /**
   * 获取可见公告列表（已发布且有效期内）
   * @param params 查询参数
   */
  list: (params: AnnouncementQueryParams) => {
    return request.get<PagedResult<TenantAnnouncementDto>>({
      url: '/api/app/announcements',
      params: normalizeQueryParams(params)
    })
  },

  /**
   * 获取未读公告列表
   * @param params 查询参数
   */
  unread: (params: AnnouncementQueryParams) => {
    return request.get<PagedResult<TenantAnnouncementDto>>({
      url: '/api/app/announcements/unread',
      params: normalizeQueryParams(params)
    })
  },

  /**
   * 标记公告已读
   * @param announcementId 公告ID
   */
  markRead: (announcementId: string) => {
    return request.post<TenantAnnouncementDto>({
      url: `/api/app/announcements/${announcementId}/mark-read`
    })
  }
}
