import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument } from 'mongoose'

export type ArchiveBatchDocument = HydratedDocument<ArchiveBatch>

// One row per successful archive run. Each row points at the S3 objects
// (JSONL + manifest) and records the manifest's sha256 + signature so the
// operator can verify cold-tier integrity without downloading the data.
//
// We keep these forever (negligible storage; one row per day at most). The
// operator UI lists them with timestamps + seq ranges; offline tooling can
// fetch the JSONL by `objectKey` for compliance retrieval.
@Schema({ collection: 'archive_batches', timestamps: true })
export class ArchiveBatch {
  // ISO date of the archive run (when we moved data, not when the events
  // happened). Used to bucket UI by day.
  @Prop({ required: true, index: true })
  archivedAt!: Date

  // Inclusive seq range of events archived in this batch. The next batch
  // starts at `endSeq + 1`.
  @Prop({ required: true, index: true })
  startSeq!: number

  @Prop({ required: true })
  endSeq!: number

  @Prop({ required: true })
  eventCount!: number

  // sha256 of the manifest JSON (after signing). The full manifest is in S3;
  // storing the hash here lets us detect manifest-level tampering without
  // downloading anything.
  @Prop({ required: true })
  manifestSha256!: string

  // S3 object keys (Bucket is global from ColdStorageClient).
  @Prop({ required: true })
  jsonlKey!: string

  @Prop({ required: true })
  manifestKey!: string

  // Uncompressed event payload bytes (informational; the gzip'd object size
  // lives in S3 metadata).
  @Prop({ required: true })
  bytesUncompressed!: number
}

export const ArchiveBatchSchema = SchemaFactory.createForClass(ArchiveBatch)
