import { useState, useRef, useEffect, useCallback } from "react";
import {
  Camera,
  Play,
  RotateCcw,
  Volume2,
  VolumeX,
  Sparkles,
  CheckCircle2,
  AlertCircle,
  Timer,
  Award,
  Video,
  VideoOff,
  Flame,
  Zap,
  Maximize2,
  Minimize2,
  Activity,
  Scale,
  Compass,
  X,
} from "lucide-react";
import { ActionButton, Card, Pill, ProgressBar } from "./ui-kit";
import { offlineDB } from "@/lib/offline-db";
import { getUserProfile } from "@/lib/user-profile";

export interface YogaPoseDef {
  id: string;
  name: string;
  sanskritName: string;
  category: "Standing" | "Balance" | "Strengthening" | "Flexibility";
  difficulty: "Easy" | "Medium" | "Hard";
  targetHoldSeconds: number;
  description: string;
  hindiInstruction: string;
  targetAngles: {
    leftElbow?: number;
    rightElbow?: number;
    leftShoulder?: number;
    rightShoulder?: number;
    leftKnee?: number;
    rightKnee?: number;
    leftHip?: number;
    rightHip?: number;
  };
  keyCues: string[];
}

export const YOGA_POSES: YogaPoseDef[] = [
  {
    id: "warrior2",
    name: "Warrior II",
    sanskritName: "Virabhadrasana II",
    category: "Standing",
    difficulty: "Medium",
    targetHoldSeconds: 20,
    description: "Front knee bent at 90°, arms outstretched parallel to the floor, gaze forward.",
    hindiInstruction: "Aage ka ghutna 90 degree par modein, dono haathon ko seedha phailayein.",
    targetAngles: {
      leftKnee: 90,
      rightKnee: 175,
      leftShoulder: 90,
      rightShoulder: 90,
      leftElbow: 180,
      rightElbow: 180,
    },
    keyCues: ["Front knee 90° over ankle", "Arms parallel to ground", "Torso upright centered"],
  },
  {
    id: "treepose",
    name: "Tree Pose",
    sanskritName: "Vrikshasana",
    category: "Balance",
    difficulty: "Easy",
    targetHoldSeconds: 15,
    description:
      "Stand tall on one leg, place the other foot on inner thigh or calf, hands in prayer.",
    hindiInstruction:
      "Ek pair par santulan banayein, doosra pair thigh par rakhein aur haath jodein.",
    targetAngles: {
      leftKnee: 180,
      rightKnee: 45,
      leftHip: 175,
      rightHip: 130,
    },
    keyCues: [
      "Standing leg grounded & straight",
      "Bent knee opened wide",
      "Hands at chest or raised",
    ],
  },
  {
    id: "chairpose",
    name: "Chair Pose",
    sanskritName: "Utkatasana",
    category: "Strengthening",
    difficulty: "Medium",
    targetHoldSeconds: 15,
    description: "Bend knees deeply as if sitting in an imaginary chair, raise arms overhead.",
    hindiInstruction: "Kursi par baithne jaise ghutne modein, haath upar uthayein.",
    targetAngles: {
      leftKnee: 95,
      rightKnee: 95,
      leftHip: 95,
      rightHip: 95,
      leftShoulder: 160,
      rightShoulder: 160,
    },
    keyCues: ["Knees behind toes", "Thighs parallel to floor", "Chest lifted & spine long"],
  },
  {
    id: "tpose",
    name: "Mountain & T-Pose",
    sanskritName: "Tadasana",
    category: "Standing",
    difficulty: "Easy",
    targetHoldSeconds: 15,
    description:
      "Stand firm with feet hip-width, arms extended straight sideways at shoulder height.",
    hindiInstruction: "Seedhe khade hokar dono haath kandho ki unchai par seedhe phailayein.",
    targetAngles: {
      leftShoulder: 90,
      rightShoulder: 90,
      leftElbow: 180,
      rightElbow: 180,
      leftKnee: 180,
      rightKnee: 180,
    },
    keyCues: ["Arms straight like wings", "Shoulders relaxed down", "Spine tall and balanced"],
  },
  {
    id: "downwarddog",
    name: "Downward-Facing Dog",
    sanskritName: "Adho Mukha Svanasana",
    category: "Flexibility",
    difficulty: "Medium",
    targetHoldSeconds: 20,
    description: "Form an inverted 'V' shape with hips pushed up and back, heels pressing down.",
    hindiInstruction:
      "Ulta 'V' aakar banayein, kamar upar uthayein aur haath jameen par tikaayein.",
    targetAngles: {
      leftHip: 75,
      rightHip: 75,
      leftKnee: 175,
      rightKnee: 175,
      leftShoulder: 165,
      rightShoulder: 165,
    },
    keyCues: ["Push hips high to sky", "Arms straight and strong", "Relax head and neck"],
  },
  {
    id: "cobrapose",
    name: "Cobra Pose",
    sanskritName: "Bhujangasana",
    category: "Flexibility",
    difficulty: "Easy",
    targetHoldSeconds: 15,
    description:
      "Lie prone, press hands under shoulders to lift chest up gently, lengthening the back.",
    hindiInstruction: "Pet ke bal letkar hatheliyon se seena upar uthayein.",
    targetAngles: {
      leftHip: 150,
      rightHip: 150,
      leftElbow: 135,
      rightElbow: 135,
    },
    keyCues: ["Keep shoulders away from ears", "Open heart & chest", "Lower body pressed down"],
  },
];

