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

export interface ISubscription extends Document {
    userId: mongoose.Types.ObjectId; // Reference to Doctor (or Clinic if it existed)
    planId: mongoose.Types.ObjectId;
    paymentId?: mongoose.Types.ObjectId; // Reference to Payment record
    category: 'Medical' | 'Dental' | 'Combined';
    startDate: Date;
    endDate: Date;
    status: 'Active' | 'PastDue' | 'Cancelled' | 'Expired';
    autoRenew: boolean;
    maxDoctors: number;
    createdAt: Date;
    updatedAt: Date;
}

const subscriptionSchema = new Schema<ISubscription>({
    userId: { type: Schema.Types.ObjectId, ref: 'User', required: true },
    planId: { type: Schema.Types.ObjectId, ref: 'Plan', required: true },
    paymentId: { type: Schema.Types.ObjectId, ref: 'Payment' },
    category: { type: String, enum: ['Medical', 'Dental', 'Combined'], default: 'Medical' },
    startDate: { type: Date, default: Date.now },
    endDate: { type: Date, required: true },
    status: { type: String, enum: ['Active', 'PastDue', 'Cancelled', 'Expired'], default: 'Active' },
    autoRenew: { type: Boolean, default: true },
    maxDoctors: { type: Number, default: 1 }
}, { timestamps: true });

export default mongoose.model<ISubscription>('Subscription', subscriptionSchema);
