import type { ModuleManifest } from "../_core/module";
import { loadContainerForShare, type ContainerShareData } from "./server/share-queries";
import { searchContainers } from "./server/queries";

const gardenManifest: ModuleManifest = {
  id: "garden",
  name: "Garden",
  nav: { href: "/garden", label: "Garden", icon: "sprout" },
  entities: [
    {
      type: "garden.container",
      label: { singular: "Container", plural: "Containers" },
      share: { canShare: true, defaultCapabilities: ["read"] },
      search: { search: searchContainers },
      resolveUrl: (id) => `/garden/containers/${id}`,
      loadForShare: (id) => loadContainerForShare(id),
      renderSharedView: ({ data }) => {
        const d = data as ContainerShareData;
        return (
          <div className="flex flex-col gap-3">
            <h2 className="text-xl font-bold">{d.name}</h2>
            <p className="text-sm capitalize text-[var(--ink-mute)]">{d.type}</p>
            {d.locationNotes && <p className="text-sm">{d.locationNotes}</p>}
            {d.plants.length > 0 && (
              <div>
                <p className="text-sm font-medium mb-1">Plants</p>
                <ul className="text-sm space-y-1">
                  {d.plants.map((p) => (
                    <li key={p.id}>
                      {p.name}
                      {p.scientificName && (
                        <span className="text-[var(--ink-mute)] italic ml-1">
                          ({p.scientificName})
                        </span>
                      )}
                    </li>
                  ))}
                </ul>
              </div>
            )}
          </div>
        );
      },
      renderActivity: (entry) => {
        const name = entry.payload?.name as string | undefined;
        if (entry.action === "create") return `Created container${name ? ` "${name}"` : ""}`;
        if (entry.action === "delete") return "Deleted container";
        return `Updated container${name ? ` "${name}"` : ""}`;
      },
    },
  ],
  dashboardWidgets: [],
  quickAdds: [
    {
      id: "garden.add-container",
      label: "Add container",
      icon: "sprout",
      url: "/garden/containers/new",
    },
  ],
};

export default gardenManifest;
