import api from '@/utils/http'

interface StoreRequestOptions {
  tenantId?: string
}

const buildTenantHeader = (options?: StoreRequestOptions) => {
  // 1. 未提供租户ID时返回空配置
  if (!options?.tenantId) {
    return {}
  }

  // 2. 返回携带租户头的请求配置
  return {
    headers: {
      'X-Tenant-Id': String(options.tenantId)
    }
  }
}

/**
 * 获取门店列表
 */
export function fetchStoreList(params?: Partial<Api.Store.StoreListParams>) {
  return api.get<Api.Store.StoreListResponse>({
    url: '/api/admin/v1/stores',
    params
  })
}

/**
 * 获取门店详情
 */
export function fetchStoreDetail(storeId: string, options?: { showErrorMessage?: boolean }) {
  return api.get<Api.Store.StoreDto>({
    url: `/api/admin/v1/stores/${storeId}`,
    showErrorMessage: options?.showErrorMessage
  })
}

/**
 * 创建门店
 */
export function fetchCreateStore(data: Api.Store.CreateStoreRequest) {
  return api.post<Api.Store.StoreDto>({
    url: '/api/admin/v1/stores',
    data
  })
}

/**
 * 更新门店
 */
export function fetchUpdateStore(storeId: string, data: Api.Store.UpdateStoreRequest) {
  return api.put<Api.Store.StoreDto>({
    url: `/api/admin/v1/stores/${storeId}`,
    data
  })
}

/**
 * 删除门店
 */
export function fetchDeleteStore(storeId: string) {
  return api.del<void>({
    url: `/api/admin/v1/stores/${storeId}`
  })
}

/**
 * 提交门店审核
 */
export function fetchSubmitStoreAudit(storeId: string) {
  return api.post<boolean>({
    url: `/api/admin/v1/stores/${storeId}/submit`,
    data: {}
  })
}

/**
 * 切换门店经营状态
 */
export function fetchToggleBusinessStatus(
  storeId: string,
  data: Api.Store.ToggleBusinessStatusRequest
) {
  return api.post<Api.Store.StoreDto>({
    url: `/api/admin/v1/stores/${storeId}/business-status`,
    data
  })
}

/**
 * 获取门店资质列表
 */
export function fetchStoreQualifications(storeId: string, options?: StoreRequestOptions) {
  return api.get<Api.Store.StoreQualificationDto[]>({
    url: `/api/admin/v1/stores/${storeId}/qualifications`,
    ...buildTenantHeader(options)
  })
}

/**
 * 检查门店资质完整性
 */
export function fetchCheckStoreQualifications(storeId: string, options?: StoreRequestOptions) {
  return api.get<Api.Store.StoreQualificationCheckResultDto>({
    url: `/api/admin/v1/stores/${storeId}/qualifications/check`,
    ...buildTenantHeader(options)
  })
}

/**
 * 创建门店资质
 */
export function fetchCreateStoreQualification(
  storeId: string,
  data: Api.Store.CreateStoreQualificationRequest,
  options?: StoreRequestOptions
) {
  return api.post<Api.Store.StoreQualificationDto>({
    url: `/api/admin/v1/stores/${storeId}/qualifications`,
    data,
    ...buildTenantHeader(options)
  })
}

/**
 * 更新门店资质
 */
export function fetchUpdateStoreQualification(
  storeId: string,
  qualificationId: string,
  data: Api.Store.UpdateStoreQualificationRequest,
  options?: StoreRequestOptions
) {
  return api.put<Api.Store.StoreQualificationDto>({
    url: `/api/admin/v1/stores/${storeId}/qualifications/${qualificationId}`,
    data,
    ...buildTenantHeader(options)
  })
}

/**
 * 删除门店资质
 */
export function fetchDeleteStoreQualification(
  storeId: string,
  qualificationId: string,
  options?: StoreRequestOptions
) {
  return api.del<boolean>({
    url: `/api/admin/v1/stores/${storeId}/qualifications/${qualificationId}`,
    ...buildTenantHeader(options)
  })
}

/**
 * 获取门店营业时段
 */
export function fetchStoreBusinessHours(storeId: string) {
  return api.get<Api.Store.StoreBusinessHourDto[]>({
    url: `/api/admin/v1/stores/${storeId}/business-hours`
  })
}

