import mongoose, { Schema, Document } from "mongoose";

export interface IAppointment extends Document {
  // Store
  storeId: mongoose.Types.ObjectId;
  storeName: string;

  // Slot
  date: Date;
  timeSlot: string;        // e.g. "10:00 AM"
  slotPeriod: "morning" | "afternoon" | "evening";

  // User Details
  name: string;
  phone: string;
  email: string;
  celebrating: "groom" | "bride" | "event" | "other";
  celebratingOther?: string;
  lookingFor?: string;

  // Status
  status: "pending" | "confirmed" | "cancelled" | "completed";

  createdAt: string;
  updatedAt: string;
}

const AppointmentSchema = new Schema<IAppointment>(
  {
    // Store
    storeId:   { type: Schema.Types.ObjectId, ref: "stores", required: true },
    storeName: { type: String, required: true },

    // Slot
    date:       { type: Date, required: true },
    timeSlot:   { type: String, required: true },
    slotPeriod: { type: String, enum: ["morning", "afternoon", "evening"], required: true },

    // User Details
    name:             { type: String, required: true, trim: true },
    phone:            { type: String, required: true, trim: true },
    email:            { type: String, required: true, trim: true, lowercase: true },
    celebrating:      { type: String, enum: ["groom", "bride", "event", "other"], required: true },
    celebratingOther: { type: String, trim: true },
    lookingFor:       { type: String, trim: true },

    // Status
    status: {
      type: String,
      enum: ["pending", "confirmed", "cancelled", "completed"],
      default: "pending",
    },
  },
  { timestamps: true }
);

// Indexes
AppointmentSchema.index({ storeId: 1, date: 1 });
AppointmentSchema.index({ phone: 1 });
AppointmentSchema.index({ status: 1 });
AppointmentSchema.index({ date: 1 });

export default mongoose.models.Appointment ||
  mongoose.model<IAppointment>("Appointment", AppointmentSchema);