import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, Grid, RefreshCw } from "lucide-react";
import SystemVisualizerWidget from "../../../components/lessons/SystemVisualizerWidget";
import Quiz from "../../../components/lessons/Quiz";
import { SYSTEMS_QUIZ_DATA } from "../../../utils/constants";
import { Frac } from "../../../components/Math";

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

const SystemsEquationsLesson: 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-blue-100" : "hover:bg-slate-100"}`}
      >
        <div
          className={`w-8 h-8 rounded-full flex items-center justify-center shrink-0 ${isActive ? "bg-blue-600 text-white" : isPast ? "bg-blue-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-blue-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="Number of Solutions" icon={Grid} />
          <SectionMarker index={1} title="Solving Methods" icon={RefreshCw} />
          <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: Number of Solutions */}
        <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">
            Systems of Equations: Number of Solutions
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              A system of two linear equations represents two lines on a graph.
              The <strong>number of solutions</strong> tells you how those lines
              relate geometrically and algebraically. Every SAT test includes at
              least one question asking you to identify how many solutions a
              system has, or to find a constant that produces a specific
              outcome.
            </p>
          </div>

          <div className="bg-blue-50 border border-blue-200 rounded-2xl p-6 mb-8 space-y-4">
            <h3 className="text-lg font-bold text-blue-900">
              The Three Possible Outcomes
            </h3>

            <div className="overflow-x-auto">
              <table className="w-full text-sm border-collapse">
                <thead>
                  <tr className="bg-blue-900 text-white">
                    <th className="p-3 text-left rounded-tl-lg">Outcome</th>
                    <th className="p-3 text-left">Geometric Picture</th>
                    <th className="p-3 text-left">Algebraic Condition</th>
                    <th className="p-3 text-left rounded-tr-lg">Example</th>
                  </tr>
                </thead>
                <tbody className="divide-y divide-blue-100">
                  <tr className="bg-blue-100">
                    <td className="p-3 font-bold text-blue-900">
                      One Solution
                    </td>
                    <td className="p-3 text-slate-700">
                      Lines intersect at exactly one point
                    </td>
                    <td className="p-3 text-slate-700">Different slopes</td>
                    <td className="p-3 font-mono text-xs text-slate-700">
                      y = 2x + 1<br />y = −x + 4
                    </td>
                  </tr>
                  <tr className="bg-red-50">
                    <td className="p-3 font-bold text-red-900">No Solution</td>
                    <td className="p-3 text-slate-700">
                      Lines are parallel — never meet
                    </td>
                    <td className="p-3 text-slate-700">
                      Same slope, different y-intercept
                    </td>
                    <td className="p-3 font-mono text-xs text-slate-700">
                      y = 2x + 1<br />y = 2x + 5
                    </td>
                  </tr>
                  <tr className="bg-emerald-50">
                    <td className="p-3 font-bold text-emerald-900">
                      Infinite Solutions
                    </td>
                    <td className="p-3 text-slate-700">
                      Same line — perfectly overlap
                    </td>
                    <td className="p-3 text-slate-700">
                      Same slope AND same y-intercept (equations are multiples)
                    </td>
                    <td className="p-3 font-mono text-xs text-slate-700">
                      y = 2x + 1<br />
                      2y = 4x + 2
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>

            {/* Finding k for specific number of solutions */}
            <div className="bg-white rounded-xl p-5 border border-blue-100">
              <p className="font-bold text-blue-800 mb-3">
                SAT Technique: Finding k for a Specific Number of Solutions
              </p>
              <div className="space-y-4">
                <div className="bg-blue-50 rounded-lg p-4 text-sm">
                  <p className="font-semibold text-blue-800 mb-2">
                    Example: For what value of k does 2x + ky = 6 and 4x + 2y =
                    12 have infinite solutions?
                  </p>
                  <div className="font-mono space-y-1 text-slate-700">
                    <p>
                      For infinite solutions, equations must be proportional.
                    </p>
                    <p>
                      Ratio of x-coefficients: <Frac n="4" d="2" /> = 2
                    </p>
                    <p>
                      So all coefficients must scale by 2: k must satisfy{" "}
                      <Frac n="2k" d="2" /> = 2, so k = 2.
                    </p>
                    <p>
                      Check constants: <Frac n="12" d="6" /> = 2 ✓
                    </p>
                    <p className="text-blue-700 font-bold">
                      k = 2 → infinite solutions
                    </p>
                  </div>
                </div>
                <div className="bg-red-50 rounded-lg p-4 text-sm">
                  <p className="font-semibold text-red-800 mb-2">
                    Example: For what value of k does 3x + 2y = 8 and kx + 4y =
                    5 have no solution?
                  </p>
                  <div className="font-mono space-y-1 text-slate-700">
                    <p>No solution → same slope, different intercept.</p>
                    <p>
                      Same slope means coefficient ratios match for x and y:{" "}
                      <Frac n="k" d="3" /> = <Frac n="4" d="2" /> = 2
                    </p>
                    <p>
                      So k = 6. Check constants: <Frac n="5" d="8" /> ≠ 2 ✓
                      (different, confirming no solution)
                    </p>
                    <p className="text-red-700 font-bold">
                      k = 6 → no solution
                    </p>
                  </div>
                </div>
              </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">
                Critical Distinction: No Solution vs. Infinite Solutions
              </p>
              <p className="text-slate-700">
                <strong>No solution</strong>: coefficients are proportional but
                constants are NOT. (Same slope, different lines.)
                <br />
                <strong>Infinite solutions</strong>: coefficients AND constants
                are proportional. (Same line, just written differently.) Divide
                one equation by the other and everything must cancel cleanly.
              </p>
            </div>
          </div>

          <SystemVisualizerWidget />

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

        {/* Section 2: Solving Methods */}
        <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">
            Solving Methods
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-6">
            <p>
              The SAT presents systems in many formats. Choose your method based
              on what form the equations are already in — don't waste time
              converting unless necessary.
            </p>
          </div>

          <div className="bg-blue-50 border border-blue-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-blue-900">
              Method 1: Substitution
            </h3>
            <div className="bg-white rounded-xl p-5 border border-blue-100">
              <p className="text-slate-600 text-sm mb-3">
                <strong>Best when:</strong> one variable is already isolated
                (e.g., y = 2x − 5) or easy to isolate.
              </p>
              <p className="text-slate-600 text-sm mb-3">
                <strong>Process:</strong> Plug one equation into the other to
                create a single-variable equation.
              </p>
              <div className="bg-blue-50 rounded-lg p-4 font-mono text-sm space-y-1 text-slate-700">
                <p>Given: y = 2x − 5 and x + y = 7</p>
                <p>Substitute y = 2x − 5 into x + y = 7:</p>
                <p>x + (2x − 5) = 7</p>
                <p>3x = 12 → x = 4</p>
                <p>y = 2(4) − 5 = 3</p>
                <p className="text-blue-700 font-bold">Solution: (4, 3)</p>
              </div>
            </div>

            <h3 className="text-lg font-bold text-blue-900">
              Method 2: Elimination (Addition/Subtraction)
            </h3>
            <div className="bg-white rounded-xl p-5 border border-blue-100">
              <p className="text-slate-600 text-sm mb-3">
                <strong>Best when:</strong> equations are in standard form (Ax +
                By = C) and coefficients are easy to match.
              </p>
              <p className="text-slate-600 text-sm mb-3">
                <strong>Process:</strong> Multiply one or both equations so one
                variable's coefficients are equal and opposite, then add the
                equations.
              </p>
              <div className="bg-blue-50 rounded-lg p-4 font-mono text-sm space-y-1 text-slate-700">
                <p>Given: 2x + y = 10 and 2x − y = 2</p>
                <p>Add equations (y terms cancel):</p>
                <p>4x = 12 → x = 3</p>
                <p>Back-substitute: 2(3) + y = 10 → y = 4</p>
                <p className="text-blue-700 font-bold">Solution: (3, 4)</p>
              </div>
              <div className="mt-3 bg-blue-50 rounded-lg p-4 font-mono text-sm space-y-1 text-slate-700">
                <p>Given: 3x + 2y = 16 and x + y = 7</p>
                <p>Multiply the second by 2: 2x + 2y = 14</p>
                <p>Subtract: (3x + 2y) − (2x + 2y) = 16 − 14</p>
                <p>x = 2, then y = 5</p>
                <p className="text-blue-700 font-bold">Solution: (2, 5)</p>
              </div>
            </div>

            {/* SAT Speed Tip */}
            <div className="bg-sky-50 border border-sky-200 rounded-xl p-5">
              <p className="font-bold text-sky-900 mb-2">
                SAT Speed Strategy: Sum/Difference Shortcut
              </p>
              <p className="text-slate-700 text-sm mb-3">
                If the SAT asks for a <em>combination</em> like x + y, or 2x −
                y, you can often get it directly by adding or subtracting the
                equations — without finding x and y individually.
              </p>
              <div className="bg-white rounded-lg p-3 font-mono text-sm space-y-1 text-slate-700">
                <p>Given: 3x + 2y = 14 and x + y = 6. Find 2x + y.</p>
                <p>Subtract eq2 from eq1: (3x + 2y) − (x + y) = 14 − 6</p>
                <p className="text-sky-700 font-bold">
                  2x + y = 8 ← answer directly!
                </p>
              </div>
            </div>

            {/* Word Problem Translation */}
            <div className="bg-white rounded-xl p-5 border border-blue-100">
              <p className="font-bold text-blue-800 mb-3">Word Problem Setup</p>
              <p className="text-slate-600 text-sm mb-3">
                Most SAT system word problems follow this template: define two
                variables, write two equations (one for each constraint), solve.
              </p>
              <div className="bg-blue-50 rounded-lg p-4 text-sm">
                <p className="text-slate-700 mb-2 italic">
                  "A store sells pens for $2 and notebooks for $5. A customer
                  buys 8 items total and spends $28. How many of each did they
                  buy?"
                </p>
                <div className="font-mono space-y-1 text-slate-700">
                  <p>Let p = pens, n = notebooks</p>
                  <p>p + n = 8 (total items)</p>
                  <p>2p + 5n = 28 (total cost)</p>
                  <p>From first: p = 8 − n. Substitute: 2(8 − n) + 5n = 28</p>
                  <p>
                    16 − 2n + 5n = 28 → 3n = 12 →{" "}
                    <strong className="text-blue-700">
                      n = 4 notebooks, p = 4 pens
                    </strong>
                  </p>
                </div>
              </div>
            </div>
          </div>

          <button
            onClick={() => scrollToSection(2)}
            className="mt-12 group flex items-center text-blue-600 font-bold hover:text-blue-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>
          {SYSTEMS_QUIZ_DATA.map((quiz, idx) => (
            <div key={quiz.id} className="mb-12">
              <Quiz data={quiz} />
            </div>
          ))}
          <div className="p-8 bg-blue-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-blue-900 font-bold rounded-full hover:bg-blue-50 transition-colors"
            >
              Finish Lesson ✓
            </button>
          </div>
        </section>
      </div>
    </div>
  );
};

export default SystemsEquationsLesson;
