import mongoose, { Schema, models, model } from "mongoose";

export interface CalendarEventDoc {
  _id: string;
  calendarId: mongoose.Types.ObjectId;
  title: string;
  description: string;
  start: Date;
  end: Date | null;
  allDay: boolean;
  createdBy: mongoose.Types.ObjectId;
  // ID des korrespondierenden Google-Calendar-Events, falls der Kalender
  // verbunden ist — wird für spätere Updates/Löschungen gebraucht.
  googleEventId: string;
  createdAt: Date;
}

const calendarEventSchema = new Schema<CalendarEventDoc>(
  {
    calendarId: { type: Schema.Types.ObjectId, ref: "Calendar", required: true, index: true },
    title: { type: String, required: true, trim: true, maxlength: 200 },
    description: { type: String, default: "", trim: true, maxlength: 2000 },
    start: { type: Date, required: true, index: true },
    end: { type: Date, default: null },
    allDay: { type: Boolean, default: false },
    createdBy: { type: Schema.Types.ObjectId, ref: "Client", required: true },
    googleEventId: { type: String, default: "" },
  },
  { timestamps: true }
);

export const CalendarEvent =
  (models.CalendarEvent as mongoose.Model<CalendarEventDoc>) ||
  model<CalendarEventDoc>("CalendarEvent", calendarEventSchema);
