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

export type ReportDocument = HydratedDocument<Report>

export type ReportKind = 'health' | 'revenue' | 'churn' | 'custom'

// A partner's saved/custom report definition. The analytics themselves are
// computed live (UsersService.partnerReports); this only persists a named,
// reusable configuration the partner created from the New-report modal.
@Schema({ collection: 'reports', timestamps: true })
export class Report {
  @Prop({ type: Types.ObjectId, ref: 'Partner', required: true, index: true })
  partnerId!: Types.ObjectId

  @Prop({ required: true, trim: true })
  name!: string

  @Prop({ enum: ['health', 'revenue', 'churn', 'custom'], default: 'custom' })
  kind!: ReportKind

  @Prop({ trim: true })
  description?: string

  // Free-form saved configuration (metrics, filters, groupBy, schedule,
  // recipients, format). UI-driven shape for v1.
  @Prop({ type: Object, default: {} })
  definition!: Record<string, unknown>

  @Prop({ trim: true })
  createdByEmail?: string
}

export const ReportSchema = SchemaFactory.createForClass(Report)
