/**
 * Membership Plan Model
 *
 * Stores membership/subscription plan data for future
 * subscription/payment integration. Current phase is CRUD only.
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPES
// ============================================

export type MembershipType = 'free' | 'basic' | 'standard' | 'premium' | 'enterprise' | 'vip' | 'lifetime' | 'custom';
export type BillingCycle = 'monthly' | 'quarterly' | 'annual' | 'one-time';
export type MembershipPlanStatus = 'active' | 'inactive';

// ============================================
// INTERFACE
// ============================================

export interface IMembershipPlan extends Document {
  companyId: string;
  name: string;
  slug?: string;
  type: MembershipType;
  price?: number;
  billingCycle: BillingCycle;
  description?: string;
  descriptionVersions?: IDescriptionVersion[];
  features?: Record<string, boolean>;
  maxUsers?: number;
  storageLimit?: string;
  status: MembershipPlanStatus;
  benefits?: string;
  limitations?: string;
  onboardingFlow?: string;
  terms?: string;
  fullDocument?: string;
  createdAt: Date;
  updatedAt: Date;
}

// Description Version sub-schema
interface IDescriptionVersion {
  id: string;
  content: string;
  tone?: string;
  customInstructions?: string;
  generatedAt: Date;
  isCurrent?: boolean;
}

const DescriptionVersionSchema = new Schema<IDescriptionVersion>({
  id: { type: String, required: true },
  content: { type: String, required: true },
  tone: String,
  customInstructions: String,
  generatedAt: { type: Date, default: Date.now },
  isCurrent: { type: Boolean, default: false }
}, { _id: false });

// ============================================
// SCHEMA
// ============================================

const MembershipPlanSchema = new Schema<IMembershipPlan>(
  {
    companyId: { type: String, required: [true, 'Company ID is required'] },
    name: { type: String, required: [true, 'Plan name is required'], trim: true, maxlength: [200, 'Plan name cannot exceed 200 characters'] },
    slug: { type: String, trim: true, lowercase: true },
    type: {
      type: String,
      enum: ['free', 'basic', 'standard', 'premium', 'enterprise', 'vip', 'lifetime', 'custom'],
      default: 'basic',
    },
    price: { type: Number, min: [0, 'Price cannot be negative'] },
    billingCycle: {
      type: String,
      enum: ['monthly', 'quarterly', 'annual', 'one-time'],
      default: 'monthly',
    },
    description: { type: String, trim: true },
    descriptionVersions: [DescriptionVersionSchema],
    features: { type: Schema.Types.Mixed, default: {} },
    maxUsers: { type: Number, min: [1, 'Max users must be at least 1'] },
    storageLimit: { type: String, trim: true },
    status: {
      type: String,
      enum: ['active', 'inactive'],
      default: 'active',
    },
    benefits: { type: String, trim: true },
    limitations: { type: String, trim: true },
    onboardingFlow: { type: String, trim: true },
    terms: { type: String, trim: true },
    fullDocument: { type: String },
  },
  { timestamps: true }
);

MembershipPlanSchema.index({ companyId: 1, status: 1 });
MembershipPlanSchema.index({ companyId: 1, type: 1 });

export const MembershipPlan = mongoose.models.MembershipPlan || mongoose.model('MembershipPlan', MembershipPlanSchema);