import mongoose, { Schema, Document, Model } from 'mongoose';

export interface ISatsang extends Document {
  scheduleId?: mongoose.Types.ObjectId; // References SatsangSchedule, optional if one-off
  name: string;
  type: string; // e.g. 'Weekly Satsang' | 'Special Satsang' | 'Other'
  date: Date; // local date part normalized
  startTime: string; // e.g. '19:00'
  endTime: string; // e.g. '21:00'
  startDateTime: Date; // UTC Absolute event start timestamp
  endDateTime: Date; // UTC Absolute event end timestamp
  timezone: string; // IANA timezone snapshot, e.g. 'Asia/Kolkata'
  centerId: mongoose.Types.ObjectId; // References SatsangCenter
  cityId: mongoose.Types.ObjectId; // References City
  areaId: mongoose.Types.ObjectId; // References Area
  inchargeId: mongoose.Types.ObjectId; // References User
  notes: string;
  status: 'Upcoming' | 'Completed' | 'Cancelled';
  cancellationReason?: string; // Optional reason log
  hasModifiedSchedule: boolean; // True if edited from original template
  occurrenceKey?: string; // Deterministic occurrence identifier (e.g. scheduleId_2026-08-02_19-00)
  createdAt: Date;
  updatedAt: Date;
}

const SatsangSchema = new Schema<ISatsang>({
  scheduleId: { type: Schema.Types.ObjectId, ref: 'SatsangSchedule' },
  name: { type: String, required: true, trim: true },
  type: { type: String, required: true, default: 'Weekly Satsang' },
  date: { type: Date, required: true },
  startTime: { type: String, required: true }, // e.g. '19:00'
  endTime: { type: String, required: true }, // e.g. '21:00'
  startDateTime: { type: Date, required: true },
  endDateTime: { type: Date, required: true },
  timezone: { type: String, required: true, default: 'Asia/Kolkata' },
  centerId: { type: Schema.Types.ObjectId, ref: 'SatsangCenter', required: true },
  cityId: { type: Schema.Types.ObjectId, ref: 'City', required: true },
  areaId: { type: Schema.Types.ObjectId, ref: 'Area', required: true },
  inchargeId: { type: Schema.Types.ObjectId, ref: 'User', required: true },
  notes: { type: String, default: '', trim: true },
  status: {
    type: String,
    enum: ['Upcoming', 'Completed', 'Cancelled'],
    required: true,
    default: 'Upcoming'
  },
  cancellationReason: { type: String, trim: true },
  hasModifiedSchedule: { type: Boolean, default: false },
  occurrenceKey: { type: String }
}, {
  timestamps: true
});

// Unique sparse index to prevent concurrent race condition duplicates for generated recurring occurrences
SatsangSchema.index({ occurrenceKey: 1 }, { unique: true, sparse: true });

// Optimizing indexes for directory filtering, upcoming date lookups, and calendar range queries
SatsangSchema.index({ startDateTime: 1 });
SatsangSchema.index({ cityId: 1, startDateTime: 1 });
SatsangSchema.index({ areaId: 1, startDateTime: 1 });
SatsangSchema.index({ inchargeId: 1, startDateTime: 1 });
SatsangSchema.index({ scheduleId: 1, startDateTime: 1 });

export const Satsang: Model<ISatsang> = mongoose.models.Satsang || mongoose.model<ISatsang>('Satsang', SatsangSchema);
export default Satsang;
