/**
 * GMB Post Model
 * Google Business Profile post management with offers, events, and AI generation
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type GmbPostType = 'whats-new' | 'offer' | 'event' | 'product' | 'announcement';
export type GmbPostCtaType = 'book' | 'order' | 'shop' | 'learn_more' | 'sign_up' | 'call' | 'get_offer' | 'visit' | 'custom';
export type GmbPostMediaType = 'photo' | 'video';
export type GmbPostStatus = 'draft' | 'scheduled' | 'published' | 'expired' | 'archived';

// ============================================
// MAIN INTERFACE
// ============================================

export interface IGmbPost extends Document {
  // A. Core
  companyId: string;
  locationId: string;
  type: GmbPostType;
  title?: string;
  description: string;

  // B. Call to Action
  ctaType?: GmbPostCtaType;
  ctaUrl?: string;
  ctaLabel?: string;

  // C. Media
  mediaUrls: string[];
  mediaType?: GmbPostMediaType;

  // D. Offer Details
  offerCouponCode?: string;
  offerTerms?: string;
  offerStartDate?: string;
  offerEndDate?: string;

  // E. Event Details
  eventStartDate?: string;
  eventEndDate?: string;
  eventStartTime?: string;
  eventEndTime?: string;

  // F. Product Details
  productPrice?: string;
  productUrl?: string;

  // G. Scheduling
  scheduledAt?: Date;
  publishedAt?: Date;
  expiresAt?: Date;
  status: GmbPostStatus;

  // H. Versioning
  version: number;
  versionHistory: Record<string, unknown>[];

  // I. AI Generation
  aiGenerated: boolean;
  aiPrompt?: string;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const GmbPostSchema = new Schema<IGmbPost>({
  // A. Core
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  locationId: {
    type: String,
    required: [true, 'Location ID is required'],
    index: true
  },
  type: {
    type: String,
    enum: ['whats-new', 'offer', 'event', 'product', 'announcement'],
    default: 'whats-new'
  },
  title: String,
  description: {
    type: String,
    required: [true, 'Description is required']
  },

  // B. Call to Action
  ctaType: {
    type: String,
    enum: ['book', 'order', 'shop', 'learn_more', 'sign_up', 'call', 'get_offer', 'visit', 'custom']
  },
  ctaUrl: String,
  ctaLabel: String,

  // C. Media
  mediaUrls: [String],
  mediaType: {
    type: String,
    enum: ['photo', 'video']
  },

  // D. Offer Details
  offerCouponCode: String,
  offerTerms: String,
  offerStartDate: String,
  offerEndDate: String,

  // E. Event Details
  eventStartDate: String,
  eventEndDate: String,
  eventStartTime: String,
  eventEndTime: String,

  // F. Product Details
  productPrice: String,
  productUrl: String,

  // G. Scheduling
  scheduledAt: Date,
  publishedAt: Date,
  expiresAt: Date,
  status: {
    type: String,
    enum: ['draft', 'scheduled', 'published', 'expired', 'archived'],
    default: 'draft'
  },

  // H. Versioning
  version: {
    type: Number,
    default: 1
  },
  versionHistory: [Schema.Types.Mixed],

  // I. AI Generation
  aiGenerated: {
    type: Boolean,
    default: false
  },
  aiPrompt: String

}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

GmbPostSchema.index({ companyId: 1 });
GmbPostSchema.index({ locationId: 1 });
GmbPostSchema.index({ status: 1 });

// ============================================
// EXPORT
// ============================================

export const GmbPost = mongoose.models.GmbPost || mongoose.model<IGmbPost>('GmbPost', GmbPostSchema);