/**
 * GMB AI Content Model
 * AI-generated content for Google Business Profile posts, offers, events, and review responses
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type GmbAiContentType = 'post' | 'offer' | 'event' | 'review-response' | 'business-description';
export type GmbAiContentPostType = 'whats-new' | 'offer' | 'event' | 'product' | 'announcement';
export type GmbAiContentTone = 'professional' | 'friendly' | 'casual' | 'formal' | 'enthusiastic';
export type GmbAiContentStatus = 'draft' | 'approved' | 'applied' | 'archived';

// ============================================
// MAIN INTERFACE
// ============================================

export interface IGmbAiContent extends Document {
  // A. Core
  companyId: string;
  locationId?: string;
  type: GmbAiContentType;
  prompt: string;
  generatedContent: string;

  // B. Post-specific
  postType?: GmbAiContentPostType;

  // C. Review Response-specific
  reviewId?: string;
  reviewRating?: number;
  reviewText?: string;

  // D. Tone & Style
  tone?: GmbAiContentTone;
  wordCount?: number;

  // E. Status & Versioning
  status: GmbAiContentStatus;
  appliedToId?: string;
  version: number;

  // F. AI Metadata
  aiModel?: string;
  tokensUsed?: number;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const GmbAiContentSchema = new Schema<IGmbAiContent>({
  // A. Core
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  locationId: String,
  type: {
    type: String,
    enum: ['post', 'offer', 'event', 'review-response', 'business-description'],
    required: [true, 'Content type is required']
  },
  prompt: {
    type: String,
    required: [true, 'Prompt is required']
  },
  generatedContent: {
    type: String,
    required: [true, 'Generated content is required']
  },

  // B. Post-specific
  postType: {
    type: String,
    enum: ['whats-new', 'offer', 'event', 'product', 'announcement']
  },

  // C. Review Response-specific
  reviewId: String,
  reviewRating: Number,
  reviewText: String,

  // D. Tone & Style
  tone: {
    type: String,
    enum: ['professional', 'friendly', 'casual', 'formal', 'enthusiastic']
  },
  wordCount: Number,

  // E. Status & Versioning
  status: {
    type: String,
    enum: ['draft', 'approved', 'applied', 'archived'],
    default: 'draft'
  },
  appliedToId: String,
  version: {
    type: Number,
    default: 1
  },

  // F. AI Metadata
  aiModel: String,
  tokensUsed: Number

}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

GmbAiContentSchema.index({ companyId: 1 });
GmbAiContentSchema.index({ type: 1 });

// ============================================
// EXPORT
// ============================================

export const GmbAiContent = mongoose.models.GmbAiContent || mongoose.model<IGmbAiContent>('GmbAiContent', GmbAiContentSchema);