import type { Static } from "@sinclair/typebox"
import type { Document } from "mongoose"
import { Type } from "@sinclair/typebox"
import { PluginFastifyInstance } from "types"

/**
 * Trigger Schema
 */
export const triggerSchema = Type.Object(
  {
    type: Type.String(),
    settings: Type.Optional(Type.Record(Type.String(), Type.Any())),
  },
  {
    $id: "#trigger/base",
    title: "core/trigger",
  },
)

/**
 * Base InteractionLink Schema - Fields common to all operations
 */
const baseInteractionLinkSchema = {
  composition: Type.String(),
  slotType: Type.String(),
  triggers: Type.Array(triggerSchema, { default: [] }),
  queryParameters: Type.Optional(Type.Array(Type.String())),
  description: Type.Optional(Type.String()),
  tags: Type.Array(Type.String(), { default: [] }),
  isLandingPage: Type.Optional(Type.Boolean({ default: false })),
}

/**
 * Full InteractionLink Schema - Used for response serialization and database model
 */
export const interactionLinkSchema = Type.Object(
  {
    _id: Type.String(),
    ...baseInteractionLinkSchema,
    author: Type.String(),
    lastEditor: Type.String(),
    createdAt: Type.String({ format: "date-time" }),
    updatedAt: Type.String({ format: "date-time" }),
    isDeleted: Type.Boolean({ default: false }),
  },
  {
    $id: "#interactionLink/base",
    title: "core/interactionLink",
  },
)

/**
 * InteractionLink Create Schema - For POST operations
 */
export const interactionLinkCreateSchema = Type.Object(
  baseInteractionLinkSchema,
  {
    $id: "#interactionLink/create",
    title: "core/interactionLink-create",
  },
)

/**
 * InteractionLink Update Schema - For PATCH operations
 */
export const interactionLinkUpdateSchema = Type.Object(
  baseInteractionLinkSchema,
  {
    $id: "#interactionLink/update",
    title: "core/interactionLink-update",
  },
)

/**
 * Type Utilities
 */

type DateTimeFields = {
  createdAt: Date
  updatedAt: Date
}

type RawTrigger = Static<typeof triggerSchema>
export type Trigger = RawTrigger

type RawInteractionLink = Static<typeof interactionLinkSchema>

export type InteractionLink = Omit<RawInteractionLink, keyof DateTimeFields> &
  DateTimeFields

export interface InteractionLinkDoc
  extends Document<string, unknown, InteractionLink>,
    InteractionLink {
  _id: string
}

export type AnyInteractionLink = InteractionLink | InteractionLinkDoc

export { interactionLinkSchema as default }

/**
 * Types for create and update operations
 */
export type InteractionLinkCreate = Static<typeof interactionLinkCreateSchema>
export type InteractionLinkUpdate = Static<typeof interactionLinkUpdateSchema>

/**
 * Register Schema/Type Generation for Shared Folder
 */
export const registerSchemas = (fastify: PluginFastifyInstance) => {
  fastify.registerSchema({
    schema: interactionLinkSchema,
    name: "InteractionLink",
  })

  fastify.registerSchema({
    schema: interactionLinkCreateSchema,
    name: "InteractionLinkCreate",
  })

  fastify.registerSchema({
    schema: interactionLinkUpdateSchema,
    name: "InteractionLinkUpdate",
  })

  fastify.registerSchema({
    schema: triggerSchema,
    name: "Trigger",
  })
}
