import BmsTextArea from '@/components/form/BmsTextArea.vue';
import { within } from 'storybook/test';
import { ref } from 'vue';

import template from '@/documentation/template_field_dependency.mdx';

export default {
  title: 'Composants/form/TextArea',
  component: BmsTextArea,
  parameters: {
    docs: {
      page: template,
    },
  },
};

const Template = (args) => ({
  setup() {
    return { args };
  },
  components: {
    BmsTextArea,
  },
  template: `
  <BmsTextArea v-bind="args"/>
  <br/>
  <BmsTextArea v-bind="{...args, small:true}"/>
  `,
});

export const Default = Template.bind({});
Default.args = {
  label: 'My label',
  placeholder: 'Placeholder',
  captions: ['There is more information'],
};

export const WithValue = Template.bind({});
WithValue.args = {
  label: 'My label',
  modelValue: 'Une valeur',
};

export const Disabled = Template.bind({});
Disabled.args = {
  label: 'My label',
  modelValue:
    'At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.',
  disabled: true,
};

export const WithIcons = Template.bind({});
WithIcons.args = {
  label: 'My label',
  modelValue: 'Une valeur',
  iconStart: true,
  iconEnd: true,
};

export const WithErrors = Template.bind({});
WithErrors.args = {
  label: 'My label',
  modelValue: 'Une valeur',
  errors: ['Error 1', 'Error 2'],
};

export const WithFocusState = {
  args: {
    modelValue: '',
  },

  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement);
    await canvas.getByRole('input').click();
    await canvas.getByRole('input').focus();
  },
};

// Simple template for documentation (without small variant)
const SimpleTemplate = (args) => ({
  setup() {
    const modelValue = ref(args.modelValue || '');
    return { args, modelValue };
  },
  components: {
    BmsTextArea,
  },
  template: `
  <BmsTextArea v-bind="args" v-model="modelValue" />
  `,
});

// Playground with default values for documentation
export const PlaygroundTextArea = SimpleTemplate.bind({});
PlaygroundTextArea.parameters = { chromatic: { disable: true } };
PlaygroundTextArea.args = {
  label: 'Description',
  placeholder: 'Enter your description here...',
};

// Do: Simple textarea with label
export const DoWithLabel = SimpleTemplate.bind({});
DoWithLabel.parameters = { chromatic: { disable: true } };
DoWithLabel.args = {
  label: 'Comments',
  placeholder: 'Enter your comments',
};

// Do: Textarea with placeholder
export const DoWithPlaceholder = SimpleTemplate.bind({});
DoWithPlaceholder.parameters = { chromatic: { disable: true } };
DoWithPlaceholder.args = {
  label: 'Description',
  placeholder: 'Describe your project in detail...',
};

// Do: Textarea with caption
export const DoWithCaption = SimpleTemplate.bind({});
DoWithCaption.parameters = { chromatic: { disable: true } };
DoWithCaption.args = {
  label: 'Message',
  placeholder: 'Enter your message',
  captions: ['Maximum 500 characters'],
};

// Do: Disabled state
export const DoDisabled = SimpleTemplate.bind({});
DoDisabled.parameters = { chromatic: { disable: true } };
DoDisabled.args = {
  label: 'Notes',
  modelValue: 'This is a read-only note that cannot be edited.',
  disabled: true,
};

// Do: Loading state
export const DoLoading = SimpleTemplate.bind({});
DoLoading.parameters = { chromatic: { disable: true } };
DoLoading.args = {
  label: 'Description',
  placeholder: 'Loading...',
  loading: true,
};

// Do: Textarea with error feedback
export const DoWithErrorFeedback = SimpleTemplate.bind({});
DoWithErrorFeedback.parameters = { chromatic: { disable: true } };
DoWithErrorFeedback.args = {
  label: 'Description',
  placeholder: 'Enter description',
  modelValue: 'Too short',
  errors: ['Description must be at least 20 characters'],
};

// Do: Textarea with helpful caption
export const DoWithHelpfulCaption = SimpleTemplate.bind({});
DoWithHelpfulCaption.parameters = { chromatic: { disable: true } };
DoWithHelpfulCaption.args = {
  label: 'Project description',
  placeholder: 'Describe your project',
  modelValue: 'Short',
  captions: ['Please provide a detailed description of at least 50 characters'],
  errors: ['Description is too short'],
};

// Don't: Long labels
export const DontLongLabel = SimpleTemplate.bind({});
DontLongLabel.parameters = { chromatic: { disable: true } };
DontLongLabel.args = {
  label:
    'Please enter your complete and detailed project description including all relevant information about the project goals, timeline, and expected outcomes',
  placeholder: 'Description',
};

export const DoShortLabel = SimpleTemplate.bind({});
DoShortLabel.parameters = { chromatic: { disable: true } };
DoShortLabel.args = {
  label: 'Description',
  placeholder: 'Enter description',
};

// Don't: No label
export const DontNoLabel = SimpleTemplate.bind({});
DontNoLabel.parameters = { chromatic: { disable: true } };
DontNoLabel.args = {
  placeholder: 'Enter your message here',
};

// Don't: Generic placeholder
export const DontGenericPlaceholder = SimpleTemplate.bind({});
DontGenericPlaceholder.parameters = { chromatic: { disable: true } };
DontGenericPlaceholder.args = {
  label: 'Message',
  placeholder: 'Enter text here',
};

export const DoSpecificPlaceholder = SimpleTemplate.bind({});
DoSpecificPlaceholder.parameters = { chromatic: { disable: true } };
DoSpecificPlaceholder.args = {
  label: 'Message',
  placeholder: 'Describe the issue you encountered...',
};

// Don't: No error feedback
export const DontNoErrorFeedback = SimpleTemplate.bind({});
DontNoErrorFeedback.parameters = { chromatic: { disable: true } };
DontNoErrorFeedback.args = {
  label: 'Description',
  placeholder: 'Enter description',
  modelValue: 'Too short',
};

// Don't: No caption for complex requirements
export const DontNoCaption = SimpleTemplate.bind({});
DontNoCaption.parameters = { chromatic: { disable: true } };
DontNoCaption.args = {
  label: 'Project description',
  placeholder: 'Enter description',
  modelValue: 'Short',
  errors: ['Error'],
};
