import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import type { HydratedDocument } from "mongoose";
import mongoose from "mongoose";

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

  @Prop()
  name!: string;

  @Prop()
  path!: string;

  @Prop()
  methods!: string[];

  @Prop({ default: "// Write your function code here" })
  code!: string;

  @Prop()
  invocationCount: number = 0;

  @Prop()
  errorCount: number = 0;

  @Prop()
  logs: string[] = [];

  @Prop()
  lastInvocation?: Date;
}

export type FunctionDocument = HydratedDocument<Function>;
export const FunctionSchema = SchemaFactory.createForClass(Function);
