"""
Evolution orchestration data models
"""

from dataclasses import dataclass, field
from typing import Dict, List, Any, Optional
import time

from .performance import PeriodMetrics
from .decision import ImprovementPlan


@dataclass
class OptimizationGoal:
    """
    Optimization goal specification

    Attributes:
        target_metric: Target metric name ('sharpe_ratio' | 'win_rate' | 'profit_factor' | 'max_drawdown')
        target_value: Target value to achieve
        description: Goal description
    """

    target_metric: str  # 'sharpe_ratio' | 'win_rate' | 'profit_factor' | 'max_drawdown'
    target_value: float
    description: str


@dataclass
class EvolutionConfig:
    """
    Evolution configuration

    Attributes:
        symbol: Trading pair symbol
        timeframe: Timeframe
        max_iterations: Maximum number of iterations
        workspace_path: Workspace directory path
        optimization_goal: Optimization goal (optional)
    """

    symbol: str
    timeframe: str
    max_iterations: int = 100
    workspace_path: str = "."
    optimization_goal: Optional[OptimizationGoal] = None


@dataclass
class EvolutionState:
    """
    Current evolution state

    Attributes:
        status: Current status ('idle' | 'running' | 'paused' | 'completed' | 'failed' | 'pending_review')
        current_strategy: Current strategy configuration
        current_metrics: Current performance metrics
        optimization_goal: Optimization goal
        start_time: Evolution start timestamp
    """

    status: str = (
        "idle"  # 'idle' | 'running' | 'paused' | 'completed' | 'failed' | 'pending_review'
    )
    current_strategy: Optional[Dict[str, Any]] = None
    current_metrics: Optional[PeriodMetrics] = None
    optimization_goal: Optional[OptimizationGoal] = None
    start_time: Optional[int] = None


@dataclass
class ModificationResult:
    """
    Code modification result

    Attributes:
        success: Whether modification was successful
        modified_files: List of modified file paths
        errors: List of error messages
        warnings: List of warning messages
        git_commit: Git commit SHA
        timestamp: Modification timestamp
    """

    success: bool
    modified_files: List[str]
    errors: List[str]
    warnings: List[str]
    git_commit: Optional[str]
    timestamp: int


@dataclass
class IterationResult:
    """
    Single iteration result

    Attributes:
        iteration_num: Iteration number
        status: Iteration status ('success' | 'no_improvement_needed' | 'validation_failed' | 'error')
        metrics: Performance metrics after iteration
        improvement_plan: Improvement plan used
        modification_result: Code modification result
        error: Error message if failed
        timestamp: Iteration timestamp
    """

    iteration_num: int
    status: str  # 'success' | 'no_improvement_needed' | 'validation_failed' | 'error'
    metrics: Optional[PeriodMetrics] = None
    improvement_plan: Optional[ImprovementPlan] = None
    modification_result: Optional[ModificationResult] = None
    error: Optional[str] = None
    timestamp: int = field(default_factory=lambda: int(time.time()))


@dataclass
class FinalReport:
    """
    Final evolution report

    Attributes:
        start_time: Evolution start timestamp
        end_time: Evolution end timestamp
        total_iterations: Total number of iterations
        successful_iterations: Number of successful iterations
        initial_metrics: Initial performance metrics
        final_metrics: Final performance metrics
        optimization_goal: Optimization goal
        goal_achieved: Whether goal was achieved
        iteration_summary: Summary of all iterations
    """

    start_time: int
    end_time: int
    total_iterations: int
    successful_iterations: int
    initial_metrics: Optional[PeriodMetrics]
    final_metrics: Optional[PeriodMetrics]
    optimization_goal: OptimizationGoal
    goal_achieved: bool
    iteration_summary: List[Dict[str, Any]]


@dataclass
class EvolutionResult:
    """
    Complete evolution result

    Attributes:
        success: Whether evolution was successful
        final_strategy: Final strategy configuration
        iterations: Number of iterations completed
        final_metrics: Final performance metrics
        report: Final report
    """

    success: bool
    final_strategy: Dict[str, Any]
    iterations: int
    final_metrics: PeriodMetrics
    report: FinalReport
