/**
 * Reputation Review Model
 * Manages customer reviews across platforms
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type ReviewPlatform =
  | 'google'
  | 'facebook'
  | 'trustpilot'
  | 'g2'
  | 'capterra'
  | 'glassdoor'
  | 'yelp'
  | 'tripadvisor'
  | 'app-store'
  | 'play-store'
  | 'other';

export type ReviewStatus = 'new' | 'responded' | 'flagged' | 'resolved';

export interface IReputationReview extends Document {
  companyId: string;
  platform: ReviewPlatform;
  reviewer: string;
  rating: number;
  title?: string;
  text: string;
  sentiment: SentimentType;
  status: ReviewStatus;
  response?: string;
  responseDate?: string;
  notes?: string;
  url?: string;
  createdAt: Date;
  updatedAt: Date;
}

// Import sentiment type from MediaMention
type SentimentType = 'positive' | 'neutral' | 'negative';

// ============================================
// MAIN SCHEMA
// ============================================

const ReputationReviewSchema = new Schema<IReputationReview>({
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  platform: {
    type: String,
    enum: ['google', 'facebook', 'trustpilot', 'g2', 'capterra', 'glassdoor', 'yelp', 'tripadvisor', 'app-store', 'play-store', 'other'],
    required: [true, 'Platform is required']
  },
  reviewer: {
    type: String,
    required: [true, 'Reviewer name is required'],
    trim: true,
    maxlength: [200, 'Reviewer name cannot exceed 200 characters']
  },
  rating: {
    type: Number,
    required: [true, 'Rating is required'],
    min: [1, 'Rating must be at least 1'],
    max: [5, 'Rating cannot exceed 5']
  },
  title: { type: String, trim: true, maxlength: 300 },
  text: {
    type: String,
    required: [true, 'Review text is required'],
    trim: true
  },
  sentiment: {
    type: String,
    enum: ['positive', 'neutral', 'negative'],
    default: 'neutral'
  },
  status: {
    type: String,
    enum: ['new', 'responded', 'flagged', 'resolved'],
    default: 'new'
  },
  response: { type: String, trim: true },
  responseDate: { type: String },
  notes: { type: String, trim: true },
  url: { type: String, trim: true },
}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

ReputationReviewSchema.index({ companyId: 1, platform: 1 });
ReputationReviewSchema.index({ companyId: 1, status: 1 });
ReputationReviewSchema.index({ companyId: 1, sentiment: 1 });
ReputationReviewSchema.index({ companyId: 1, rating: 1 });

// ============================================
// EXPORT
// ============================================

export const ReputationReview = mongoose.model<IReputationReview>('ReputationReview', ReputationReviewSchema);