"use client";

import { useState } from "react";
import { Input } from "./ui/input";
import { Button } from "./ui/button";
import { Search } from "lucide-react";

interface VinInputProps {
  onDecode: (vin: string, mode: "browser" | "api") => void;
  mode: "browser" | "api";
  onModeChange: (mode: "browser" | "api") => void;
  isLoading?: boolean;
}

export function VinInput({
  onDecode,
  mode,
  onModeChange,
  isLoading,
}: VinInputProps) {
  const [vin, setVin] = useState("");

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    if (vin.trim().length === 17) {
      onDecode(vin.trim().toUpperCase(), mode);
    }
  };

  return (
    <form onSubmit={handleSubmit} className="space-y-4">
      <div className="flex flex-col sm:flex-row gap-3">
        <div className="flex-1">
          <Input
            type="text"
            placeholder="Enter 17-character VIN"
            value={vin}
            onChange={(e) => setVin(e.target.value.toUpperCase())}
            maxLength={17}
            className="font-mono text-lg h-12"
            disabled={isLoading}
          />
        </div>
        <Button
          type="submit"
          size="lg"
          disabled={vin.length !== 17 || isLoading}
          className="h-12 px-8"
        >
          {isLoading ? (
            "Decoding..."
          ) : (
            <>
              <Search className="mr-2 h-4 w-4" />
              Decode
            </>
          )}
        </Button>
      </div>

      {/* Mode Toggle */}
      <div className="flex items-center justify-center gap-2">
        <span className="text-sm text-muted-foreground">Decode with:</span>
        <div className="flex rounded-lg border bg-muted p-1">
          <button
            type="button"
            onClick={() => onModeChange("browser")}
            className={`px-4 py-1.5 text-sm font-medium rounded-md transition-colors ${
              mode === "browser"
                ? "bg-background shadow-sm"
                : "text-muted-foreground hover:text-foreground"
            }`}
          >
            Browser
          </button>
          <button
            type="button"
            onClick={() => onModeChange("api")}
            className={`px-4 py-1.5 text-sm font-medium rounded-md transition-colors ${
              mode === "api"
                ? "bg-background shadow-sm"
                : "text-muted-foreground hover:text-foreground"
            }`}
          >
            API
          </button>
        </div>
      </div>

      {vin.length > 0 && vin.length !== 17 && (
        <p className="text-sm text-destructive text-center">
          VIN must be exactly 17 characters
        </p>
      )}
    </form>
  );
}
