/**
 * GMB Insight Model
 * Google Business Profile analytics and insights with daily/weekly/monthly aggregation
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type GmbInsightPeriod = 'daily' | 'weekly' | 'monthly';

// ============================================
// MAIN INTERFACE
// ============================================

export interface IGmbInsight extends Document {
  // A. Core
  companyId: string;
  locationId: string;
  period: GmbInsightPeriod;
  startDate: string;
  endDate: string;

  // B. Visibility
  totalViews: number;
  searchViews: number;
  mapsViews: number;

  // C. Customer Actions
  websiteClicks: number;
  phoneCalls: number;
  directionRequests: number;
  messageClicks: number;
  bookingClicks: number;
  foodOrderClicks: number;

  // D. Breakdowns
  viewsByDevice: Record<string, unknown>;
  viewsBySource: Record<string, unknown>;
  queries: Record<string, unknown>[];
  customerActions: Record<string, unknown>[];

  // E. Photo & Review Stats
  photoViews: number;
  photoCount: number;
  followerCount: number;
  reviewCount: number;
  averageRating: number;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const GmbInsightSchema = new Schema<IGmbInsight>({
  // A. Core
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  locationId: {
    type: String,
    required: [true, 'Location ID is required'],
    index: true
  },
  period: {
    type: String,
    enum: ['daily', 'weekly', 'monthly'],
    required: [true, 'Period is required']
  },
  startDate: {
    type: String,
    required: [true, 'Start date is required']
  },
  endDate: {
    type: String,
    required: [true, 'End date is required']
  },

  // B. Visibility
  totalViews: {
    type: Number,
    default: 0
  },
  searchViews: {
    type: Number,
    default: 0
  },
  mapsViews: {
    type: Number,
    default: 0
  },

  // C. Customer Actions
  websiteClicks: {
    type: Number,
    default: 0
  },
  phoneCalls: {
    type: Number,
    default: 0
  },
  directionRequests: {
    type: Number,
    default: 0
  },
  messageClicks: {
    type: Number,
    default: 0
  },
  bookingClicks: {
    type: Number,
    default: 0
  },
  foodOrderClicks: {
    type: Number,
    default: 0
  },

  // D. Breakdowns
  viewsByDevice: {
    type: Schema.Types.Mixed,
    default: {}
  },
  viewsBySource: {
    type: Schema.Types.Mixed,
    default: {}
  },
  queries: [Schema.Types.Mixed],
  customerActions: [Schema.Types.Mixed],

  // E. Photo & Review Stats
  photoViews: {
    type: Number,
    default: 0
  },
  photoCount: {
    type: Number,
    default: 0
  },
  followerCount: {
    type: Number,
    default: 0
  },
  reviewCount: {
    type: Number,
    default: 0
  },
  averageRating: {
    type: Number,
    default: 0
  }

}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

GmbInsightSchema.index({ companyId: 1 });
GmbInsightSchema.index({ locationId: 1, period: 1, startDate: 1 }, { unique: true });

// ============================================
// EXPORT
// ============================================

export const GmbInsight = mongoose.models.GmbInsight || mongoose.model<IGmbInsight>('GmbInsight', GmbInsightSchema);