import { Schema, model } from 'mongoose';
import { toJSON, paginate } from '@/shared/utils/plugins';
import { ITimelineDoc, ITimelineModel } from './timeline.interface';
import { TimelineStatus } from './timeline.constant';

const photoSchema = new Schema(
  {
    photoUrl: {
      type: String,
      required: true,
    },
    caption: {
      type: String,
      trim: true,
    },
  },
  { _id: false },
);

const timelineSchema = new Schema<ITimelineDoc>(
  {
    label: {
      type: String,
      required: true,
      trim: true,
      default: '',
    },
    progress: {
      type: Number,
      default: 0,
      required: true,
    },
    startDate: {
      type: Date,
      required: true,
    },
    endDate: {
      type: Date,
      required: true,
    },
    status: {
      type: String,
      enum: Object.values(TimelineStatus),
      default: TimelineStatus.NOT_STARTED,
    },
    photos: [photoSchema],
    createdBy: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    updatedBy: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    project: {
      type: Schema.Types.ObjectId,
      ref: 'Project',
      required: true,
    },
  },
  {
    timestamps: true,
  },
);

timelineSchema.plugin(toJSON);
timelineSchema.plugin(paginate);

export const Timeline = model<ITimelineDoc, ITimelineModel>(
  'Timeline',
  timelineSchema,
);
