'use client';

import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Castle, ArrowUp, Eye } from 'lucide-react';
import { useCombatStore } from '@/lib/game/stores';

export function SpireDebugSection() {
  const [floorInput, setFloorInput] = useState('50');

  const currentFloor = useCombatStore((s) => s.currentFloor);
  const maxFloorReached = useCombatStore((s) => s.maxFloorReached);
  const spireMode = useCombatStore((s) => s.spireMode);
  const debugSetFloor = useCombatStore((s) => s.debugSetFloor);
  const resetFloorHP = useCombatStore((s) => s.resetFloorHP);
  const enterSpireMode = useCombatStore((s) => s.enterSpireMode);
  const exitSpireMode = useCombatStore((s) => s.exitSpireMode);
  const setMaxFloorReached = useCombatStore((s) => s.setMaxFloorReached);

  const handleJumpToFloor = () => {
    const floor = parseInt(floorInput, 10);
    if (isNaN(floor) || floor < 1 || floor > 100) return;
    debugSetFloor(floor);
    setMaxFloorReached(floor);
  };

  const handleClearFloor = () => {
    resetFloorHP();
  };

  const handleToggleSpireMode = () => {
    if (spireMode) {
      exitSpireMode();
    } else {
      enterSpireMode();
    }
  };

  return (
    <Card className="bg-gray-900/80 border-gray-700">
      <CardHeader className="pb-2">
        <CardTitle className="text-teal-400 text-sm flex items-center gap-2">
          <Castle className="w-4 h-4" />
          Spire Debug
        </CardTitle>
      </CardHeader>
      <CardContent className="space-y-3">
        <div className="text-xs text-gray-400">
          Current Floor: {currentFloor} | Max Reached: {maxFloorReached} | Spire Mode: {spireMode ? 'ON' : 'OFF'}
        </div>

        <div className="flex gap-2 items-end">
          <div className="flex-1">
            <label className="text-xs text-gray-400 mb-1 block">Floor (1-100)</label>
            <Input
              type="number"
              min={1}
              max={100}
              value={floorInput}
              onChange={(e) => setFloorInput(e.target.value)}
              className="h-8"
            />
          </div>
          <Button size="sm" variant="outline" onClick={handleJumpToFloor}>
            <ArrowUp className="w-3 h-3 mr-1" /> Jump
          </Button>
        </div>

        <div className="flex gap-2 flex-wrap">
          <Button size="sm" variant="outline" onClick={handleClearFloor}>
            Reset Floor HP
          </Button>
          <Button
            size="sm"
            variant={spireMode ? 'default' : 'outline'}
            onClick={handleToggleSpireMode}
          >
            <Eye className="w-3 h-3 mr-1" />
            {spireMode ? 'Exit Spire Mode' : 'Enter Spire Mode'}
          </Button>
        </div>

        <div className="flex gap-2 flex-wrap">
          {[10, 25, 50, 75, 100].map((f) => (
            <Button
              key={f}
              size="sm"
              variant="outline"
              onClick={() => {
                setFloorInput(String(f));
                debugSetFloor(f);
                setMaxFloorReached(f);
              }}
            >
              Floor {f}
            </Button>
          ))}
        </div>
      </CardContent>
    </Card>
  );
}

SpireDebugSection.displayName = "SpireDebugSection";
