/**
 * SWOT Analysis Model
 * AI-powered SWOT analysis for strategic planning and competitive assessment
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type SwotStatus = 'draft' | 'review' | 'approved' | 'archived';
export type SwotSource = 'ai-generation' | 'manual';

export interface ISwotAnalysis extends Document {
  // Core fields
  companyId: string;
  name: string;
  status: SwotStatus;
  version: number;
  source: SwotSource;

  // Input fields
  businessName: string;
  industry: string;
  businessDescription: string;
  targetMarket: string;
  productsServices: string;
  uniqueSellingProposition?: string;
  competitorInformation?: string;
  currentStrengths?: string;
  currentChallenges?: string;
  businessGoals?: string;

  // AI-generated SWOT sections
  strengths: string[];
  strengthDetails: string[];
  weaknesses: string[];
  weaknessDetails: string[];
  opportunities: string[];
  opportunityDetails: string[];
  threats: string[];
  threatDetails: string[];

  // Strategic summary
  strategicSummary?: string;
  keyRecommendations?: string[];

  // Version & regeneration tracking
  previousVersions?: {
    id: string;
    version: number;
    createdAt: string;
    summary: string;
  }[];
  regenerationHistory?: {
    date: string;
    type: string;
    sections?: string[];
    triggeredBy: string;
  }[];
  lockedSections?: string[];

  // Linked data references
  linkedData?: {
    businessProfileId?: string;
    competitorIds?: string[];
    productIds?: string[];
  };

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// MAIN SCHEMA
// ============================================

const SwotAnalysisSchema = new Schema<ISwotAnalysis>({
  // Core fields
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
  },
  name: {
    type: String,
    required: [true, 'SWOT Analysis name is required'],
    trim: true,
    maxlength: [500, 'Name cannot exceed 500 characters'],
    default: 'SWOT Analysis',
  },
  status: {
    type: String,
    enum: ['draft', 'review', 'approved', 'archived'],
    default: 'draft',
  },
  version: {
    type: Number,
    default: 1,
  },
  source: {
    type: String,
    enum: ['ai-generation', 'manual'],
    default: 'manual',
  },

  // Input fields
  businessName: {
    type: String,
    required: [true, 'Business name is required'],
    trim: true,
  },
  industry: {
    type: String,
    required: [true, 'Industry is required'],
    trim: true,
  },
  businessDescription: {
    type: String,
    required: [true, 'Business description is required'],
    trim: true,
  },
  targetMarket: {
    type: String,
    required: [true, 'Target market is required'],
    trim: true,
  },
  productsServices: {
    type: String,
    required: [true, 'Products/services is required'],
    trim: true,
  },
  uniqueSellingProposition: {
    type: String,
    trim: true,
  },
  competitorInformation: {
    type: String,
    trim: true,
  },
  currentStrengths: {
    type: String,
    trim: true,
  },
  currentChallenges: {
    type: String,
    trim: true,
  },
  businessGoals: {
    type: String,
    trim: true,
  },

  // AI-generated SWOT sections
  strengths: {
    type: [String],
    default: [],
  },
  strengthDetails: {
    type: [String],
    default: [],
  },
  weaknesses: {
    type: [String],
    default: [],
  },
  weaknessDetails: {
    type: [String],
    default: [],
  },
  opportunities: {
    type: [String],
    default: [],
  },
  opportunityDetails: {
    type: [String],
    default: [],
  },
  threats: {
    type: [String],
    default: [],
  },
  threatDetails: {
    type: [String],
    default: [],
  },

  // Strategic summary
  strategicSummary: {
    type: String,
    default: '',
    trim: true,
  },
  keyRecommendations: {
    type: [String],
    default: [],
  },

  // Version & regeneration tracking
  previousVersions: {
    type: [{
      id: { type: String },
      version: { type: Number },
      createdAt: { type: String },
      summary: { type: String },
    }],
    default: [],
  },
  regenerationHistory: {
    type: [{
      date: { type: String },
      type: { type: String },
      sections: [{ type: String }],
      triggeredBy: { type: String },
    }],
    default: [],
  },
  lockedSections: {
    type: [String],
    default: [],
  },

  // Linked data references
  linkedData: {
    businessProfileId: { type: String },
    competitorIds: [{ type: String }],
    productIds: [{ type: String }],
  },
}, {
  timestamps: true,
});

// ============================================
// INDEXES
// ============================================

SwotAnalysisSchema.index({ companyId: 1, status: 1 });
SwotAnalysisSchema.index({ companyId: 1, createdAt: -1 });

// ============================================
// EXPORT
// ============================================

export const SwotAnalysis = mongoose.models.SwotAnalysis as mongoose.Model<ISwotAnalysis>
  || mongoose.model<ISwotAnalysis>('SwotAnalysis', SwotAnalysisSchema);