"use client";

import { useRouter } from "next/navigation";
import { useState } from "react";

export function SearchBar() {
  const router = useRouter();
  const [query, setQuery] = useState("");

  function handleSearch(e: React.FormEvent) {
    e.preventDefault();
    if (query.trim()) {
      router.push(`/trips?q=${encodeURIComponent(query.trim())}`);
    } else {
      router.push("/trips");
    }
  }

  return (
    <form onSubmit={handleSearch} className="mx-auto max-w-xl">
      <div className="flex overflow-hidden rounded-2xl bg-white/10 ring-1 ring-white/20 backdrop-blur-sm transition-all focus-within:bg-white/15 focus-within:ring-primary-400/50">
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          placeholder="Cari gunung, lokasi, atau trip..."
          className="flex-1 border-none bg-transparent px-5 py-3.5 text-sm text-white outline-none placeholder:text-neutral-400"
        />
        <button
          type="submit"
          className="bg-primary-600 px-6 text-sm font-semibold text-white transition-colors hover:bg-primary-500"
        >
          Cari
        </button>
      </div>
    </form>
  );
}
