"use client"

import { Card, CardContent } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Search, X, Filter } from "lucide-react"
import { useState } from "react"

interface PatientsSearchProps {
  searchTerm: string
  onSearchChange: (value: string) => void
  totalPatients: number
  filteredCount: number
  showFilters?: boolean
}

export default function PatientsSearch({
  searchTerm,
  onSearchChange,
  totalPatients,
  filteredCount,
  showFilters = false
}: PatientsSearchProps) {
  const [activeFilter, setActiveFilter] = useState<string>("all")

  const handleClearSearch = () => {
    onSearchChange("")
  }

  const isFiltering = searchTerm.length > 0

  return (
    <Card>
      <CardContent className="pt-6 space-y-4">
        {/* Barra de búsqueda principal */}
        <div className="relative">
          <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400" />
          <Input
            placeholder="Buscar por nombre, apellido, email o teléfono..."
            value={searchTerm}
            onChange={(e) => onSearchChange(e.target.value)}
            className="pl-10 pr-10 h-11"
          />
          {searchTerm && (
            <button
              onClick={handleClearSearch}
              className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600 transition-colors"
            >
              <X className="h-4 w-4" />
            </button>
          )}
        </div>

        {/* Filtros rápidos y estadísticas */}
        <div className="flex items-center justify-between flex-wrap gap-3">
          <div className="flex items-center gap-2 flex-wrap">
            {showFilters && (
              <>
                <span className="text-sm text-gray-600 font-medium">Filtros:</span>
                <Badge
                  variant={activeFilter === "all" ? "default" : "outline"}
                  className="cursor-pointer hover:bg-primary/90"
                  onClick={() => setActiveFilter("all")}
                >
                  Todos
                </Badge>
                <Badge
                  variant={activeFilter === "recent" ? "default" : "outline"}
                  className="cursor-pointer hover:bg-primary/90"
                  onClick={() => setActiveFilter("recent")}
                >
                  Recientes
                </Badge>
                <Badge
                  variant={activeFilter === "active" ? "default" : "outline"}
                  className="cursor-pointer hover:bg-primary/90"
                  onClick={() => setActiveFilter("active")}
                >
                  Activos
                </Badge>
              </>
            )}
          </div>

          {/* Contador de resultados */}
          <div className="flex items-center gap-2">
            {isFiltering ? (
              <div className="flex items-center gap-2">
                <span className="text-sm text-gray-600">
                  Mostrando <span className="font-semibold text-primary">{filteredCount}</span> de{" "}
                  <span className="font-semibold">{totalPatients}</span> pacientes
                </span>
                {filteredCount === 0 && (
                  <Badge variant="secondary" className="text-xs">
                    Sin resultados
                  </Badge>
                )}
              </div>
            ) : (
              <span className="text-sm text-gray-600">
                <span className="font-semibold text-primary">{totalPatients}</span> pacientes en total
              </span>
            )}
          </div>
        </div>

        {/* Sugerencias de búsqueda */}
        {!isFiltering && totalPatients > 0 && (
          <div className="pt-2 border-t">
            <div className="flex items-center gap-2 text-xs text-gray-500">
              <Filter className="h-3 w-3" />
              <span>Tip: Puedes buscar por nombre, apellido, email o información médica</span>
            </div>
          </div>
        )}

        {/* Mensaje cuando no hay resultados */}
        {isFiltering && filteredCount === 0 && (
          <div className="pt-2 border-t">
            <div className="text-sm text-amber-600 bg-amber-50 rounded-lg p-3 flex items-center gap-2">
              <Search className="h-4 w-4" />
              <span>No se encontraron pacientes con &quot;{searchTerm}&quot;</span>
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  )
}
