import Rule from "../../app/components/cards/Rule";
import Image from "next/image";
import { getAssetPath } from "../../lib/assetUtils";
import { getGovernanceTemplatesForHome } from "../../lib/templates/governanceTemplateCatalog";

export default {
  title: "Components/Cards/Rule",
  component: Rule,
  parameters: {
    layout: "centered",
    docs: {
      description: {
        component:
          "An interactive card component that displays governance templates and decision-making patterns. Features collapsed/expanded states, size variants (L/M), category sections with pills and + buttons, hover states, keyboard navigation, analytics tracking, and accessibility support. Use Tab key to test focus indicators and Enter/Space to activate.",
      },
    },
  },
  argTypes: {
    title: {
      control: { type: "text" },
      description: "The title of the governance template",
    },
    description: {
      control: { type: "text" },
      description: "The description of the governance pattern",
    },
    backgroundColor: {
      control: { type: "select" },
      options: [
        "bg-[var(--color-surface-default-brand-lime)]",
        "bg-[var(--color-surface-default-brand-rust)]",
        "bg-[var(--color-surface-default-brand-red)]",
        "bg-[var(--color-surface-default-brand-teal)]",
        "bg-[var(--color-community-teal-100)]",
      ],
      description: "The background color variant for the card",
    },
    expanded: {
      control: { type: "boolean" },
      description: "Whether the card is in expanded state",
    },
    recommended: {
      control: { type: "boolean" },
      description:
        "When true and collapsed, show RECOMMENDED tag above the title (templates grid)",
    },
    size: {
      control: { type: "select" },
      options: ["XS", "S", "M", "L", "xs", "s", "m", "l"],
      description: "Size variant of the card",
    },
    onClick: { action: "clicked" },
  },
  tags: ["autodocs"],
};

export const Default = {
  args: {
    title: "Consensus clusters",
    description:
      "Units called Circles have the ability to decide and act on matters in their domains, which their members agree on through a Council.",
    backgroundColor: "bg-[var(--color-surface-default-brand-lime)]",
    expanded: false,
    recommended: false,
    size: "L",
    icon: (
      <Image
        src="assets/template-mark/consensus-clusters.svg"
        alt="Sociocracy"
        width={40}
        height={40}
        className="md:w-[56px] md:h-[56px] lg:w-[90px] lg:h-[90px]"
      />
    ),
  },
};

/** Figma `22142:898446` — compact `Card / Rule` with template recommendation tag. */
export const CollapsedWithRecommended = {
  args: {
    ...Default.args,
    title: "Do-ocracy",
    description:
      "Authority is granted to those doing the work. If you do the task, you decide how it gets done.",
    backgroundColor: "bg-[var(--color-surface-invert-brand-royal)]",
    recommended: true,
  },
};