/**
 * 批量更新门店营业时段
 */
export function fetchBatchUpdateBusinessHours(
  storeId: string,
  data: Api.Store.BatchUpdateBusinessHoursRequest
) {
  return api.put<Api.Store.StoreBusinessHourDto[]>({
    url: `/api/admin/v1/stores/${storeId}/business-hours/batch`,
    data
  })
}

/**
 * 获取配送区域
 */
export function fetchStoreDeliveryZones(storeId: string) {
  return api.get<Api.Store.StoreDeliveryZoneDto[]>({
    url: `/api/admin/v1/stores/${storeId}/delivery-zones`
  })
}

/**
 * 创建配送区域
 */
export function fetchCreateStoreDeliveryZone(
  storeId: string,
  data: Api.Store.CreateStoreDeliveryZoneRequest
) {
  return api.post<Api.Store.StoreDeliveryZoneDto>({
    url: `/api/admin/v1/stores/${storeId}/delivery-zones`,
    data
  })
}

/**
 * 更新配送区域
 */
export function fetchUpdateStoreDeliveryZone(
  storeId: string,
  deliveryZoneId: string,
  data: Api.Store.UpdateStoreDeliveryZoneRequest
) {
  return api.put<Api.Store.StoreDeliveryZoneDto>({
    url: `/api/admin/v1/stores/${storeId}/delivery-zones/${deliveryZoneId}`,
    data
  })
}

/**
 * 删除配送区域
 */
export function fetchDeleteStoreDeliveryZone(storeId: string, deliveryZoneId: string) {
  return api.del<void>({
    url: `/api/admin/v1/stores/${storeId}/delivery-zones/${deliveryZoneId}`
  })
}

/**
 * 配送范围检测
 */
export function fetchDeliveryZoneCheck(storeId: string, data: Api.Store.StoreDeliveryCheckRequest) {
  return api.post<Api.Store.StoreDeliveryCheckResultDto>({
    url: `/api/admin/v1/stores/${storeId}/delivery-check`,
    data
  })
}

/**
 * 获取门店费用配置
 */
export function fetchStoreFee(storeId: string) {
  return api.get<Api.Store.StoreFeeDto>({
    url: `/api/admin/v1/stores/${storeId}/fee`
  })
}

/**
 * 更新门店费用配置
 */
export function fetchUpdateStoreFee(storeId: string, data: Api.Store.UpdateStoreFeeRequest) {
  return api.put<Api.Store.StoreFeeDto>({
    url: `/api/admin/v1/stores/${storeId}/fee`,
    data
  })
}

/**
 * 门店费用预览
 */
export function fetchCalculateStoreFee(storeId: string, data: Api.Store.CalculateStoreFeeRequest) {
  return api.post<Api.Store.StoreFeeCalculationResultDto>({
    url: `/api/admin/v1/stores/${storeId}/fee/calculate`,
    data
  })
}

// ==================== 临时时段 API ====================

/**
 * 获取门店临时时段列表
 */
export function fetchStoreHolidays(storeId: string) {
  return api.get<Api.Store.StoreHolidayDto[]>({
    url: `/api/admin/v1/stores/${storeId}/holidays`
  })
}

/**
 * 创建临时时段
 */
export function fetchCreateStoreHoliday(
  storeId: string,
  data: Api.Store.CreateStoreHolidayRequest
) {
  return api.post<Api.Store.StoreHolidayDto>({
    url: `/api/admin/v1/stores/${storeId}/holidays`,
    data
  })
}

/**
 * 更新临时时段
 */
export function fetchUpdateStoreHoliday(
  storeId: string,
  holidayId: string,
  data: Api.Store.UpdateStoreHolidayRequest
) {
  return api.put<Api.Store.StoreHolidayDto>({
    url: `/api/admin/v1/stores/${storeId}/holidays/${holidayId}`,
    data
  })
}

/**
 * 删除临时时段
 */
export function fetchDeleteStoreHoliday(storeId: string, holidayId: string) {
  return api.del<boolean>({
    url: `/api/admin/v1/stores/${storeId}/holidays/${holidayId}`
  })
}
