import type { Meta, StoryObj } from '@storybook/vue3-vite';
import { defineComponent, h } from 'vue';

const ThemePlaceholder = defineComponent({
  name: 'ThemePlaceholder',
  render() {
    return h('div');
  },
});

const meta: Meta<typeof ThemePlaceholder> = {
  title: 'CSS utility/Grid',
  component: ThemePlaceholder,
  tags: ['autodocs'],
  parameters: {
    layout: 'fullscreen',
  },
};

export default meta;
type Story = StoryObj<typeof ThemePlaceholder>;

const copyScript = `
  function copyToClipboard(text, el) {
    navigator.clipboard.writeText(text).then(() => {
      el.classList.add('copied');
      setTimeout(() => el.classList.remove('copied'), 1500);
    });
  }
`;

/**
 * Grid column utilities for defining the number of columns in a grid layout.
 * Use for responsive multi-column layouts like card grids and dashboards.
 */
export const Columns: Story = {
  render: () => ({
    template: `
      <div class="p-8 bg-white dark:bg-background">
        <style>
          .copy-btn { cursor: pointer; transition: all 0.15s; }
          .copy-btn:hover { background: rgba(0,0,0,0.05); }
          .copy-btn.copied { background: #22c55e22; }
          .copy-btn.copied::after { content: ' ✓'; color: #22c55e; }
        </style>
        <script>${copyScript}</script>
        
        <div class="mb-8">
          <h2 class="heading-s text-content-on-surface-primary mb-1">Columns</h2>
          <p class="body-14-regular text-content-on-surface-muted">Control the number of grid columns. Click class name to copy.</p>
        </div>
        
        <div class="gap-y-8">
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid grid-cols-2', this)">grid grid-cols-2</code>
            <div class="grid grid-cols-2 gap-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">4</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid grid-cols-3', this)">grid grid-cols-3</code>
            <div class="grid grid-cols-3 gap-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">4</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">5</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">6</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid grid-cols-4', this)">grid grid-cols-4</code>
            <div class="grid grid-cols-4 gap-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">4</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">5</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">6</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">7</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">8</div>
            </div>
          </div>
        </div>
      </div>
    `,
  }),
};

/**
 * Column span utilities for items that need to span multiple columns.
 * Use for featured content, headers, or elements that need more horizontal space.
 */
export const ColumnSpan: Story = {
  render: () => ({
    template: `
      <div class="p-8 bg-white dark:bg-background">
        <style>
          .copy-btn { cursor: pointer; transition: all 0.15s; }
          .copy-btn:hover { background: rgba(0,0,0,0.05); }
          .copy-btn.copied { background: #22c55e22; }
          .copy-btn.copied::after { content: ' ✓'; color: #22c55e; }
        </style>
        <script>${copyScript}</script>
        
        <div class="mb-8">
          <h2 class="heading-s text-content-on-surface-primary mb-1">Column Span</h2>
          <p class="body-14-regular text-content-on-surface-muted">Make items span multiple columns. Click class name to copy.</p>
        </div>
        
        <div class="gap-y-8">
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('col-span-2', this)">col-span-2</code>
            <div class="grid grid-cols-4 gap-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 col-span-2 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">span-2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 col-span-2 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">span-2</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('col-span-full', this)">col-span-full</code>
            <div class="grid grid-cols-4 gap-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 col-span-full bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">span-full</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
            </div>
          </div>
        </div>
      </div>
    `,
  }),
};

/**
 * Grid gap utilities for controlling spacing between grid items.
 * Use consistent gap values for visual harmony across grid layouts.
 */
