import { ref } from "vue";

import {
  getArgs,
  getArgTypes,
  getSlotNames,
  getSlotsFragment,
  getDocsDescription,
} from "../../utils/storybook";

import UCheckboxGroup from "../../ui.form-checkbox-group/UCheckboxGroup.vue";
import UCheckbox from "../../ui.form-checkbox/UCheckbox.vue";
import UAlert from "../../ui.text-alert/UAlert.vue";
import UCol from "../../ui.container-col/UCol.vue";
import URow from "../../ui.container-row/URow.vue";
import UBadge from "../../ui.text-badge/UBadge.vue";
import UText from "../../ui.text-block/UText.vue";
import ULink from "../../ui.button-link/ULink.vue";
import UIcon from "../../ui.image-icon/UIcon.vue";

import type { Meta, StoryFn } from "@storybook/vue3-vite";
import type { Props } from "../types";

interface UCheckboxGroupArgs extends Props {
  slotTemplate?: string;
  enum: "size" | "color";
}

export default {
  id: "3110",
  title: "Form Inputs & Controls / Checkbox Group",
  component: UCheckboxGroup,
  args: {
    label: "Select your preferred communication methods:",
    options: [
      { label: "Email Notifications", value: "email" },
      { label: "SMS Alerts", value: "sms" },
      { label: "Push Notifications", value: "push" },
    ],
  },
  argTypes: {
    ...getArgTypes(UCheckboxGroup.__name),
  },
  parameters: {
    docs: {
      ...getDocsDescription(UCheckboxGroup.__name),
    },
  },
} as Meta;

const DefaultTemplate: StoryFn<UCheckboxGroupArgs> = (args: UCheckboxGroupArgs) => ({
  components: { UCheckboxGroup, UCheckbox, URow, UCol, UBadge, UText, ULink, UIcon },
  setup: () => ({ args, slots: getSlotNames(UCheckboxGroup.__name), modelValue: ref([]) }),
  template: `
    <UCheckboxGroup v-bind="args" v-model="modelValue">
      ${args.slotTemplate || getSlotsFragment("")}
    </UCheckboxGroup>
  `,
});

const EnumTemplate: StoryFn<UCheckboxGroupArgs> = (args: UCheckboxGroupArgs, { argTypes }) => ({
  components: { UCheckboxGroup, URow },
  setup: () => ({ args, argTypes, getArgs, modelValue: ref([]) }),
  template: `
    <URow>
      <UCheckboxGroup
        v-for="option in argTypes?.[args.enum]?.options"
        v-bind="getArgs(args, option)"
        :key="option"
        v-model="modelValue"
      />
    </URow>
  `,
});

export const Default = DefaultTemplate.bind({});
Default.args = { name: "Default" };

export const Description = DefaultTemplate.bind({});
Description.args = {
  name: "Description",
  description: "You may select multiple options that best fit your preferences.",
};

export const Error: StoryFn<UCheckboxGroupArgs> = (args: UCheckboxGroupArgs) => ({
  components: { UCheckboxGroup },
  setup: () => ({ args, modelValue: ref([]) }),
  template: `
    <UCheckboxGroup
      name="Error"
      v-bind="args"
      v-model="modelValue"
      :error="modelValue.length ? '' : 'At least one option must be selected.'"
    />
  `,
});

export const Disabled = DefaultTemplate.bind({});
Disabled.args = { name: "Disabled", disabled: true };

export const Options: StoryFn<UCheckboxGroupArgs> = (args: UCheckboxGroupArgs) => ({
  components: { UCheckboxGroup, UCol, UAlert },
  setup: () => ({ args, modelValue: ref([]) }),
  template: `
    <UCol>
      <UCheckboxGroup v-bind="args" v-model="modelValue" />

      <UAlert
        :description="modelValue"
        size="sm"
        variant="soft"
        color="success"
        bordered
      />
    </UCol>
  `,
});
Options.args = {
  name: "Options",
  options: [
    { label: "String", value: "Subscribed" },
    { label: "Number", value: 42 },
    { label: "Boolean", value: true },
    { label: "Object", value: { value: 101, status: "active" } },
    { label: "Array", value: ["Admin", "Editor"] },
  ],
};
Options.parameters = {
  docs: {
    description: {
      story:
        "Every option you pass via the `options` prop can be of different type (see object meta keys table below).",
    },
  },
};

export const Colors = EnumTemplate.bind({});
Colors.args = { enum: "color", name: "Colors", label: "{enumValue}" };

export const Sizes = EnumTemplate.bind({});
Sizes.args = { enum: "size", name: "Sizes", label: "{enumValue}" };

export const LabelSlot = DefaultTemplate.bind({});
LabelSlot.args = {
  name: "LabelSlot",
  slotTemplate: `
    <template #label="{ label }">
      {{ label }}
      <span class="text-red-500">*</span>
    </template>
  `,
};

export const CustomKeys: StoryFn<UCheckboxGroupArgs> = (args: UCheckboxGroupArgs) => ({
  components: { UCheckboxGroup, UCol, UAlert },
  setup: () => ({ args, modelValue: ref([]) }),
  template: `
    <UCol>
      <UCheckboxGroup v-bind="args" v-model="modelValue" />

      <UAlert
        :description="modelValue"
        size="sm"
        variant="soft"
        color="success"
        bordered
      />
    </UCol>
  `,
});
CustomKeys.args = {
  name: "CustomKeys",
  label: "Select your preferred features:",
  labelKey: "name",
  valueKey: "id",
  options: [
    { id: "dark-mode", name: "Dark Mode" },
    { id: "auto-save", name: "Auto-save" },
    { id: "notifications", name: "Notifications" },
    { id: "two-factor", name: "Two-factor Authentication" },
  ],
};
CustomKeys.parameters = {
  docs: {
    description: {
      story:
        "Use `labelKey` and `valueKey` props to specify custom keys for label and value in option objects. " +
        "This is useful when working with data from APIs that use different property names.",
    },
  },
};

export const Slots: StoryFn<UCheckboxGroupArgs> = (args) => ({
  components: { UCheckboxGroup, UCol, UText, URow, ULink, UIcon },
  setup: () => ({
    args,
    modelValueLabel: ref(""),
    modelValueDescription: ref(""),
    modelValueError: ref(""),
  }),
  template: `
    <UCol gap="3xl">
      <UCheckboxGroup
        v-bind="args"
        v-model="modelValueLabel"
        name="LabelSlot"
      >
        <template #label>
          <URow align="center" gap="2xs">
            <UText>Choose how we notify you</UText>
            <UIcon name="notifications" size="xs" color="neutral" />
          </URow>
        </template>
      </UCheckboxGroup>

      <UCheckboxGroup
        v-bind="args"
        v-model="modelValueDescription"
        name="SlotsDescription"
        label="Notification channels"
      >
        <template #description>
          <URow align="center" gap="2xs" class="text-neutral">
            <UIcon name="notifications" size="xs" color="primary" />
            <UText size="sm" variant="lifted">
              You can change this later in
              <ULink label="notification settings" underlined size="sm" />.
            </UText>
          </URow>
        </template>
      </UCheckboxGroup>

      <UCheckboxGroup
        v-bind="args"
        v-model="modelValueError"
        name="SlotsError"
        label="Select options"
        :error="true"
      >
        <template #error>
          <UText size="sm" color="error">
            <ul>
              <li>Invalid selection</li>
              <li>At least one channel is required</li>
              <li>Choose at least one option before submitting</li>
            </ul>
          </UText>
        </template>
      </UCheckboxGroup>
    </UCol>
  `,
});
