/**
 * Pricing Item Model
 * Defines individual pricing items within categories (e.g., "Blog Module", "OpenAI Provider", "GPT-4o Model").
 * Each item has pricing for all billing cycles and can map to existing PackageLimits keys for backward compatibility.
 */

import mongoose, { Schema, Document } from 'mongoose';

export type PricingItemMode = 'per_unit' | 'flat' | 'tiered' | 'graduated';

export interface IPricingTier extends Document {
  from: number;   // Start of tier (inclusive)
  to: number;     // End of tier (inclusive), -1 for unlimited
  price: number;  // Price per unit in this tier
}

const PricingTierSchema = new Schema<IPricingTier>({
  from: { type: Number, required: true },
  to: { type: Number, required: true },
  price: { type: Number, required: true, min: 0 },
}, { _id: false });

export interface IPricingItem extends Document {
  categorySlug: string;
  slug: string;
  name: string;
  description?: string;
  icon?: string;
  parentItemSlug?: string;
  basePrice: number;
  pricingMode: PricingItemMode;
  monthlyPrice: number;
  quarterlyPrice: number;
  halfYearlyPrice: number;
  yearlyPrice: number;
  lifetimePrice: number;
  tiers?: IPricingTier[];
  usageLimitKey?: string;
  defaultLimit: number;
  metadata?: Record<string, any>;
  isActive: boolean;
  sortOrder: number;
  createdAt: Date;
  updatedAt: Date;
}

const PricingItemSchema = new Schema<IPricingItem>({
  categorySlug: {
    type: String,
    required: [true, 'Category slug is required'],
    trim: true,
    lowercase: true,
  },
  slug: {
    type: String,
    required: [true, 'Item slug is required'],
    unique: true,
    trim: true,
    lowercase: true,
    match: [/^[a-z0-9_]+$/, 'Slug must contain only lowercase letters, numbers, and underscores'],
  },
  name: {
    type: String,
    required: [true, 'Item name is required'],
    trim: true,
    maxlength: [150, 'Item name cannot exceed 150 characters'],
  },
  description: {
    type: String,
    trim: true,
    maxlength: [1000, 'Description cannot exceed 1000 characters'],
  },
  icon: {
    type: String,
    trim: true,
    maxlength: [50, 'Icon name cannot exceed 50 characters'],
  },
  parentItemSlug: {
    type: String,
    trim: true,
    lowercase: true,
    default: null,
  },
  basePrice: {
    type: Number,
    required: [true, 'Base price is required'],
    default: 0,
    min: [0, 'Base price cannot be negative'],
  },
  pricingMode: {
    type: String,
    enum: ['per_unit', 'flat', 'tiered', 'graduated'],
    default: 'flat',
  },
  monthlyPrice: {
    type: Number,
    required: [true, 'Monthly price is required'],
    default: 0,
    min: [0, 'Monthly price cannot be negative'],
  },
  quarterlyPrice: {
    type: Number,
    default: 0,
    min: [0, 'Quarterly price cannot be negative'],
  },
  halfYearlyPrice: {
    type: Number,
    default: 0,
    min: [0, 'Half-yearly price cannot be negative'],
  },
  yearlyPrice: {
    type: Number,
    default: 0,
    min: [0, 'Yearly price cannot be negative'],
  },
  lifetimePrice: {
    type: Number,
    default: 0,
    min: [0, 'Lifetime price cannot be negative'],
  },
  tiers: [PricingTierSchema],
  usageLimitKey: {
    type: String,
    trim: true,
    default: null,
  },
  defaultLimit: {
    type: Number,
    default: 0,
    min: [0, 'Default limit cannot be negative'],
  },
  metadata: {
    type: Schema.Types.Mixed,
    default: {},
  },
  isActive: {
    type: Boolean,
    default: true,
  },
  sortOrder: {
    type: Number,
    default: 0,
  },
}, {
  timestamps: true,
  toJSON: { virtuals: true },
  toObject: { virtuals: true },
});

PricingItemSchema.index({ slug: 1 }, { unique: true });
PricingItemSchema.index({ categorySlug: 1, sortOrder: 1 });
PricingItemSchema.index({ parentItemSlug: 1 });
PricingItemSchema.index({ isActive: 1 });

export const PricingItem = mongoose.models.PricingItem || mongoose.model<IPricingItem>('PricingItem', PricingItemSchema);