/**
 * Complaint Model
 * Tracks customer complaints and issues
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type ComplaintSeverity = 'low' | 'medium' | 'high' | 'critical';
export type ComplaintStatus = 'new' | 'investigating' | 'in-progress' | 'resolved' | 'escalated';

export interface IComplaint extends Document {
  companyId: string;
  title: string;
  severity: ComplaintSeverity;
  description?: string;
  source?: string;
  assignedTo?: string;
  status: ComplaintStatus;
  resolutionNotes?: string;
  resolutionDate?: string;
  followUpRequired?: boolean;
  followUpDate?: string;
  tags?: string[];
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const ComplaintSchema = new Schema<IComplaint>({
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  },
  title: {
    type: String,
    required: [true, 'Complaint title is required'],
    trim: true,
    maxlength: [300, 'Title cannot exceed 300 characters']
  },
  severity: {
    type: String,
    enum: ['low', 'medium', 'high', 'critical'],
    default: 'medium'
  },
  description: { type: String, trim: true },
  source: { type: String, trim: true },
  assignedTo: { type: String, trim: true },
  status: {
    type: String,
    enum: ['new', 'investigating', 'in-progress', 'resolved', 'escalated'],
    default: 'new'
  },
  resolutionNotes: { type: String, trim: true },
  resolutionDate: { type: String },
  followUpRequired: { type: Boolean, default: false },
  followUpDate: { type: String },
  tags: [{ type: String, trim: true }],
}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

ComplaintSchema.index({ companyId: 1, status: 1 });
ComplaintSchema.index({ companyId: 1, severity: 1 });
ComplaintSchema.index({ companyId: 1, assignedTo: 1 });

// ============================================
// EXPORT
// ============================================

export const Complaint = mongoose.model<IComplaint>('Complaint', ComplaintSchema);