import { Building2 } from "lucide-react";
import { Skeleton } from "@/shared/common/ui/skeleton";
import { useLocalizedName } from "@/shared/common/localizedName";

interface Organization {
  id: string;
  name: {
    en: string;
    am: string;
  };
}

interface OrganizationListProps {
  selectedOrgId: string;
  organizations: Organization[];
  isLoading: boolean;
  searchQuery: string;
  onSelectOrganization: (id: string) => void;
}

export const OrganizationList = ({
  selectedOrgId,
  organizations,
  isLoading,
  searchQuery,
  onSelectOrganization,
}: OrganizationListProps) => {
  const localizedName = useLocalizedName();

  const filteredOrganizations = organizations.filter((org) =>
    localizedName(org.name).toLowerCase().includes(searchQuery.toLowerCase())
  );

  if (isLoading) {
    return (
      <div className="space-y-2">
        {[1, 2, 3].map((i) => (
          <Skeleton key={i} className="h-12 w-full" />
        ))}
      </div>
    );
  }

  if (filteredOrganizations.length === 0) {
    return (
      <div className="text-center py-8 text-gray-500">
        No organizations found
      </div>
    );
  }

  return (
    <div className="space-y-2">
      {filteredOrganizations.map((org) => (
        <button
          key={org.id}
          onClick={() => onSelectOrganization(org.id)}
          className={`w-full text-left p-3 rounded-lg border transition-colors ${
            selectedOrgId === org.id
              ? "bg-purple-50 border-purple-500 text-purple-700"
              : "bg-white border-gray-200 hover:bg-gray-50"
          }`}
        >
          <div className="flex items-center gap-2">
            <Building2 className="h-4 w-4" />
            <span className="font-medium">{localizedName(org.name)}</span>
          </div>
        </button>
      ))}
    </div>
  );
};
