# ============================================================
# File: _solve_ode_definitions.py
# Topic: ODE Definitions & Notation (Order, Linearity, Variables)
# ============================================================

from __future__ import annotations
import random
from typing import Any, Dict, Optional
import sympy as sp

def solve_ode_definitions(
    *,
    simplify: bool = True,
) -> dict:
    """
    Generates a problem asking to identify the Order, Linearity, and
    Variables (Dependent/Independent) of a given ODE.
    """

    # List of tuples: (Latex_Equation, Order, Is_Linear, Dep_Var, Indep_Var, Explanation_Note)
    # We use raw strings for LaTeX to avoid escape issues.
    problems = [
        # Linear Examples
        (r"\frac{dy}{dx} + 5y = e^x",
         1, True, "y", "x",
         "The highest derivative is the first derivative. All y terms are to the first power."),

        (r"y'' - 3y' + 2y = 0",
         2, True, "y", "x",
         "The highest derivative is the second derivative ($y''$). Coefficients are constants."),

        (r"x^2 \frac{d^3y}{dx^3} - \frac{dy}{dx} + 4y = \sin(x)",
         3, True, "y", "x",
         "Order is 3. It is linear because $y$ and its derivatives appear to the first power, even though coefficients like $x^2$ are variable."),

        (r"\frac{d^4 x}{dt^4} = t^2",
         4, True, "x", "t",
         "The dependent variable is $x$ and independent is $t$. Highest derivative is 4."),

        # Nonlinear Examples
        (r"y' + y^2 = x",
         1, False, "y", "x",
         "It is nonlinear because of the $y^2$ term."),

        (r"y'' + \sin(y) = 0",
         2, False, "y", "x",
         "It is nonlinear because $y$ is inside a trigonometric function $\\sin(y)$."),

        (r"\left(\frac{dy}{dx}\right)^2 + y = 1",
         1, False, "y", "x",
         "It is nonlinear because the derivative $\\frac{dy}{dx}$ is squared."),

        (r"y y' = x",
         1, False, "y", "x",
         "It is nonlinear because the dependent variable $y$ is multiplied by its derivative $y'$."),

        (r"\frac{d^2y}{dx^2} + x y = \ln(y)",
         2, False, "y", "x",
         "It is nonlinear due to the $\\ln(y)$ term."),

        # Notation variations
        (r"\dot{x} + 2x = t",
         1, True, "x", "t",
         "The dot notation $\\dot{x}$ usually implies differentiation with respect to time $t$."),

        (r"\ddot{u} + \omega^2 u = 0",
         2, True, "u", "t",
         "$\\ddot{u}$ represents the second derivative with respect to time."),
    ]

    # Select a random problem
    eq_str, order, is_linear, dep, indep, note = random.choice(problems)

    linearity_str = "Linear" if is_linear else "Nonlinear"

    # --- Problem Statement ---
    problem_s = (
        f"Analyze the following Ordinary Differential Equation (ODE):<br>"
        f"$$ {eq_str} $$"
        f"Identify the following properties:"
        f"<ul>"
        f"<li><b>Order</b></li>"
        f"<li><b>Linearity</b> (Linear or Nonlinear)</li>"
        f"<li><b>Dependent Variable</b></li>"
        f"<li><b>Independent Variable</b></li>"
        f"</ul>"
    )

    # --- Solution Steps ---
    soln_s = (
        "<div class='wrap'><div class='card'>"
        "<div class='badge'>ODE Classification</div>"
        "<h3>Analysis</h3>"
        "<ul>"
        f"<li><b>Order:</b> The highest derivative appearing in the equation is <b>{order}</b>.</li>"
        f"<li><b>Linearity:</b> The equation is <b>{linearity_str}</b>. <br><i>Reason: {note}</i></li>"
        f"<li><b>Variables:</b>"
        f"<ul><li>Dependent: \\({dep}\\) (the function being differentiated)</li>"
        f"<li>Independent: \\({indep}\\) (the variable we differentiate with respect to)</li></ul>"
        f"</li>"
        "</ul>"
        "</div></div>"
    )

    # --- Final Answer ---
    answer_s = (
        f"Order: {order}, "
        f"Linearity: {linearity_str}, "
        f"Variables: {dep}({indep})"
    )

    return {
        "problem_s": problem_s,
        "soln_s": soln_s,
        "answer_s": answer_s
    }