# ============================================================
# File: _solve_pde_definitions.py
# Topic: PDE Definitions & Notation (Order, Linearity, Unknown Function)
# ============================================================

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

def solve_pde_definitions(
    *,
    simplify: bool = True,
) -> dict:
    """
    Generates a problem asking to identify the Order, Linearity, and
    Unknown Function (with its independent variables) of a given PDE.
    """

    # List of tuples: (Latex_Equation, Order, Is_Linear, Unknown_Func, Indep_Vars, Explanation_Note)
    # We use r"..." (raw strings) for both the equation AND the explanation
    # to handle LaTeX backslashes correctly.
    problems = [
        # --- Standard Linear PDEs ---
        (r"\frac{\partial u}{\partial t} = k \frac{\partial^2 u}{\partial x^2}",
         2, True, "u", "x, t",
         r"This is the Heat Equation. The highest derivative is 2nd order (in x). It is linear."),

        (r"\frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2}",
         2, True, "u", "x, t",
         r"This is the Wave Equation. Highest derivative is 2nd order. It is linear."),

        (r"\frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} = 0",
         2, True, "u", "x, y",
         r"This is Laplace's Equation. It is linear and 2nd order."),

        (r"u_t + c u_x = 0",
         1, True, "u", "x, t",
         r"This is the Linear Transport Equation (if c is constant). 1st order."),

        (r"\frac{\partial^4 w}{\partial x^4} + \frac{\partial^4 w}{\partial y^4} = f(x,y)",
         4, True, "w", "x, y",
         r"This is a 4th order linear PDE (often used in plate mechanics)."),

        # --- Nonlinear PDEs ---
        (r"\frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} = 0",
         1, False, "u", "x, t",
         r"This is the Inviscid Burgers' Equation. It is <b>nonlinear</b> because of the term $u \cdot u_x$ (product of unknown and its derivative)."),

        (r"(\frac{\partial u}{\partial x})^2 + (\frac{\partial u}{\partial y})^2 = 1",
         1, False, "u", "x, y",
         r"This is the Eikonal Equation. It is <b>nonlinear</b> because the first derivatives are squared."),

        (r"\frac{\partial^2 u}{\partial t^2} - \frac{\partial^2 u}{\partial x^2} + \sin(u) = 0",
         2, False, "u", "x, t",
         r"This is the Sine-Gordon Equation. It is <b>nonlinear</b> because $u$ appears inside the sine function."),

        (r"\frac{\partial u}{\partial t} = \frac{\partial^2 u}{\partial x^2} + u^3",
         2, False, "u", "x, t",
         r"It is <b>nonlinear</b> due to the $u^3$ term."),

        # --- Different Notation / Variables ---
        (r"V_{xx} + V_{yy} + V_{zz} = -\frac{\rho}{\epsilon_0}",
         2, True, "V", "x, y, z",
         r"This is Poisson's Equation in 3D. Subscript notation $V_{xx}$ means $\partial^2 V / \partial x^2$."),

        (r"\frac{\partial \Psi}{\partial t} = i \hbar \frac{\partial^2 \Psi}{\partial x^2}",
         2, True, "Psi", "x, t",
         r"Schrödinger Equation form. Linear, 2nd order."),
    ]

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

    linearity_str = "Linear" if is_linear else "Nonlinear"

    # --- Problem Statement ---
    problem_s = (
        f"Analyze the following Partial Differential Equation (PDE):<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>Unknown Function</b> (and its Independent Variables)</li>"
        f"</ul>"
    )

    # --- Solution Steps ---
    soln_s = (
        "<div class='wrap'><div class='card'>"
        "<div class='badge'>PDE Analysis</div>"
        "<h3>Analysis</h3>"
        "<ul>"
        f"<li><b>Order:</b> The highest partial derivative appearing is <b>{order}</b>.</li>"
        f"<li><b>Linearity:</b> The equation is <b>{linearity_str}</b>. <br><i>Note: {note}</i></li>"
        f"<li><b>Unknown Function:</b> \\({func}\\) <br>"
        f"It depends on the variables: \\( ({indep}) \\).</li>"
        "</ul>"
        "</div></div>"
    )

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

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