/**
 * Pricing Discount Model
 * Defines promotional discount codes and automatic discounts
 * that can be applied during subscription checkout or plan changes.
 */

import mongoose, { Schema, Document } from 'mongoose';

export type PricingDiscountType = 'percentage' | 'fixed_amount' | 'free_trial_extension';

export interface IPricingDiscount extends Document {
  code: string;
  name: string;
  description?: string;
  discountType: PricingDiscountType;
  value: number;
  applicableBillingCycles: string[];
  applicableCategorySlugs?: string[];
  applicableItemSlugs?: string[];
  applicablePackageIds?: string[];
  maxRedemptions?: number;
  currentRedemptions: number;
  maxRedemptionsPerUser?: number;
  isActive: boolean;
  validFrom?: Date;
  validUntil?: Date;
  createdAt: Date;
  updatedAt: Date;
}

const PricingDiscountSchema = new Schema<IPricingDiscount>({
  code: {
    type: String,
    required: [true, 'Discount code is required'],
    unique: true,
    trim: true,
    uppercase: true,
    match: [/^[A-Z0-9_]+$/, 'Discount code must contain only uppercase letters, numbers, and underscores'],
  },
  name: {
    type: String,
    required: [true, 'Discount name is required'],
    trim: true,
    maxlength: [150, 'Discount name cannot exceed 150 characters'],
  },
  description: {
    type: String,
    trim: true,
    maxlength: [1000, 'Description cannot exceed 1000 characters'],
  },
  discountType: {
    type: String,
    enum: ['percentage', 'fixed_amount', 'free_trial_extension'],
    required: true,
  },
  value: {
    type: Number,
    required: [true, 'Discount value is required'],
    min: [0, 'Discount value cannot be negative'],
  },
  applicableBillingCycles: [{
    type: String,
    enum: ['monthly', 'quarterly', 'half_yearly', 'yearly', 'lifetime'],
  }],
  applicableCategorySlugs: [{ type: String, trim: true, lowercase: true }],
  applicableItemSlugs: [{ type: String, trim: true, lowercase: true }],
  applicablePackageIds: [{ type: String }],
  maxRedemptions: {
    type: Number,
    min: [0, 'Max redemptions cannot be negative'],
    default: null,
  },
  currentRedemptions: {
    type: Number,
    default: 0,
    min: [0, 'Current redemptions cannot be negative'],
  },
  maxRedemptionsPerUser: {
    type: Number,
    min: [0, 'Max redemptions per user cannot be negative'],
    default: null,
  },
  isActive: {
    type: Boolean,
    default: true,
  },
  validFrom: {
    type: Date,
    default: null,
  },
  validUntil: {
    type: Date,
    default: null,
  },
}, {
  timestamps: true,
  toJSON: { virtuals: true },
  toObject: { virtuals: true },
});

PricingDiscountSchema.index({ code: 1 }, { unique: true });
PricingDiscountSchema.index({ isActive: 1 });
PricingDiscountSchema.index({ validFrom: 1, validUntil: 1 });

export const PricingDiscount = mongoose.models.PricingDiscount || mongoose.model<IPricingDiscount>('PricingDiscount', PricingDiscountSchema);