// models/appointmentModel.js
const mongoose = require("mongoose");

const appointmentSchema = new mongoose.Schema(
  {
    doctor: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },

    patient: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Patient",
      required: true,
    },

    date: {
      type: String, // "YYYY-MM-DD" (same as Slot model)
      required: true,
    },

    startTime: {
      type: String, // "09:30"
      required: true,
    },

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

    slotRef: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Slot", // optional reference
      required: true,
    },

    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: true,
    },

    status: {
      type: String,
      enum: [
        "Booked",
        "Cancelled",
        "Waiting",
        "Completed",
        "No-Show",
      ],
      default: "Booked",
    },
    remarks: {
      type: String,
      trim: true,
      maxlength: 500
    },

    cancelledAt: {
      type: Date
    },

    cancelledBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    },

    bookedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User", // receptionist / admin who booked the appointment
      required: true,
    },

    notes: {
      type: String,
      trim: true,
      maxlength: 500,
    },

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

module.exports = mongoose.model("Appointment", appointmentSchema);