export interface StoredDatasetVersion {
  id: string
  version: number
  create_time: string
  description?: string
  content: string
}

export interface StoredDatasetVersionState {
  versions: StoredDatasetVersion[]
  active_version_id: string
  /** 下次创建版本时使用的序号，确保已删除的版本号不会被复用。 */
  next_version_number: number
}

export class DatasetVersionMutationError extends Error {
  constructor(
    message: string,
    readonly status: number,
  ) {
    super(message)
    this.name = 'DatasetVersionMutationError'
  }
}

export function createInitialVersionState(
  content: string,
  createTime = '2026-07-13T00:00:00Z',
): StoredDatasetVersionState {
  const version: StoredDatasetVersion = {
    id: 'v1',
    version: 1,
    create_time: createTime,
    description: '初始版本',
    content,
  }
  return { versions: [version], active_version_id: version.id, next_version_number: 2 }
}

/** 为旧版 localStorage 数据补齐单调递增的版本号游标。 */
export function normalizeDatasetVersionState(
  state: Omit<StoredDatasetVersionState, 'next_version_number'> & {
    next_version_number?: number
  },
): StoredDatasetVersionState {
  const minimumNextNumber = Math.max(0, ...state.versions.map((item) => item.version)) + 1
  const nextVersionNumber = Number.isInteger(state.next_version_number)
    ? Math.max(minimumNextNumber, state.next_version_number as number)
    : minimumNextNumber

  return {
    ...state,
    next_version_number: nextVersionNumber,
  }
}

/** 追加不可变版本快照，并自动把新版本设为当前版本。 */
export function appendDatasetVersion(
  state: StoredDatasetVersionState,
  content: string,
  createTime: string,
  description = '在线编辑',
): StoredDatasetVersionState {
  const nextNumber = state.next_version_number
  const version: StoredDatasetVersion = {
    id: `v${nextNumber}`,
    version: nextNumber,
    create_time: createTime,
    description,
    content,
  }
  return {
    versions: [...state.versions, version],
    active_version_id: version.id,
    next_version_number: nextNumber + 1,
  }
}

/** 删除非当前、非初始的历史版本，并保留版本号游标。 */
export function deleteDatasetVersion(
  state: StoredDatasetVersionState,
  versionId: string,
): StoredDatasetVersionState {
  const version = state.versions.find((item) => item.id === versionId)
  if (!version) {
    throw new DatasetVersionMutationError('数据集版本不存在', 404)
  }
  if (state.versions.length === 1) {
    throw new DatasetVersionMutationError('唯一版本不可删除', 400)
  }
  if (version.id === state.active_version_id) {
    throw new DatasetVersionMutationError('当前版本不可删除，请先切换到其他版本', 409)
  }
  if (version.version === 1) {
    throw new DatasetVersionMutationError('初始版本不可删除', 400)
  }

  return {
    ...state,
    versions: state.versions.filter((item) => item.id !== versionId),
  }
}

/** 仅移动当前版本指针，不修改任何历史快照。 */
export function activateDatasetVersion(
  state: StoredDatasetVersionState,
  versionId: string,
): StoredDatasetVersionState {
  if (!state.versions.some((item) => item.id === versionId)) {
    throw new Error('数据集版本不存在')
  }
  return { ...state, active_version_id: versionId }
}

export function getActiveDatasetVersion(state: StoredDatasetVersionState) {
  const version = state.versions.find((item) => item.id === state.active_version_id)
  if (!version) throw new Error('当前数据集版本不存在')
  return version
}