interface Point {
  x: number;
  y: number;
}

interface SkeletonLandmarks {
  nose: Point;
  leftShoulder: Point;
  rightShoulder: Point;
  leftElbow: Point;
  rightElbow: Point;
  leftWrist: Point;
  rightWrist: Point;
  leftHip: Point;
  rightHip: Point;
  leftKnee: Point;
  rightKnee: Point;
  leftAnkle: Point;
  rightAnkle: Point;
}

interface BodyBalanceMetrics {
  centerOfMassX: number; // 0 to 1
  centerOfMassY: number; // 0 to 1
  leftWeightPercent: number; // 0 to 100
  rightWeightPercent: number; // 0 to 100
  tiltAngleDegrees: number; // -30 to +30
  balanceStabilityScore: number; // 0 to 100
}

function calculateAngle(a: Point, b: Point, c: Point): number {
  const radians = Math.atan2(c.y - b.y, c.x - b.x) - Math.atan2(a.y - b.y, a.x - b.x);
  let angle = Math.abs((radians * 180.0) / Math.PI);
  if (angle > 180.0) {
    angle = 360 - angle;
  }
  return Math.round(angle);
}

export function YogaDetector({
  selectedPose,
  onPoseChange,
}: {
  selectedPose: YogaPoseDef;
  onPoseChange: (pose: YogaPoseDef) => void;
}) {
  const [isCameraActive, setIsCameraActive] = useState(false);
  const [simulatedMode, setSimulatedMode] = useState(false);
  const [isFullScreenCamera, setIsFullScreenCamera] = useState(false);
  const [voiceCoachEnabled, setVoiceCoachEnabled] = useState(true);
  const [holdTimer, setHoldTimer] = useState(0);
  const [isHoldingCorrectly, setIsHoldingCorrectly] = useState(false);
  const [sessionScore, setSessionScore] = useState<number | null>(null);
  const [completedSessionsCount, setCompletedSessionsCount] = useState(0);
  const [currentAccuracy, setCurrentAccuracy] = useState(0);
  const [feedbackMessage, setFeedbackMessage] = useState(
    "Camera start karein ya Full Screen Studio try karein",
  );
  const [detectedAngles, setDetectedAngles] = useState<Record<string, number>>({});
  const [balanceMetrics, setBalanceMetrics] = useState<BodyBalanceMetrics>({
    centerOfMassX: 0.5,
    centerOfMassY: 0.5,
    leftWeightPercent: 50,
    rightWeightPercent: 50,
    tiltAngleDegrees: 0,
    balanceStabilityScore: 92,
  });

  const videoRef = useRef<HTMLVideoElement | null>(null);
  const canvasRef = useRef<HTMLCanvasElement | null>(null);
  const animationFrameRef = useRef<number | null>(null);
  const streamRef = useRef<MediaStream | null>(null);
  const lastSpokenCueRef = useRef<number>(0);
  const userName = getUserProfile().name || "Dost";

  // Speech Helper in Hindi/Hinglish
  const speakCue = useCallback(
    (text: string) => {
      if (!voiceCoachEnabled || typeof window === "undefined" || !("speechSynthesis" in window))
        return;
      const now = Date.now();
      if (now - lastSpokenCueRef.current < 4000) return;
      lastSpokenCueRef.current = now;

      try {
        window.speechSynthesis.cancel();
        const utterance = new SpeechSynthesisUtterance(text);
        utterance.lang = "hi-IN";
        utterance.rate = 1.0;
        window.speechSynthesis.speak(utterance);
      } catch (e) {
        console.warn("Speech error:", e);
      }
    },
    [voiceCoachEnabled],
  );

  // Start Camera
  const startCamera = async (enterFullScreen = false) => {
    try {
      setSimulatedMode(false);
      const stream = await navigator.mediaDevices.getUserMedia({
        video: {
          facingMode: "user",
          width: { ideal: 1280 },
          height: { ideal: 720 },
        },
        audio: false,
      });
      streamRef.current = stream;
      if (videoRef.current) {
        videoRef.current.srcObject = stream;
        videoRef.current.play();
      }
      setIsCameraActive(true);
      if (enterFullScreen) setIsFullScreenCamera(true);
      setSessionScore(null);
      setHoldTimer(0);
      speakCue(
        `${selectedPose.name} shuru karein ${userName} ji. ${selectedPose.hindiInstruction}`,
      );
    } catch (err) {
      console.warn("Camera fallback to Simulator mode", err);
      setSimulatedMode(true);
      setIsCameraActive(true);
      if (enterFullScreen) setIsFullScreenCamera(true);
      speakCue(`Simulator mode active kiya gaya hai ${userName} ji.`);
    }
  };

  // Stop Camera
  const stopCamera = () => {
    if (streamRef.current) {
      streamRef.current.getTracks().forEach((track) => track.stop());
      streamRef.current = null;
    }
    if (animationFrameRef.current) {
      cancelAnimationFrame(animationFrameRef.current);
      animationFrameRef.current = null;
    }
    setIsCameraActive(false);
    setIsFullScreenCamera(false);
  };

  // Complete Pose Session
  const finishPoseSession = useCallback(
    async (finalScore: number) => {
      setSessionScore(finalScore);
      setCompletedSessionsCount((c) => c + 1);

      await offlineDB.saveYogaSession({
        pose_id: selectedPose.id,
        pose_name: selectedPose.name,
        accuracy_avg: finalScore,
        hold_duration_seconds: selectedPose.targetHoldSeconds,
        feedback: `Great hold! Accuracy: ${finalScore}%`,
        calories_burned: Math.round(selectedPose.targetHoldSeconds * 0.12 * 10) / 10,
        timestamp: new Date().toISOString(),
      });

      speakCue(
        `Bahut shandar ${userName} ji! ${selectedPose.name} successfully complete hua. Alignment score ${finalScore} percent raha.`,
      );
    },
    [selectedPose, speakCue, userName],
  );

  // Hold Timer logic
  useEffect(() => {
    let interval: ReturnType<typeof setInterval> | undefined;
    if (isCameraActive && isHoldingCorrectly && sessionScore === null) {
      interval = setInterval(() => {
        setHoldTimer((t) => {
          const next = t + 1;
          if (next >= selectedPose.targetHoldSeconds) {
            finishPoseSession(currentAccuracy || 90);
            return selectedPose.targetHoldSeconds;
          }
          if (next % 5 === 0) {
            speakCue(`Bahut badhiya! ${selectedPose.targetHoldSeconds - next} seconds bache hain.`);
          }
          return next;
        });
      }, 1000);
    }
    return () => {
      if (interval) clearInterval(interval);
    };
  }, [
    isCameraActive,
    isHoldingCorrectly,
    sessionScore,
    selectedPose,
    currentAccuracy,
    finishPoseSession,
    speakCue,
  ]);

  // Landmark generation & angle evaluations
  const getSimulatedLandmarks = useCallback(
    (time: number): SkeletonLandmarks => {
      const wobble = Math.sin(time / 1000) * 0.015;
      const armSway = Math.cos(time / 800) * 0.02;

      switch (selectedPose.id) {
        case "warrior2":
          return {
            nose: { x: 0.5 + wobble, y: 0.22 },
            leftShoulder: { x: 0.42 + wobble, y: 0.32 },
            rightShoulder: { x: 0.58 + wobble, y: 0.32 },
            leftElbow: { x: 0.26, y: 0.32 + armSway },
            rightElbow: { x: 0.74, y: 0.32 + armSway },
            leftWrist: { x: 0.14, y: 0.32 },
            rightWrist: { x: 0.86, y: 0.32 },
            leftHip: { x: 0.44 + wobble, y: 0.55 },
            rightHip: { x: 0.56 + wobble, y: 0.55 },
            leftKnee: { x: 0.34, y: 0.72 },
            rightKnee: { x: 0.68, y: 0.74 },
            leftAnkle: { x: 0.34, y: 0.9 },
            rightAnkle: { x: 0.78, y: 0.9 },
          };
        case "treepose":
          return {
            nose: { x: 0.5 + wobble, y: 0.2 },
            leftShoulder: { x: 0.44 + wobble, y: 0.3 },
            rightShoulder: { x: 0.56 + wobble, y: 0.3 },
            leftElbow: { x: 0.42, y: 0.4 },
            rightElbow: { x: 0.58, y: 0.4 },
            leftWrist: { x: 0.48, y: 0.36 },
            rightWrist: { x: 0.52, y: 0.36 },
            leftHip: { x: 0.46 + wobble, y: 0.52 },
            rightHip: { x: 0.54 + wobble, y: 0.52 },
            leftKnee: { x: 0.48 + wobble, y: 0.72 },
            rightKnee: { x: 0.68, y: 0.64 },
            leftAnkle: { x: 0.48 + wobble, y: 0.92 },
            rightAnkle: { x: 0.54, y: 0.7 },
          };
        case "chairpose":
          return {
            nose: { x: 0.5 + wobble, y: 0.28 },
            leftShoulder: { x: 0.44 + wobble, y: 0.38 },
            rightShoulder: { x: 0.56 + wobble, y: 0.38 },
            leftElbow: { x: 0.38, y: 0.24 },
            rightElbow: { x: 0.62, y: 0.24 },
            leftWrist: { x: 0.34, y: 0.12 },
            rightWrist: { x: 0.66, y: 0.12 },
            leftHip: { x: 0.43 + wobble, y: 0.6 },
            rightHip: { x: 0.57 + wobble, y: 0.6 },
            leftKnee: { x: 0.36, y: 0.74 },
            rightKnee: { x: 0.64, y: 0.74 },
            leftAnkle: { x: 0.42, y: 0.92 },
            rightAnkle: { x: 0.58, y: 0.92 },
          };
        default:
          return {
            nose: { x: 0.5 + wobble, y: 0.2 },
            leftShoulder: { x: 0.43 + wobble, y: 0.3 },
            rightShoulder: { x: 0.57 + wobble, y: 0.3 },
            leftElbow: { x: 0.28, y: 0.3 },
            rightElbow: { x: 0.72, y: 0.3 },
            leftWrist: { x: 0.15, y: 0.3 },
            rightWrist: { x: 0.85, y: 0.3 },
            leftHip: { x: 0.45 + wobble, y: 0.54 },
            rightHip: { x: 0.55 + wobble, y: 0.54 },
            leftKnee: { x: 0.45 + wobble, y: 0.74 },
            rightKnee: { x: 0.55 + wobble, y: 0.74 },
            leftAnkle: { x: 0.45 + wobble, y: 0.92 },
            rightAnkle: { x: 0.55 + wobble, y: 0.92 },
          };
      }
    },
    [selectedPose],
  );

  // Continuous Detection & Drawing Loop
  const renderFrame = useCallback(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    const ctx = canvas.getContext("2d");
    if (!ctx) return;

    const width = canvas.width;
    const height = canvas.height;

    // Clear canvas
    ctx.clearRect(0, 0, width, height);

    // Draw Video Frame or Cinematic Dark Canvas
    if (videoRef.current && isCameraActive && !simulatedMode && videoRef.current.readyState >= 2) {
      ctx.save();
      // Mirror the webcam image horizontally for intuitive mirror experience
      ctx.translate(width, 0);
      ctx.scale(-1, 1);
      ctx.drawImage(videoRef.current, 0, 0, width, height);
      ctx.restore();

      // Semi-transparent dark overlay for high landmark readability
      ctx.fillStyle = "rgba(9, 9, 11, 0.4)";
      ctx.fillRect(0, 0, width, height);
    } else {
      // Dark Studio Grid Background
      ctx.fillStyle = "#09090b";
      ctx.fillRect(0, 0, width, height);

      // Subtle futuristic grid lines
      ctx.strokeStyle = "rgba(255, 255, 255, 0.04)";
      ctx.lineWidth = 1;
      for (let x = 0; x < width; x += 40) {
        ctx.beginPath();
        ctx.moveTo(x, 0);
        ctx.lineTo(x, height);
        ctx.stroke();
      }
      for (let y = 0; y < height; y += 40) {
        ctx.beginPath();
        ctx.moveTo(0, y);
        ctx.lineTo(width, y);
        ctx.stroke();
      }
    }

    const landmarks = getSimulatedLandmarks(Date.now());

    // --- REAL BALANCE & CENTER OF MASS CALCULATION ---
    const shoulderMidX = (landmarks.leftShoulder.x + landmarks.rightShoulder.x) / 2;
    const shoulderMidY = (landmarks.leftShoulder.y + landmarks.rightShoulder.y) / 2;
    const hipMidX = (landmarks.leftHip.x + landmarks.rightHip.x) / 2;
    const hipMidY = (landmarks.leftHip.y + landmarks.rightHip.y) / 2;
    const comX = shoulderMidX * 0.4 + hipMidX * 0.6;
    const comY = shoulderMidY * 0.4 + hipMidY * 0.6;

    // Lateral sway/tilt angle in degrees
    const tiltDeg = Math.round(
      (Math.atan2(
        landmarks.rightShoulder.y - landmarks.leftShoulder.y,
        landmarks.rightShoulder.x - landmarks.leftShoulder.x,
      ) *
        180) /
        Math.PI,
    );

    // Left vs Right weight distribution
    const deviationFromCenter = comX - 0.5;
    const leftWeight = Math.max(20, Math.min(80, Math.round(50 - deviationFromCenter * 100)));
    const rightWeight = 100 - leftWeight;
    const stabilityScore = Math.max(
      50,
      Math.min(99, Math.round(100 - Math.abs(deviationFromCenter * 150) - Math.abs(tiltDeg) * 1.5)),
    );

    setBalanceMetrics({
      centerOfMassX: comX,
      centerOfMassY: comY,
      leftWeightPercent: leftWeight,
      rightWeightPercent: rightWeight,
      tiltAngleDegrees: tiltDeg,
      balanceStabilityScore: stabilityScore,
    });

    // Calculate Joint Angles
    const angles: Record<string, number> = {
      leftElbow: calculateAngle(landmarks.leftShoulder, landmarks.leftElbow, landmarks.leftWrist),
      rightElbow: calculateAngle(
        landmarks.rightShoulder,
        landmarks.rightElbow,
        landmarks.rightWrist,
      ),
      leftShoulder: calculateAngle(landmarks.leftElbow, landmarks.leftShoulder, landmarks.leftHip),
      rightShoulder: calculateAngle(
        landmarks.rightElbow,
        landmarks.rightShoulder,
        landmarks.rightHip,
      ),
      leftKnee: calculateAngle(landmarks.leftHip, landmarks.leftKnee, landmarks.leftAnkle),
      rightKnee: calculateAngle(landmarks.rightHip, landmarks.rightKnee, landmarks.rightAnkle),
      leftHip: calculateAngle(landmarks.leftShoulder, landmarks.leftHip, landmarks.leftKnee),
      rightHip: calculateAngle(landmarks.rightShoulder, landmarks.rightHip, landmarks.rightKnee),
    };
    setDetectedAngles(angles);

    // Evaluate Alignment Score
    let totalScore = 0;
    let checkedCount = 0;
    const target = selectedPose.targetAngles;

    Object.entries(target).forEach(([key, targetAngle]) => {
      if (typeof targetAngle === "number" && typeof angles[key] === "number") {
        checkedCount++;
        const diff = Math.abs(targetAngle - angles[key]);
        const angleScore = Math.max(0, 100 - diff * 1.6);
        totalScore += angleScore;
      }
    });

    const calculatedAccuracy = checkedCount > 0 ? Math.round(totalScore / checkedCount) : 85;
    setCurrentAccuracy(calculatedAccuracy);

    // Posture evaluation
    const isAccurate = calculatedAccuracy >= 75;
    setIsHoldingCorrectly(isAccurate);

    if (isAccurate) {
      setFeedbackMessage(
        "✨ Shandar! Body balance aur posture bilkul sahi hai. Hold banaye rakhein.",
      );
    } else {
      setFeedbackMessage(
        "⚠️ Posture adjust karein: Haath aur ghutne target angle ke anusaar seedhe rakhein.",
      );
    }

    // --- DRAW CENTER PLUMB LINE & BALANCE RETICLE ---
    ctx.save();
    // Vertical center plumb line
    ctx.setLineDash([4, 6]);
    ctx.strokeStyle = "rgba(52, 211, 153, 0.35)";
    ctx.lineWidth = 1.5;
    ctx.beginPath();
    ctx.moveTo(width * 0.5, 0);
    ctx.lineTo(width * 0.5, height);
    ctx.stroke();
    ctx.setLineDash([]);

    // Glowing Center of Mass target
    const comPixelX = comX * width;
    const comPixelY = comY * height;

    const balanceGradient = ctx.createRadialGradient(
      comPixelX,
      comPixelY,
      2,
      comPixelX,
      comPixelY,
      18,
    );
    balanceGradient.addColorStop(
      0,
      isAccurate ? "rgba(52, 211, 153, 0.9)" : "rgba(251, 191, 36, 0.9)",
    );
    balanceGradient.addColorStop(1, "rgba(52, 211, 153, 0)");

    ctx.fillStyle = balanceGradient;
    ctx.beginPath();
    ctx.arc(comPixelX, comPixelY, 18, 0, Math.PI * 2);
    ctx.fill();

    ctx.fillStyle = isAccurate ? "#34d399" : "#fbbf24";
    ctx.beginPath();
    ctx.arc(comPixelX, comPixelY, 5, 0, Math.PI * 2);
    ctx.fill();

    // Center of Mass Text
    ctx.fillStyle = "#ffffff";
    ctx.font = "bold 10px monospace";
    ctx.fillText(`BALANCE ${stabilityScore}%`, comPixelX + 10, comPixelY - 6);
    ctx.restore();

    // --- DRAW SKELETON CONNECTIONS ---
    const connections: [keyof SkeletonLandmarks, keyof SkeletonLandmarks][] = [
      ["nose", "leftShoulder"],
      ["nose", "rightShoulder"],
      ["leftShoulder", "rightShoulder"],
      ["leftShoulder", "leftElbow"],
      ["leftElbow", "leftWrist"],
      ["rightShoulder", "rightElbow"],
      ["rightElbow", "rightWrist"],
      ["leftShoulder", "leftHip"],
      ["rightShoulder", "rightHip"],
      ["leftHip", "rightHip"],
      ["leftHip", "leftKnee"],
      ["leftKnee", "leftAnkle"],
      ["rightHip", "rightKnee"],
      ["rightKnee", "rightAnkle"],
    ];

    ctx.lineWidth = 4;
    ctx.lineCap = "round";
    ctx.lineJoin = "round";

    connections.forEach(([from, to]) => {
      const p1 = landmarks[from];
      const p2 = landmarks[to];
      if (p1 && p2) {
        ctx.beginPath();
        ctx.moveTo(p1.x * width, p1.y * height);
        ctx.lineTo(p2.x * width, p2.y * height);
        ctx.strokeStyle = isAccurate ? "rgba(52, 211, 153, 0.85)" : "rgba(251, 191, 36, 0.75)";
        ctx.stroke();
      }
    });

    // --- DRAW SKELETON JOINTS & ANGLE LABELS ---
    Object.entries(landmarks).forEach(([key, pt]) => {
      const px = pt.x * width;
      const py = pt.y * height;

      // Outer glow
      ctx.fillStyle = isAccurate ? "rgba(52, 211, 153, 0.4)" : "rgba(251, 191, 36, 0.4)";
      ctx.beginPath();
      ctx.arc(px, py, 9, 0, Math.PI * 2);
      ctx.fill();

      // Core point
      ctx.fillStyle = "#ffffff";
      ctx.beginPath();
      ctx.arc(px, py, 4.5, 0, Math.PI * 2);
      ctx.fill();

      // Display angle label on key joints
      if (angles[key] !== undefined) {
        ctx.fillStyle = "#10b981";
        ctx.font = "bold 11px sans-serif";
        ctx.fillText(`${angles[key]}°`, px + 8, py - 4);
      }
    });

    animationFrameRef.current = requestAnimationFrame(renderFrame);
  }, [isCameraActive, simulatedMode, getSimulatedLandmarks, selectedPose]);

  useEffect(() => {
    if (isCameraActive) {
      animationFrameRef.current = requestAnimationFrame(renderFrame);
    }
    return () => {
      if (animationFrameRef.current) cancelAnimationFrame(animationFrameRef.current);
    };
  }, [isCameraActive, renderFrame]);

  // Adjust canvas size
  useEffect(() => {
    const handleResize = () => {
      if (canvasRef.current) {
        if (isFullScreenCamera) {
          canvasRef.current.width = window.innerWidth;
          canvasRef.current.height = window.innerHeight;
        } else {
          const parentWidth = canvasRef.current.parentElement?.clientWidth || 500;
          canvasRef.current.width = Math.min(600, parentWidth);
          canvasRef.current.height = canvasRef.current.width; // 1:1 Square aspect ratio
        }
      }
    };
    handleResize();
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, [isFullScreenCamera, isCameraActive]);

  return (
    <div className="space-y-4 max-w-4xl mx-auto">
      {/* Hidden Video element for webcam stream */}
      <video
        ref={videoRef}
        playsInline
        muted
        className="hidden"
        onLoadedMetadata={() => {
          if (canvasRef.current) {
            if (isFullScreenCamera) {
              canvasRef.current.width = window.innerWidth;
              canvasRef.current.height = window.innerHeight;
            } else {
              const parentWidth = canvasRef.current.parentElement?.clientWidth || 500;
              canvasRef.current.width = Math.min(600, parentWidth);
              canvasRef.current.height = canvasRef.current.width; // 1:1 Square
            }
          }
        }}
      />

      {/* Main Viewfinder Card (LARGE SQUARE CAMERA DISPLAY) */}
      <div
        className={
          isFullScreenCamera
            ? "fixed inset-0 z-50 bg-black flex flex-col items-center justify-center animate-in fade-in duration-300 select-none"
            : "relative overflow-hidden rounded-3xl border border-border bg-card shadow-2xl w-full max-w-xl aspect-square mx-auto"
        }
      >
        {/* Canvas for Live Video + Skeleton + Real Balance */}
        <canvas
          ref={canvasRef}
          width={500}
          height={500}
          className={
            isFullScreenCamera
              ? "w-full h-full object-cover"
              : "w-full h-full aspect-square block bg-zinc-950 object-cover"
          }
        />

        {/* --- NON-OBSTRUCTIVE TOP HUD OVERLAY --- */}
        <div className="absolute top-3 inset-x-3 sm:top-4 sm:inset-x-4 flex items-center justify-between gap-2 z-20 pointer-events-none">
          <div className="flex items-center gap-2 pointer-events-auto">
            <div className="rounded-2xl border border-border bg-card/90 backdrop-blur-md px-3.5 py-1.5 shadow-lg">
              <p className="font-display text-xs sm:text-sm font-bold text-foreground flex items-center gap-1.5">
                {selectedPose.name}
                <span className="text-[10px] font-normal text-emerald-600 dark:text-emerald-400 font-sans">
                  ({selectedPose.sanskritName})
                </span>
              </p>
            </div>

            <span className="hidden sm:inline-flex items-center gap-1 px-2.5 py-1 rounded-full bg-emerald-500/20 text-emerald-700 dark:text-emerald-300 border border-emerald-500/30 text-[11px] font-semibold">
              <Activity className="h-3 w-3" /> Live Camera
            </span>
          </div>

          {/* Active Hold Timer HUD */}
          {isCameraActive && (
            <div className="flex items-center gap-1.5 rounded-2xl border border-border bg-card/90 backdrop-blur-md px-3 py-1.5 shadow-lg pointer-events-auto">
              <Timer className="h-3.5 w-3.5 text-emerald-500 animate-pulse" />
              <span className="font-mono text-xs font-bold text-foreground">{holdTimer}s</span>
              <span className="text-[10px] text-muted-foreground">
                / {selectedPose.targetHoldSeconds}s
              </span>
            </div>
          )}
        </div>

        {/* Start Overlay inside canvas frame when inactive */}
        {!isCameraActive && (
          <div className="absolute inset-0 z-20 flex flex-col items-center justify-center p-6 text-center bg-background/85 backdrop-blur-sm text-foreground">
            <div className="grid h-16 w-16 place-items-center rounded-3xl bg-emerald-500/20 text-emerald-600 dark:text-emerald-400 border border-emerald-500/30 mb-3 shadow-lg">
              <Camera className="h-8 w-8" />
            </div>

            <h3 className="font-display text-lg font-bold text-foreground mb-1">
              AI Yoga & Balance Camera
            </h3>
            <p className="text-xs text-muted-foreground max-w-xs mb-4 leading-relaxed">
              Bada square camera feed real-time skeleton tracking aur posture alignment ke saath.
            </p>

            <button
              type="button"
              onClick={() => startCamera(false)}
              className="press flex items-center gap-2 rounded-2xl bg-gradient-to-r from-emerald-500 to-teal-600 px-6 py-3 text-sm font-bold text-white shadow-lg hover:brightness-110"
            >
              <Play className="h-4 w-4" /> Start Camera Feed
            </button>
          </div>
        )}
      </div>

      {/* --- ALL CONTROL BUTTONS PLACED CLEANLY BELOW CAMERA CANVAS --- */}
      <Card className="p-4 bg-card text-card-foreground border border-border shadow-xl space-y-3">
        <div className="flex flex-wrap items-center justify-between gap-2 border-b border-border pb-3">
          <div className="flex items-center gap-2">
            <span className="h-2.5 w-2.5 rounded-full bg-emerald-500 animate-pulse" />
            <p className="font-display text-sm font-bold text-foreground">
              {isCameraActive ? feedbackMessage : "Camera Controls & Audio Feedback"}
            </p>
          </div>

          <div className="flex items-center gap-2">
            {/* Voice Coach Toggle Button */}
            <button
              type="button"
              onClick={() => {
                const next = !voiceCoachEnabled;
                setVoiceCoachEnabled(next);
                if (next) speakCue("Voice coaching enabled");
              }}
              className={`press flex items-center gap-1.5 px-3 py-1.5 rounded-xl border text-xs font-semibold transition-all ${
                voiceCoachEnabled
                  ? "bg-emerald-500/10 border-emerald-500/30 text-emerald-600 dark:text-emerald-400"
                  : "bg-red-500/10 border-red-500/30 text-red-500"
              }`}
            >
              {voiceCoachEnabled ? (
                <Volume2 className="h-4 w-4" />
              ) : (
                <VolumeX className="h-4 w-4" />
              )}
              <span>{voiceCoachEnabled ? "Voice ON" : "Voice OFF"}</span>
            </button>

            {/* Fullscreen Button */}
            <button
              type="button"
              onClick={() => {
                if (!isCameraActive) {
                  startCamera(true);
                } else {
                  setIsFullScreenCamera(!isFullScreenCamera);
                }
              }}
              className="press flex items-center gap-1.5 px-3 py-1.5 rounded-xl border border-border bg-muted text-foreground text-xs font-bold hover:border-emerald-500"
            >
              <Maximize2 className="h-3.5 w-3.5 text-emerald-500" /> Fullscreen Studio
            </button>
          </div>
        </div>

        {/* Primary Action Button Row */}
        <div className="flex items-center justify-between gap-3 pt-1">
          {!isCameraActive ? (
            <button
              type="button"
              onClick={() => startCamera(false)}
              className="press flex-1 flex items-center justify-center gap-2 rounded-2xl bg-gradient-to-r from-emerald-500 via-teal-500 to-emerald-600 py-3 px-4 text-sm font-bold text-white shadow-md hover:brightness-110"
            >
              <Camera className="h-4 w-4" /> Camera Feed Live Karein
            </button>
          ) : (
            <button
              type="button"
              onClick={stopCamera}
              className="press flex-1 flex items-center justify-center gap-2 rounded-2xl bg-red-500/15 border border-red-500/40 py-3 px-4 text-sm font-bold text-red-500 hover:bg-red-500/25"
            >
              <VideoOff className="h-4 w-4" /> Stop Camera
            </button>
          )}

          <button
            type="button"
            onClick={() => {
              setHoldTimer(0);
              setSessionScore(null);
              speakCue(`${selectedPose.name} restart kiya gaya.`);
            }}
            className="press flex items-center justify-center gap-1.5 rounded-2xl border border-border bg-muted px-4 py-3 text-xs font-bold text-foreground hover:bg-card"
          >
            <RotateCcw className="h-4 w-4 text-muted-foreground" /> Reset Timer
          </button>
        </div>

        {/* Alignment & Weight Distribution Gauge (When Active) */}
        {isCameraActive && (
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-2 border-t border-border">
            <div className="p-3 rounded-2xl border border-border bg-muted/40 space-y-1">
              <div className="flex items-center justify-between text-xs font-bold text-foreground">
                <span className="flex items-center gap-1 text-emerald-600 dark:text-emerald-400">
                  <Scale className="h-3.5 w-3.5" /> Body Weight Balance
                </span>
                <span className="font-mono text-emerald-600 dark:text-emerald-400">
                  {balanceMetrics.balanceStabilityScore}%
                </span>
              </div>
              <div className="flex justify-between text-[10px] text-muted-foreground">
                <span>Left Foot: {balanceMetrics.leftWeightPercent}%</span>
                <span>Right Foot: {balanceMetrics.rightWeightPercent}%</span>
              </div>
              <div className="h-2 w-full overflow-hidden rounded-full bg-border flex">
                <div
                  className="h-full bg-teal-500 transition-all duration-150"
                  style={{ width: `${balanceMetrics.leftWeightPercent}%` }}
                />
                <div
                  className="h-full bg-emerald-500 transition-all duration-150"
                  style={{ width: `${balanceMetrics.rightWeightPercent}%` }}
                />
              </div>
            </div>

            <div className="p-3 rounded-2xl border border-border bg-muted/40 flex flex-col justify-between">
              <div className="flex justify-between items-center text-xs text-foreground font-semibold">
                <span>Pose Accuracy:</span>
                <span className="font-bold font-mono text-emerald-600 dark:text-emerald-400">
                  {currentAccuracy}%
                </span>
              </div>
              <div className="flex justify-between items-center text-xs text-foreground font-semibold mt-1">
                <span>Spine Tilt Angle:</span>
                <span className="font-bold font-mono text-foreground">
                  {balanceMetrics.tiltAngleDegrees}°
                </span>
              </div>
            </div>
          </div>
        )}
      </Card>

      {/* Pose Selection Carousel */}
      <div className="space-y-2">
        <div className="flex items-center justify-between">
          <span className="text-xs font-bold uppercase tracking-wider text-muted-foreground">
            Select Yoga Pose
          </span>
          <span className="text-xs text-emerald-600 dark:text-emerald-400 font-semibold">
            {YOGA_POSES.length} Guided Poses
          </span>
        </div>

        <div className="grid grid-cols-2 sm:grid-cols-3 gap-2.5">
          {YOGA_POSES.map((pose) => {
            const isSelected = selectedPose.id === pose.id;
            return (
              <button
                key={pose.id}
                type="button"
                onClick={() => {
                  onPoseChange(pose);
                  setHoldTimer(0);
                  setSessionScore(null);
                  speakCue(`${pose.name} chuna gaya. ${pose.hindiInstruction}`);
                }}
                className={`press flex flex-col p-3 rounded-2xl border text-left transition-all ${
                  isSelected
                    ? "border-emerald-500 bg-emerald-500/15 text-foreground shadow-md ring-2 ring-emerald-500/40"
                    : "border-border bg-card text-muted-foreground hover:border-emerald-500/40 hover:text-foreground"
                }`}
              >
                <div className="flex items-center justify-between w-full mb-1">
                  <span className="text-[10px] font-bold uppercase tracking-wider text-emerald-600 dark:text-emerald-400">
                    {pose.category}
                  </span>
                  <span className="text-[10px] text-muted-foreground">
                    {pose.targetHoldSeconds}s
                  </span>
                </div>
                <p className="font-display text-sm font-bold text-foreground line-clamp-1">
                  {pose.name}
                </p>
                <p className="text-[10px] text-muted-foreground italic line-clamp-1">
                  {pose.sanskritName}
                </p>
              </button>
            );
          })}
        </div>
      </div>

      {/* Target Joint Angles Card */}
      <Card className="p-4 bg-card border border-border space-y-2.5 text-card-foreground shadow-sm">
        <div className="flex items-center justify-between">
          <span className="font-display text-xs font-bold text-foreground flex items-center gap-1.5">
            <Compass className="h-4 w-4 text-emerald-500" /> Target Alignment Angles
          </span>
          <span className="text-[10px] text-muted-foreground">Live AI Verification</span>
        </div>

        <div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
          {Object.entries(selectedPose.targetAngles).map(([joint, targetDeg]) => {
            const currentDeg = detectedAngles[joint] || targetDeg;
            const diff = Math.abs(currentDeg - targetDeg);
            const isNear = diff <= 15;

            return (
              <div
                key={joint}
                className={`p-2 rounded-xl border text-xs flex justify-between items-center ${
                  isNear
                    ? "border-emerald-500/40 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"
                    : "border-amber-500/30 bg-amber-500/10 text-amber-700 dark:text-amber-300"
                }`}
              >
                <span className="capitalize text-[11px]">
                  {joint.replace(/([A-Z])/g, " $1").trim()}
                </span>
                <span className="font-bold font-mono">
                  {currentDeg}° / {targetDeg}°
                </span>
              </div>
            );
          })}
        </div>
      </Card>
    </div>
  );
}
