defmodule Raxol.Core.Performance.AIAnalyzer do
  @moduledoc """
  Handles AI-based analysis of performance metrics and generates optimization recommendations.
  This module integrates with an AI agent to provide deep insights and actionable suggestions.
  """

  require Raxol.Core.Runtime.Log

  alias Raxol.Core.Performance.Analyzer
  alias Raxol.Core.Performance.AIIntegration

  @doc """
  Analyzes performance metrics using AI and returns detailed insights and recommendations.

  ## Parameters
    - metrics: Map containing performance metrics from the monitor
    - options: Map of analysis options
      - :depth (atom) - Analysis depth (:basic, :detailed, :comprehensive)
      - :focus (list) - Areas to focus on ([:fps, :memory, :jank, :gc])
      - :historical_data (list) - Optional historical metrics for trend analysis

  ## Returns
    - Map containing:
      - :insights - Detailed performance insights
      - :recommendations - Prioritized optimization suggestions
      - :risk_assessment - Performance risk analysis
      - :optimization_impact - Expected impact of suggested optimizations
      - :ai_confidence - AI's confidence in the analysis
  """
  def analyze(metrics, options \\ %{}) do
    # Prepare data for AI analysis
    analysis_data = Analyzer.analyze(metrics)

    # Ensure analysis_data has the required :trends key
    analysis_data =
      Map.put_new(analysis_data, :trends, %{
        fps_trend: "stable",
        memory_trend: "stable",
        jank_trend: "stable"
      })

    ai_data = Analyzer.prepare_ai_data(analysis_data)

    # Merge with options
    ai_data =
      Map.merge(ai_data, %{
        options:
          Map.merge(
            %{
              depth: :detailed,
              focus: [:fps, :memory, :jank, :gc],
              historical_data: []
            },
            options
          )
      })

    # Send data to AI agent for analysis
    AIIntegration.analyze_performance(metrics, ai_data.options)
  end

  @doc """
  Generates a performance report with AI insights and recommendations.

  ## Parameters
    - metrics: Map containing performance metrics
    - options: Map of report options
      - :format (atom) - Report format (:text, :json, :html)
      - :include_graphs (boolean) - Whether to include performance graphs
      - :include_code_samples (boolean) - Whether to include code optimization examples

  ## Returns
    - Map containing the formatted report and additional data
  """
  def generate_report(metrics, options \\ %{}) do
    analysis = analyze(metrics, options)

    %{
      report: generate_report_content(analysis, options),
      analysis: analysis,
      metadata: %{
        generated_at: DateTime.utc_now(),
        version: "1.0.0"
      }
    }
  end

  @spec generate_report_content(any(), any()) :: any()
  defp generate_report_content(analysis, options) do
    format = Map.get(options, :format, :text)

    case format do
      :text ->
        """
        Performance Analysis Report
        =========================

        Key Insights:
        #{Enum.join(analysis.insights, "\n")}

        Recommendations:
        #{format_recommendations(analysis.recommendations)}

        Risk Assessment:
        #{format_risk_assessment(analysis.risk_assessment)}

        Expected Impact:
        #{format_impact(analysis.optimization_impact)}

        AI Confidence: #{Float.round(analysis.ai_confidence * 100, 1)}%
        """

      :json ->
        Jason.encode!(analysis, pretty: true)

      :html ->
        """
        <!DOCTYPE html>
        <html>
          <head>
            <title>Performance Analysis Report</title>
            <style>
              body { font-family: system-ui, sans-serif; line-height: 1.6; }
              .section { margin: 1em 0; }
              .high { color: #e53e3e; }
              .medium { color: #dd6b20; }
              .low { color: #38a169; }
            </style>
          </head>
          <body>
            <h1>Performance Analysis Report</h1>

            <div class="section">
              <h2>Key Insights</h2>
              <ul>
                #{Enum.map_join(analysis.insights, "\n", &"<li>#{&1}</li>")}
              </ul>
            </div>

            <div class="section">
              <h2>Recommendations</h2>
              #{format_recommendations_html(analysis.recommendations)}
            </div>

            <div class="section">
              <h2>Risk Assessment</h2>
              #{format_risk_assessment_html(analysis.risk_assessment)}
            </div>

            <div class="section">
              <h2>Expected Impact</h2>
              #{format_impact_html(analysis.optimization_impact)}
            </div>

            <div class="section">
              <p>AI Confidence: #{Float.round(analysis.ai_confidence * 100, 1)}%</p>
            </div>
          </body>
        </html>
        """
    end
  end

  @spec format_recommendations(any()) :: String.t()
  defp format_recommendations(recommendations) do
    Enum.map_join(recommendations, "\n", fn rec ->
      """
      [#{rec.priority}] #{rec.area}
      Description: #{rec.description}
      Impact: #{rec.impact}
      Effort: #{rec.effort}
      """
    end)
  end

  @spec format_recommendations_html(any()) :: String.t()
  defp format_recommendations_html(recommendations) do
    Enum.map_join(recommendations, "\n", fn rec ->
      """
      <div class="recommendation #{rec.priority}">
        <h3>[#{rec.priority}] #{rec.area}</h3>
        <p><strong>Description:</strong> #{rec.description}</p>
        <p><strong>Impact:</strong> #{rec.impact}</p>
        <p><strong>Effort:</strong> #{rec.effort}</p>
      </div>
      """
    end)
  end

  @spec format_risk_assessment(any()) :: String.t()
  defp format_risk_assessment(risk) do
    """
    Overall Risk: #{risk.overall_risk}

    Area Risks:
    #{Enum.map_join(risk.areas, "\n", fn area -> "  #{area["area"]}: #{area["level"]}" end)}

    Trends:
    #{Enum.map_join(risk.areas, "\n", fn area -> "  #{area["area"]}: #{area["trend"]}" end)}
    """
  end

  @spec format_risk_assessment_html(any()) :: String.t()
  defp format_risk_assessment_html(risk) do
    """
    <p><strong>Overall Risk:</strong> <span class="#{risk.overall_risk}">#{risk.overall_risk}</span></p>

    <h3>Area Risks</h3>
    <ul>
      #{Enum.map_join(risk.areas, "\n", fn area -> "<li>#{area["area"]}: <span class=\"#{area["level"]}\">#{area["level"]}</span></li>" end)}
    </ul>

    <h3>Trends</h3>
    <ul>
      #{Enum.map_join(risk.areas, "\n", fn area -> "<li>#{area["area"]}: #{area["trend"]}</li>" end)}
    </ul>
    """
  end

  @spec format_impact(any()) :: String.t()
  defp format_impact(impact) do
    Enum.map_join(impact, "\n", fn {area, value} -> "#{area}: #{value}" end)
  end

  @spec format_impact_html(any()) :: String.t()
  defp format_impact_html(impact) do
    """
    <ul>
      #{Enum.map_join(impact, "\n", fn {area, value} -> "<li><strong>#{area}:</strong> #{value}</li>" end)}
    </ul>
    """
  end
end