export const Gap: Story = {
  render: () => ({
    template: `
      <div class="p-8 bg-white dark:bg-background">
        <style>
          .copy-btn { cursor: pointer; transition: all 0.15s; }
          .copy-btn:hover { background: rgba(0,0,0,0.05); }
          .copy-btn.copied { background: #22c55e22; }
          .copy-btn.copied::after { content: ' ✓'; color: #22c55e; }
        </style>
        <script>${copyScript}</script>
        
        <div class="mb-8">
          <h2 class="heading-s text-content-on-surface-primary mb-1">Gap</h2>
          <p class="body-14-regular text-content-on-surface-muted">Control spacing between grid items. Click class name to copy.</p>
        </div>
        
        <div class="gap-y-8">
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid gap-1', this)">grid gap-1</code>
            <div class="grid grid-cols-3 gap-1 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid gap-4', this)">grid gap-4</code>
            <div class="grid grid-cols-3 gap-4 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid gap-8', this)">grid gap-8</code>
            <div class="grid grid-cols-3 gap-8 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid gap-x-4 gap-y-2', this)">grid gap-x-4 gap-y-2</code>
            <div class="grid grid-cols-3 gap-x-4 gap-y-2 p-4 bg-background-surface-muted rounded-md">
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">4</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">5</div>
              <div class="h-16 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">6</div>
            </div>
          </div>
        </div>
      </div>
    `,
  }),
};

/**
 * Auto-fit and auto-fill grid templates for responsive layouts.
 * Creates flexible grids that automatically adjust the number of columns based on available space.
 */
export const AutoColumns: Story = {
  render: () => ({
    template: `
      <div class="p-8 bg-white dark:bg-background">
        <style>
          .copy-btn { cursor: pointer; transition: all 0.15s; }
          .copy-btn:hover { background: rgba(0,0,0,0.05); }
          .copy-btn.copied { background: #22c55e22; }
          .copy-btn.copied::after { content: ' ✓'; color: #22c55e; }
        </style>
        <script>${copyScript}</script>
        
        <div class="mb-8">
          <h2 class="heading-s text-content-on-surface-primary mb-1">Auto Columns</h2>
          <p class="body-14-regular text-content-on-surface-muted">Create responsive grids with auto-fit. Click class name to copy.</p>
        </div>
        
        <div class="gap-y-8">
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid grid-cols-[repeat(auto-fit,minmax(150px,1fr))]', this)">grid grid-cols-[repeat(auto-fit,minmax(150px,1fr))]</code>
            <div class="grid grid-cols-[repeat(auto-fit,minmax(150px,1fr))] gap-4 p-4 bg-background-surface-muted rounded-md">
              <div class="h-24 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="h-24 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="h-24 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
              <div class="h-24 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">4</div>
              <div class="h-24 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">5</div>
            </div>
            <p class="body-14-regular text-content-on-surface-muted mt-2">Resize the window to see columns adjust automatically.</p>
          </div>
        </div>
      </div>
    `,
  }),
};

/**
 * Place items utilities for positioning all grid items at once.
 * Combines justify and align for quick centering and positioning.
 */
export const PlaceItems: Story = {
  render: () => ({
    template: `
      <div class="p-8 bg-white dark:bg-background">
        <style>
          .copy-btn { cursor: pointer; transition: all 0.15s; }
          .copy-btn:hover { background: rgba(0,0,0,0.05); }
          .copy-btn.copied { background: #22c55e22; }
          .copy-btn.copied::after { content: ' ✓'; color: #22c55e; }
        </style>
        <script>${copyScript}</script>
        
        <div class="mb-8">
          <h2 class="heading-s text-content-on-surface-primary mb-1">Place Items</h2>
          <p class="body-14-regular text-content-on-surface-muted">Position all grid items at once. Click class name to copy.</p>
        </div>
        
        <div class="gap-y-8">
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid place-items-center', this)">grid place-items-center</code>
            <div class="grid grid-cols-3 place-items-center gap-4 p-4 bg-background-surface-muted rounded-md h-48">
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid place-items-start', this)">grid place-items-start</code>
            <div class="grid grid-cols-3 place-items-start gap-4 p-4 bg-background-surface-muted rounded-md h-48">
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
          
          <div>
            <code class="copy-btn label-12-regular text-content-on-surface-muted block mb-2 px-1 py-0.5 rounded w-fit" onclick="copyToClipboard('grid place-items-end', this)">grid place-items-end</code>
            <div class="grid grid-cols-3 place-items-end gap-4 p-4 bg-background-surface-muted rounded-md h-48">
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">1</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">2</div>
              <div class="w-12 h-12 bg-background-accent-default rounded flex items-center justify-center text-content-on-accent-default">3</div>
            </div>
          </div>
        </div>
      </div>
    `,
  }),
};
