import mongoose from "mongoose"
import { PluginFastifyInstance } from "types"
import { v4 as uuidv4 } from "uuid"
const { Schema } = mongoose

export const MODEL_BASENAME = "interactionLink"

const triggerSchema = new Schema(
  {
    type: {
      type: String,
      required: true,
    },
    settings: {
      type: Schema.Types.Mixed,
    },
  },
  { _id: false },
)

const interactionLinkSchema = new Schema(
  {
    _id: {
      type: String,
      default: () => uuidv4(),
      required: true,
    },

    composition: {
      type: Schema.Types.String,
      ref: "raclette__core-composition",
      required: true,
    },

    slotType: {
      type: String,
      required: true,
    },

    triggers: {
      type: [triggerSchema],
      default: [],
    },

    queryParameters: {
      type: Schema.Types.Array,
      of: Schema.Types.String,
    },

    description: {
      type: String,
    },

    tags: [
      {
        type: Schema.Types.String,
        ref: "raclette__core-tag",
      },
    ],

    author: {
      type: Schema.Types.String,
      ref: "raclette__core-user",
      required: true,
    },

    lastEditor: {
      type: Schema.Types.String,
      ref: "raclette__core-user",
      required: true,
    },

    isLandingPage: { type: Boolean, default: false },
    isDeleted: { type: Boolean, default: false },
  },
  { timestamps: true },
)

export const createModels = (fastify: PluginFastifyInstance) => {
  return fastify.createModel(MODEL_BASENAME, interactionLinkSchema)
}
