/**
 * PromptConfig Model
 *
 * Stores image generation prompt configurations in MongoDB.
 * Supports multiple prompt variants per category (type + key).
 * Allows Super Admin to view, edit, and create prompt variants from the admin panel.
 * Seeded from hardcoded defaults on first startup.
 */

import mongoose, { Schema, Document, Types } from 'mongoose';

// ============================================
// TYPES
// ============================================

export type PromptConfigType =
  | 'style_guidance'
  | 'platform_guidance'
  | 'asset_category_guidance'
  | 'asset_content_elements'
  | 'system_prompt'
  | 'fallback_prompt';

export interface IPromptConfig extends Document {
  type: PromptConfigType;
  key: string;
  name: string;            // Variant name (e.g., "Default", "Professional", "Creative")
  label: string;
  description: string;      // Short description of what this prompt does
  category?: string;
  prompt: string;
  isActive: boolean;
  isDefault: boolean;       // Seeded from hardcoded defaults
  isDefaultVariant: boolean; // Is this the default variant for this (type, key)?
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SCHEMA
// ============================================

const PromptConfigSchema = new Schema<IPromptConfig>(
  {
    type: {
      type: String,
      enum: [
        'style_guidance',
        'platform_guidance',
        'asset_category_guidance',
        'asset_content_elements',
        'system_prompt',
        'fallback_prompt',
      ],
      required: true,
      index: true,
    },
    key: {
      type: String,
      required: true,
      trim: true,
    },
    name: {
      type: String,
      required: true,
      trim: true,
      default: 'Default',
    },
    label: {
      type: String,
      required: true,
      trim: true,
    },
    description: {
      type: String,
      trim: true,
      default: '',
    },
    category: {
      type: String,
      trim: true,
      default: '',
    },
    prompt: {
      type: String,
      required: true,
    },
    isActive: {
      type: Boolean,
      default: true,
    },
    isDefault: {
      type: Boolean,
      default: true,
    },
    isDefaultVariant: {
      type: Boolean,
      default: true,
    },
  },
  {
    timestamps: true,
    collection: 'promptconfigs',
  }
);

// Compound unique index on type + key + name (allows multiple variants per category)
PromptConfigSchema.index({ type: 1, key: 1, name: 1 }, { unique: true });

// Index for category-based lookups (all variants of a type+key)
PromptConfigSchema.index({ type: 1, key: 1 });
PromptConfigSchema.index({ type: 1 });
PromptConfigSchema.index({ isActive: 1 });

export const PromptConfig = mongoose.model<IPromptConfig>('PromptConfig', PromptConfigSchema);