"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-xl bg-white/10 ring-1 ring-white/20 backdrop-blur-sm transition-all focus-within:bg-white/15 focus-within:ring-primary-400/50 sm:rounded-2xl">
        <input
          type="text"
          value={query}
          onChange={(e) => setQuery(e.target.value)}
          placeholder="Cari destinasi, lokasi, atau trip..."
          className="min-w-0 flex-1 border-none bg-transparent px-3.5 py-3 text-sm text-white outline-none placeholder:text-neutral-400 sm:px-5 sm:py-3.5"
        />
        <button
          type="submit"
          className="shrink-0 bg-primary-600 px-4 text-sm font-semibold text-white transition-colors hover:bg-primary-500 sm:px-6"
        >
          Cari
        </button>
      </div>
    </form>
  );
}
