import type {
  GeoLocationStatus,
  ServiceType,
  StoreAuditStatus,
  StoreBusinessStatus,
} from '#/enums/storeEnum';

import { requestClient } from '#/api/request';

export * from '#/enums/storeEnum';

/** 门店列表项 */
export interface StoreListItemDto {
  /** 门店ID */
  id: string;
  /** 门店名称 */
  name: string;
  /** 门店编码 */
  code: string;
  /** 联系电话 */
  contactPhone: string;
  /** 店长/负责人 */
  managerName: string;
  /** 门店地址 */
  address: string;
  /** 所在省份 */
  province?: string;
  /** 所在城市 */
  city?: string;
  /** 所在区县 */
  district?: string;
  /** 门店经度 */
  longitude?: null | number;
  /** 门店纬度 */
  latitude?: null | number;
  /** 地理定位状态 */
  geoStatus?: GeoLocationStatus;
  /** 地理定位失败原因 */
  geoFailReason?: string;
  /** 地理定位成功时间 */
  geoUpdatedAt?: null | string;
  /** 门店封面图 */
  coverImage?: string;
  /** 营业状态 */
  businessStatus: StoreBusinessStatus;
  /** 审核状态 */
  auditStatus: StoreAuditStatus;
  /** 服务方式 */
  serviceTypes: ServiceType[];
  /** 创建时间 */
  createdAt: string;
}

/** 门店统计 */
export interface StoreStatsDto {
  /** 门店总数 */
  total: number;
  /** 营业中 */
  operating: number;
  /** 休息中 */
  resting: number;
  /** 待审核 */
  pendingAudit: number;
}

/** 门店列表查询参数 */
export interface StoreListQuery {
  keyword?: string;
  businessStatus?: StoreBusinessStatus;
  auditStatus?: StoreAuditStatus;
  serviceType?: ServiceType;
  page: number;
  pageSize: number;
}

/** 分页结果 */
export interface PaginatedResult<T> {
  items: T[];
  total: number;
  page: number;
  pageSize: number;
}

/** 创建/编辑门店参数 */
export interface SaveStoreDto {
  id?: string;
  name: string;
  contactPhone: string;
  managerName: string;
  address: string;
  coverImage?: string;
  businessStatus?: StoreBusinessStatus;
  serviceTypes?: ServiceType[];
}

/** 快速切换门店营业状态参数 */
export interface ToggleStoreBusinessStatusDto {
  storeId: string;
  businessStatus: StoreBusinessStatus;
  closureReason?: number;
  closureReasonText?: string;
}

/** 获取门店列表 */
export async function getStoreListApi(params: StoreListQuery) {
  return requestClient.get<PaginatedResult<StoreListItemDto>>('/store/list', {
    params,
  });
}

/** 获取门店统计 */
export async function getStoreStatsApi() {
  return requestClient.get<StoreStatsDto>('/store/stats');
}

/** 创建门店 */
export async function createStoreApi(data: SaveStoreDto) {
  return requestClient.post('/store/create', data);
}

/** 更新门店 */
export async function updateStoreApi(data: SaveStoreDto) {
  return requestClient.post('/store/update', data);
}

/** 删除门店 */
export async function deleteStoreApi(id: string) {
  return requestClient.post('/store/delete', { id });
}

/** 快速切换门店营业状态 */
export async function toggleStoreBusinessStatusApi(
  data: ToggleStoreBusinessStatusDto,
) {
  return requestClient.post('/store/toggle-business-status', data);
}

/** 手动重试门店地理定位 */
export async function retryStoreGeocodeApi(storeId: string) {
  return requestClient.post(`/store/${storeId}/geocode/retry`);
}
