package kz.konturai.parser.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * AI-предсказание эффективности рекламной кампании.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "campaign_predictions")
public class CampaignPrediction {

    @Id
    private String id;

    @Indexed
    @Field("campaign_id")
    private String campaignId;

    @Field("created_at")
    private LocalDateTime createdAt;

    @Field("is_latest")
    private boolean isLatest;

    @Field("model_version")
    @Builder.Default
    private String modelVersion = "claude-sonnet-4-20250514";

    // ── Основные показатели ──────────────────────────────────────────────
    @Field("prediction_score")
    private Integer predictionScore;       // 0-100

    @Field("prediction_label")
    private String predictionLabel;        // Отличная / Хорошая / Средняя / Слабая

    @Field("confidence")
    private Integer confidence;            // 0-100

    // ── Прогнозируемые метрики ───────────────────────────────────────────
    @Field("predicted_metrics")
    private Map<String, MetricRange> predictedMetrics;

    // ── Прогноз по неделям ───────────────────────────────────────────────
    @Field("weekly_forecast")
    private List<WeeklyForecast> weeklyForecast;

    // ── Факторы и рекомендации ───────────────────────────────────────────
    @Field("strength_factors")
    private List<String> strengthFactors;

    @Field("risk_factors")
    private List<String> riskFactors;

    @Field("top_recommendations")
    private List<Recommendation> topRecommendations;

    // ── Аудитория и бюджет ───────────────────────────────────────────────
    @Field("audience_insights")
    private AudienceInsights audienceInsights;

    @Field("budget_optimization")
    private BudgetOptimizationInsights budgetOptimization;

    @Field("competitive_analysis")
    private CompetitiveAnalysis competitiveAnalysis;

    // ── Вложенные классы ─────────────────────────────────────────────────

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MetricRange {
        private Double min;
        private Double max;
        private String unit;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class WeeklyForecast {
        private Integer week;
        private Double expectedSpend;
        private Integer expectedLeads;
        private Integer expectedReach;
        private String phase;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Recommendation {
        private String priority;        // HIGH / MEDIUM / LOW
        private String category;        // CREATIVE / AUDIENCE / BUDGET / TIMING / PLATFORM
        private String title;
        private String description;
        private String expectedImpact;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class AudienceInsights {
        private String bestPerformingSegment;
        private String recommendedExpansion;
        private String exclusionAdvice;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class BudgetOptimizationInsights {
        private String currentAllocation;
        private String recommendedReallocation;
        private String potentialCplReduction;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class CompetitiveAnalysis {
        private String marketSaturation;        // HIGH / MEDIUM / LOW
        private Integer differentiationScore;   // 0-100
        private List<String> competitiveAdvantages;
    }
}
