import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import type { Function } from "./function.schema.js";
import type { HydratedDocument } from "mongoose";
import mongoose from "mongoose";

@Schema()
export class Project {
  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: "User" })
  owner!: mongoose.Types.ObjectId;

  @Prop()
  name!: string;

  @Prop({ unique: true })
  slug!: string;

  @Prop()
  description!: string;

  @Prop({
    type: [{ type: mongoose.Schema.Types.ObjectId, ref: "Function" }],
    default: [],
  })
  functions!: Function[];

  @Prop({ default: 1000 })
  cpuTimeQuotaMsPerMinute!: number;

  @Prop({ default: Date.now })
  cpuTimeWindowStartedAt!: Date;

  @Prop({ default: 0 })
  cpuTimeUsedMs!: number;

  @Prop()
  functionsInvocationCount: number = 0;

  @Prop()
  functionsErrorCount: number = 0;
}

export type ProjectDocument = HydratedDocument<Project>;
export const ProjectSchema = SchemaFactory.createForClass(Project);