export const Expanded = {
  args: {
    title: "Mutual Aid Mondays",
    description:
      "Mutual Aid Monday is a grassroots community in Denver, founded in November 2020 by Kelsang Virya, dedicated to supporting neighbors experiencing homelessness.",
    backgroundColor: "bg-[#b7d9d5]",
    expanded: true,
    size: "L",
    logoUrl:
      "http://localhost:3845/assets/d2513a6ab56f2b2927e8a7c442c06326e7a29541.png",
    logoAlt: "Mutual Aid Mondays",
    categories: [
      {
        name: "Values",
        chipOptions: [
          { id: "values-1", label: "Consciousness", state: "unselected" },
          { id: "values-2", label: "Ecology", state: "unselected" },
          { id: "values-3", label: "Abundance", state: "unselected" },
          { id: "values-4", label: "Art", state: "unselected" },
          { id: "values-5", label: "Decisiveness", state: "unselected" },
        ],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
      {
        name: "Communication",
        chipOptions: [{ id: "comm-1", label: "Signal", state: "unselected" }],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
      {
        name: "Membership",
        chipOptions: [
          { id: "membership-1", label: "Open Admission", state: "unselected" },
        ],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
      {
        name: "Decision-making",
        chipOptions: [
          { id: "decision-1", label: "Lazy Consensus", state: "unselected" },
          {
            id: "decision-2",
            label: "Modified Consensus",
            state: "unselected",
          },
        ],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
      {
        name: "Conflict management",
        chipOptions: [
          { id: "conflict-1", label: "Code of Conduct", state: "unselected" },
          {
            id: "conflict-2",
            label: "Restorative Justice",
            state: "unselected",
          },
        ],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
    ],
  },
};

export const SizeLarge = {
  args: {
    title: "Consensus clusters",
    description:
      "Units called Circles have the ability to decide and act on matters in their domains, which their members agree on through a Council.",
    backgroundColor: "bg-[var(--color-surface-default-brand-lime)]",
    expanded: false,
    size: "L",
    icon: (
      <Image
        src="assets/template-mark/consensus-clusters.svg"
        alt="Sociocracy"
        width={103}
        height={103}
      />
    ),
  },
};

export const SizeMedium = {
  args: {
    title: "Consensus clusters",
    description:
      "Units called Circles have the ability to decide and act on matters in their domains, which their members agree on through a Council.",
    backgroundColor: "bg-[var(--color-surface-default-brand-lime)]",
    expanded: false,
    size: "M",
    icon: (
      <Image
        src="assets/template-mark/consensus-clusters.svg"
        alt="Sociocracy"
        width={56}
        height={56}
      />
    ),
  },
};

export const SizeSmall = {
  args: {
    title: "Consensus clusters",
    description:
      "Units called Circles have the ability to decide and act on matters in their domains, which their members agree on through a Council.",
    backgroundColor: "bg-[var(--color-surface-default-brand-lime)]",
    expanded: false,
    size: "S",
    icon: (
      <Image
        src="assets/template-mark/consensus-clusters.svg"
        alt="Sociocracy"
        width={56}
        height={56}
      />
    ),
  },
};

export const SizeExtraSmall = {
  args: {
    title: "Consensus clusters",
    description:
      "Units called Circles have the ability to decide and act on matters in their domains, which their members agree on through a Council.",
    backgroundColor: "bg-[var(--color-surface-default-brand-lime)]",
    expanded: false,
    size: "XS",
    icon: (
      <Image
        src="assets/template-mark/consensus-clusters.svg"
        alt="Sociocracy"
        width={8}
        height={8}
      />
    ),
  },
};

export const ExpandedMedium = {
  args: {
    title: "Mutual Aid Mondays",
    description:
      "Mutual Aid Monday is a grassroots community in Denver, founded in November 2020 by Kelsang Virya, dedicated to supporting neighbors experiencing homelessness.",
    backgroundColor: "bg-[#b7d9d5]",
    expanded: true,
    size: "M",
    logoUrl:
      "http://localhost:3845/assets/d2513a6ab56f2b2927e8a7c442c06326e7a29541.png",
    logoAlt: "Mutual Aid Mondays",
    categories: [
      {
        name: "Values",
        chipOptions: [
          { id: "values-1", label: "Consciousness", state: "unselected" },
          { id: "values-2", label: "Ecology", state: "unselected" },
          { id: "values-3", label: "Abundance", state: "unselected" },
          { id: "values-4", label: "Art", state: "unselected" },
          { id: "values-5", label: "Decisiveness", state: "unselected" },
        ],
      },
      {
        name: "Communication",
        chipOptions: [{ id: "comm-1", label: "Signal", state: "unselected" }],
      },
      {
        name: "Membership",
        chipOptions: [
          { id: "membership-1", label: "Open Admission", state: "unselected" },
        ],
      },
      {
        name: "Decision-making",
        chipOptions: [
          { id: "decision-1", label: "Lazy Consensus", state: "unselected" },
          {
            id: "decision-2",
            label: "Modified Consensus",
            state: "unselected",
          },
        ],
      },
      {
        name: "Conflict management",
        chipOptions: [
          { id: "conflict-1", label: "Code of Conduct", state: "unselected" },
          {
            id: "conflict-2",
            label: "Restorative Justice",
            state: "unselected",
          },
        ],
      },
    ],
  },
};

export const WithLogoFallback = {
  args: {
    title: "Community Example",
    description:
      "This card shows the logo fallback with community initials when no logo is provided.",
    backgroundColor: "bg-[var(--color-surface-default-brand-teal)]",
    expanded: false,
    size: "L",
    communityInitials: "CE",
  },
};

export const AllVariants = {
  // eslint-disable-next-line no-unused-vars
  render: (_args) => (
    <div className="grid grid-cols-1 md:grid-cols-2 gap-6 max-w-4xl">
      {getGovernanceTemplatesForHome().map((entry) => (
        <Rule
          key={entry.slug}
          title={entry.title}
          description={entry.description}
          backgroundColor={entry.backgroundColor}
          templateGridFigmaShell
          icon={
            <Image
              src={getAssetPath(entry.iconPath)}
              alt={entry.title}
              width={40}
              height={40}
              className="md:w-[56px] md:h-[56px] lg:w-[90px] lg:h-[90px]"
            />
          }
          onClick={() => console.log(`${entry.slug} template selected`)}
        />
      ))}
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story:
          "All four governance template variants with their respective colors and icons. Test hover states, focus indicators, and click interactions.",
      },
    },
  },
};

export const InteractiveStates = {
  args: {
    title: "Interactive Demo",
    description:
      "Hover over this card to see the scale and shadow effects. Use Tab to focus and Enter/Space to activate. Click pills and + buttons to see event handlers.",
    backgroundColor: "bg-[var(--color-community-teal-100)]",
    expanded: true,
    size: "L",
    icon: (
      <div className="w-[103px] h-[103px] bg-white rounded-full flex items-center justify-center">
        <span className="text-[36px] font-bold text-gray-800">?</span>
      </div>
    ),
    categories: [
      {
        name: "Values",
        chipOptions: [
          { id: "values-1", label: "Consciousness", state: "unselected" },
          { id: "values-2", label: "Ecology", state: "unselected" },
          { id: "values-3", label: "Abundance", state: "unselected" },
        ],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
      {
        name: "Communication",
        chipOptions: [{ id: "comm-1", label: "Signal", state: "unselected" }],
        onChipClick: (categoryName, chipId) => {
          console.log(`Chip clicked: ${categoryName} - ${chipId}`);
        },
        onAddClick: (categoryName) => {
          console.log(`Add clicked: ${categoryName}`);
        },
      },
    ],
  },
  parameters: {
    docs: {
      description: {
        story:
          "Demonstrates interactive states including hover effects, focus indicators, keyboard navigation, and pill/+ button interactions. Test with mouse hover, keyboard Tab/Enter/Space, and click pills/+ buttons.",
      },
    },
  },
};

export const AccessibilityTest = {
  args: {
    title: "Accessibility Demo",
    description:
      "This card is designed for accessibility testing. Use Tab to focus, Enter/Space to activate, and screen readers to test ARIA labels.",
    backgroundColor: "bg-[var(--color-surface-default-brand-teal)]",
    icon: (
      <div className="w-10 h-10 md:w-14 md:h-14 lg:w-[90px] lg:h-[90px] bg-white rounded-full flex items-center justify-center">
        <span className="text-lg font-bold text-gray-800">♿</span>
      </div>
    ),
  },
  parameters: {
    docs: {
      description: {
        story:
          "Specifically designed for testing accessibility features including keyboard navigation, screen reader support, and focus management.",
      },
    },
  },
};
