import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, Target, Grid } from "lucide-react";
import RationalExplorer from "../../../components/lessons/RationalExplorer";
import RadicalSolutionWidget from "../../../components/lessons/RadicalSolutionWidget";
import Quiz from "../../../components/lessons/Quiz";
import { ADV_RATIONAL_QUIZ } from "../../../utils/constants";
import { Frac } from "../../../components/Math";

interface LessonProps {
  onFinish?: () => void;
}

const RationalRadicalLesson: React.FC<LessonProps> = ({ onFinish }) => {
  const [activeSection, setActiveSection] = useState(0);
  const sectionsRef = useRef<(HTMLElement | null)[]>([]);

  const scrollToSection = (index: number) => {
    setActiveSection(index);
    sectionsRef.current[index]?.scrollIntoView({
      behavior: "smooth",
      block: "start",
    });
  };

  useEffect(() => {
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            const index = sectionsRef.current.indexOf(
              entry.target as HTMLElement,
            );
            if (index !== -1) setActiveSection(index);
          }
        });
      },
      { rootMargin: "-20% 0px -60% 0px" },
    );
    sectionsRef.current.forEach((section) => {
      if (section) observer.observe(section);
    });
    return () => observer.disconnect();
  }, []);

  const SectionMarker = ({
    index,
    title,
    icon: Icon,
  }: {
    index: number;
    title: string;
    icon: any;
  }) => {
    const isActive = activeSection === index;
    const isPast = activeSection > index;
    return (
      <button
        onClick={() => scrollToSection(index)}
        className={`flex items-center gap-3 p-3 w-full rounded-lg transition-all ${isActive ? "bg-white shadow-md border border-violet-100" : "hover:bg-slate-100"}`}
      >
        <div
          className={`w-8 h-8 rounded-full flex items-center justify-center shrink-0 ${isActive ? "bg-violet-600 text-white" : isPast ? "bg-violet-400 text-white" : "bg-slate-200 text-slate-500"}`}
        >
          {isPast ? (
            <Check className="w-4 h-4" />
          ) : (
            <Icon className="w-4 h-4" />
          )}
        </div>
        <div className="text-left">
          <p
            className={`text-sm font-bold ${isActive ? "text-violet-900" : "text-slate-600"}`}
          >
            {title}
          </p>
        </div>
      </button>
    );
  };

  return (
    <div className="flex flex-col lg:flex-row min-h-screen">
      <aside className="w-full lg:w-64 lg:fixed lg:top-20 lg:bottom-0 lg:overflow-y-auto p-4 border-r border-slate-200 bg-slate-50 lg:bg-transparent z-0 hidden lg:block">
        <nav className="space-y-2">
          <SectionMarker index={0} title="Rational Functions" icon={Grid} />
          <SectionMarker index={1} title="Radical Equations" icon={Target} />
          <SectionMarker index={2} title="Practice" icon={BookOpen} />
        </nav>
      </aside>

      <div className="flex-1 lg:ml-64 p-6 md:p-12 max-w-4xl mx-auto">
        {/* Section 1: Rational Functions */}
        <section
          ref={(el) => {
            sectionsRef.current[0] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24 pt-20 lg:pt-0"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Rational Functions & Discontinuities
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              A <strong>rational function</strong> is any function of the form
              f(x) = P(x) ÷ Q(x), where both P and Q are polynomials. Wherever
              Q(x) = 0, the function is undefined — creating a{" "}
              <strong>discontinuity</strong>. There are two types of
              discontinuities: holes and vertical asymptotes.
            </p>
          </div>

          {/* Holes vs Vertical Asymptotes */}
          <div className="bg-violet-50 border border-violet-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-violet-900">
              Holes vs. Vertical Asymptotes
            </h3>

            <div className="bg-violet-100 rounded-xl p-4 text-sm">
              <p className="font-bold text-violet-900 mb-1">
                Key Step: Always Factor Both Numerator and Denominator First
              </p>
              <p className="text-slate-700">
                Once factored, look at the denominator's zeros. If a zero also
                cancels with the numerator → <strong>hole</strong>. If it does
                not cancel → <strong>vertical asymptote</strong>.
              </p>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              <div className="bg-white rounded-xl p-5 border border-violet-100">
                <div className="text-center mb-3">
                  <span className="inline-block bg-amber-100 text-amber-800 font-bold px-3 py-1 rounded-full text-sm">
                    Hole (Removable Discontinuity)
                  </span>
                </div>
                <p className="text-slate-600 text-sm mb-2">
                  Occurs when a factor in the denominator{" "}
                  <strong>also cancels</strong> with a factor in the numerator.
                  The function is undefined at that x-value, but there's no
                  asymptote.
                </p>
                <div className="bg-amber-50 rounded-lg p-3 font-mono text-sm">
                  <p className="text-slate-600">
                    f(x) = <Frac n="(x − 2)(x + 3)" d="(x − 2)(x − 1)" />
                  </p>
                  <p className="text-slate-600">
                    After canceling: f(x) = <Frac n="x + 3" d="x − 1" />
                  </p>
                  <p className="text-amber-700 font-bold">
                    Hole at x = 2 (canceled factor)
                  </p>
                  <p className="text-violet-700 font-bold">
                    V. asymptote at x = 1 (remains)
                  </p>
                </div>
                <p className="text-slate-500 text-xs mt-2">
                  To find the y-coordinate of the hole: plug x = 2 into the
                  simplified function: f(2) = (2 + 3) ÷ (2 − 1) = 5. Hole is at
                  (2, 5).
                </p>
              </div>

              <div className="bg-white rounded-xl p-5 border border-violet-100">
                <div className="text-center mb-3">
                  <span className="inline-block bg-red-100 text-red-800 font-bold px-3 py-1 rounded-full text-sm">
                    Vertical Asymptote
                  </span>
                </div>
                <p className="text-slate-600 text-sm mb-2">
                  Occurs when a denominator factor does{" "}
                  <strong>NOT cancel</strong>. The function approaches ±∞ near
                  that x-value. The graph has a vertical line it never crosses.
                </p>
                <div className="bg-red-50 rounded-lg p-3 font-mono text-sm">
                  <p className="text-slate-600">
                    f(x) = <Frac n="x + 1" d="(x − 3)(x + 2)" />
                  </p>
                  <p className="text-slate-600">No factors cancel.</p>
                  <p className="text-red-700 font-bold">
                    V. asymptotes at x = 3 and x = −2
                  </p>
                </div>
                <p className="text-slate-500 text-xs mt-2">
                  The denominator has zeros at x = 3 and x = −2. Neither cancels
                  with the numerator, so both are vertical asymptotes.
                </p>
              </div>
            </div>

            {/* Horizontal Asymptotes */}
            <div className="bg-white rounded-xl p-5 border border-violet-100">
              <p className="font-bold text-violet-800 mb-3">
                Horizontal Asymptotes — End Behavior of Rational Functions
              </p>
              <p className="text-slate-600 text-sm mb-3">
                Compare the degree of the numerator (n) to the degree of the
                denominator (d):
              </p>
              <div className="overflow-x-auto">
                <table className="w-full text-sm border-collapse">
                  <thead>
                    <tr className="bg-violet-100 text-violet-900">
                      <th className="p-2 text-left font-bold">Condition</th>
                      <th className="p-2 text-left font-bold">
                        Horizontal Asymptote
                      </th>
                    </tr>
                  </thead>
                  <tbody className="text-slate-600">
                    <tr className="border-b border-violet-50">
                      <td className="p-2 font-semibold">
                        n &lt; d (numerator degree lower)
                      </td>
                      <td className="p-2">y = 0</td>
                    </tr>
                    <tr className="border-b border-violet-50 bg-violet-50">
                      <td className="p-2 font-semibold">n = d (same degree)</td>
                      <td className="p-2">
                        y ={" "}
                        <Frac
                          n="leading coefficient of top"
                          d="leading coefficient of bottom"
                        />
                      </td>
                    </tr>
                    <tr>
                      <td className="p-2 font-semibold">
                        n &gt; d (numerator degree higher)
                      </td>
                      <td className="p-2">
                        No horizontal asymptote (oblique asymptote instead)
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div className="mt-3 bg-violet-50 rounded-lg p-3 text-sm font-mono">
                <p className="text-slate-600">
                  f(x) = <Frac n="3x²" d="x² − 4" />: n = d = 2 → H.A. at y ={" "}
                  <Frac n="3" d="1" /> ={" "}
                  <strong className="text-violet-700">3</strong>
                </p>
              </div>
            </div>
          </div>

          <RationalExplorer />
          <button
            onClick={() => scrollToSection(1)}
            className="mt-12 group flex items-center text-violet-600 font-bold hover:text-violet-800 transition-colors"
          >
            Next: Radical Equations{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 2: Radical Equations */}
        <section
          ref={(el) => {
            sectionsRef.current[1] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Radical Equations & Extraneous Solutions
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              A <strong>radical equation</strong> has the variable under a
              square root (or other radical). The key strategy is to isolate the
              radical and then raise both sides to the appropriate power to
              eliminate it. This process can introduce{" "}
              <strong>extraneous solutions</strong> — values that satisfy the
              transformed equation but not the original. You{" "}
              <strong>must always check</strong> your answers in the original
              equation.
            </p>
          </div>

          <div className="bg-violet-50 border border-violet-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-violet-900">
              Solving Radical Equations: Step-by-Step
            </h3>

            <div className="bg-white rounded-xl p-5 border border-violet-100">
              <p className="font-bold text-violet-800 mb-3">General Process</p>
              <div className="space-y-2">
                {[
                  {
                    step: "1",
                    text: "Isolate the radical on one side of the equation.",
                  },
                  {
                    step: "2",
                    text: "Square both sides (for square roots) or cube both sides (for cube roots).",
                  },
                  {
                    step: "3",
                    text: "Solve the resulting equation (may be linear or quadratic).",
                  },
                  {
                    step: "4",
                    text: "CHECK all solutions in the ORIGINAL equation. Reject any that make the original false.",
                  },
                ].map((item) => (
                  <div key={item.step} className="flex gap-3 items-start">
                    <div className="w-6 h-6 bg-violet-600 text-white rounded-full flex items-center justify-center font-bold text-xs shrink-0">
                      {item.step}
                    </div>
                    <p className="text-slate-600 text-sm">{item.text}</p>
                  </div>
                ))}
              </div>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              <div className="bg-white rounded-xl p-5 border border-violet-100">
                <p className="font-bold text-green-700 mb-3">
                  Example 1 — No Extraneous Solution
                </p>
                <div className="font-mono text-sm space-y-1 text-slate-600">
                  <p>√(x + 3) = 5</p>
                  <p>Square both sides:</p>
                  <p>x + 3 = 25</p>
                  <p>x = 22</p>
                  <p className="text-slate-500 mt-1">
                    Check: √(22 + 3) = √25 = 5 ✓
                  </p>
                  <p className="text-green-700 font-bold">x = 22 ✓ (valid)</p>
                </div>
              </div>

              <div className="bg-white rounded-xl p-5 border border-violet-100">
                <p className="font-bold text-red-700 mb-3">
                  Example 2 — Extraneous Solution Found!
                </p>
                <div className="font-mono text-sm space-y-1 text-slate-600">
                  <p>√(2x + 1) = x − 1</p>
                  <p>Square both sides:</p>
                  <p>2x + 1 = x² − 2x + 1</p>
                  <p>0 = x² − 4x → x(x − 4) = 0</p>
                  <p>x = 0 or x = 4</p>
                  <p className="text-slate-500 mt-1">
                    Check x = 0: √1 = 0 − 1 → 1 ≠ −1 ✗
                  </p>
                  <p className="text-slate-500">
                    Check x = 4: √9 = 4 − 1 → 3 = 3 ✓
                  </p>
                  <p className="text-red-700 font-bold">
                    x = 0 is extraneous! Only x = 4.
                  </p>
                </div>
              </div>
            </div>

            {/* Why Extraneous Solutions Occur */}
            <div className="bg-violet-100 rounded-xl p-4 text-sm">
              <p className="font-bold text-violet-900 mb-1">
                Why Do Extraneous Solutions Appear?
              </p>
              <p className="text-slate-700">
                Squaring both sides is not a reversible step — if you square a
                negative number, it becomes positive. For example, if the
                original equation has √x = A, then A must be non-negative (a
                square root can never give a negative output). But squaring
                produces solutions for both A and −A. Any solution that would
                require the radical to equal a negative number is extraneous.
              </p>
            </div>

            {/* Rational Exponents */}
            <div className="bg-white rounded-xl p-5 border border-violet-100">
              <p className="font-bold text-violet-800 mb-3">
                Rational Exponents: Connecting Radicals and Powers
              </p>
              <div className="bg-violet-50 rounded-lg p-3 text-center mb-3">
                <p className="font-mono text-violet-800 font-bold text-lg">
                  x<sup>m/n</sup> = (<sup>n</sup>√x)<sup>m</sup> = <sup>n</sup>
                  √(x<sup>m</sup>)
                </p>
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-3 gap-3 text-sm">
                <div className="bg-violet-50 rounded-lg p-3 text-center">
                  <p className="font-mono text-violet-700 font-bold">
                    x<sup>1/2</sup> = √x
                  </p>
                  <p className="text-slate-500 text-xs mt-1">
                    Exponent ½ = square root
                  </p>
                </div>
                <div className="bg-violet-50 rounded-lg p-3 text-center">
                  <p className="font-mono text-violet-700 font-bold">
                    x<sup>1/3</sup> = <sup>3</sup>√x
                  </p>
                  <p className="text-slate-500 text-xs mt-1">
                    Exponent ⅓ = cube root
                  </p>
                </div>
                <div className="bg-violet-50 rounded-lg p-3 text-center">
                  <p className="font-mono text-violet-700 font-bold">
                    x<sup>2/3</sup> = (<sup>3</sup>√x)²
                  </p>
                  <p className="text-slate-500 text-xs mt-1">
                    Numerator = power, denominator = root
                  </p>
                </div>
              </div>
              <div className="mt-3 bg-violet-50 rounded-lg p-3 font-mono text-sm">
                <p className="text-slate-600">
                  Solve: x<sup>2/3</sup> = 4
                </p>
                <p className="text-slate-600">
                  Raise both sides to power 3/2: x = 4<sup>3/2</sup> = (√4)³ =
                  2³ = <strong className="text-violet-700">8</strong>
                </p>
              </div>
            </div>

            <div className="bg-red-50 border border-red-200 rounded-xl p-4 text-sm">
              <p className="font-bold text-red-800 mb-1">
                SAT Trap: The Domain of Radical Functions
              </p>
              <p className="text-slate-700">
                For even roots (square root, fourth root, etc.), the expression
                under the radical must be <strong>≥ 0</strong>. The SAT may test
                this by asking for the domain of f(x) = √(2x − 6). Set 2x − 6 ≥
                0 → x ≥ 3. The domain is x ≥ 3.
              </p>
            </div>
          </div>

          <RadicalSolutionWidget />

          <button
            onClick={() => scrollToSection(2)}
            className="mt-12 group flex items-center text-violet-600 font-bold hover:text-violet-800 transition-colors"
          >
            Next: Practice Quiz{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 3: Quiz */}
        <section
          ref={(el) => {
            sectionsRef.current[2] = el;
          }}
          className="min-h-screen flex flex-col justify-center"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-8">
            Practice Time
          </h2>
          {ADV_RATIONAL_QUIZ.map((quiz, idx) => (
            <div key={quiz.id} className="mb-12">
              <Quiz data={quiz} />
            </div>
          ))}
          <div className="p-8 bg-violet-900 rounded-2xl text-white text-center mt-12">
            <h3 className="text-2xl font-bold mb-4">Topic Mastered!</h3>
            <button
              onClick={onFinish}
              className="px-6 py-3 bg-white text-violet-900 font-bold rounded-full hover:bg-violet-50 transition-colors"
            >
              Finish Lesson ✓
            </button>
          </div>
        </section>
      </div>
    </div>
  );
};

export default RationalRadicalLesson;
