import { Meta, StoryObj } from '@storybook/react-vite'

import {
  Carousel,
  CarouselApi,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
  CarouselProps,
} from './carousel'
import { Card, CardContent } from '@components/ui/card/card'
import { useEffect, useState } from 'react'

const meta = {
  title: 'Carousel',
  component: Carousel,
  tags: ['autodocs'],
  argTypes: {
    orientation: {
      control: 'select',
      options: ['horizontal', 'vertical'],
    },
  },
} satisfies Meta<typeof Carousel>

export default meta

export const Default: StoryObj<typeof meta> = {
  parameters: {
    docs: {
      description: {
        story: 'The Carousel component displays a list of items that can be scrolled through.',
      },
    },
  },
  args: {
    orientation: 'horizontal',
  },
  render: (args) => (
    <div className="flex items-center justify-center border rounded p-4">
      <Carousel {...args} className="w-full max-w-xs">
        <CarouselContent>
          {Array.from({ length: 5 }).map((_, index) => (
            <CarouselItem key={index} className="basis-1/3">
              <div className="p-1">
                <Card>
                  <CardContent className="flex aspect-square items-center justify-center p-6">
                    <span className="text-4xl font-semibold">{index + 1}</span>
                  </CardContent>
                </Card>
              </div>
            </CarouselItem>
          ))}
        </CarouselContent>
        <CarouselPrevious />
        <CarouselNext />
      </Carousel>
    </div>
  ),
}

function CarouselWithApi() {
  const [api, setApi] = useState<CarouselApi>()
  const [current, setCurrent] = useState(0)
  const [count, setCount] = useState(0)

  useEffect(() => {
    if (!api) {
      return
    }

    setCount(api.scrollSnapList().length)
    setCurrent(api.selectedScrollSnap() + 1)

    api.on('select', () => {
      setCurrent(api.selectedScrollSnap() + 1)
    })
  }, [api])

  return (
    <div className="mx-auto max-w-xs">
      <Carousel setApi={setApi} className="w-full max-w-xs">
        <CarouselContent>
          {Array.from({ length: 5 }).map((_, index) => (
            <CarouselItem key={index}>
              <Card>
                <CardContent className="flex aspect-square items-center justify-center p-6">
                  <span className="text-4xl font-semibold">{index + 1}</span>
                </CardContent>
              </Card>
            </CarouselItem>
          ))}
        </CarouselContent>
        <CarouselPrevious />
        <CarouselNext />
      </Carousel>
      <div className="py-2 text-center text-sm text-muted-foreground">
        Slide {current} of {count}
      </div>
    </div>
  )
}

export const WithApi: StoryObj<typeof meta> = {
  parameters: {
    docs: {
      description: {
        story:
          'Carousel with API integration showing current slide counter. The setApi prop allows you to access carousel methods and state for custom controls and indicators.',
      },
    },
  },
  render: () => (
    <div className="flex items-center justify-center border rounded p-4">
      <CarouselWithApi />
    </div>
  ),
}

export const Vertical: StoryObj<typeof meta> = {
  parameters: {
    docs: {
      description: {
        story:
          'Vertical orientation carousel that scrolls up and down instead of left and right. Perfect for vertical content layouts or space-constrained horizontal designs.',
      },
    },
  },
  args: {
    orientation: 'vertical',
  },
  render: (args) => (
    <div className="flex items-center justify-center border rounded p-4">
      <Carousel {...args} className="w-full max-w-xs">
        <CarouselContent className="h-[200px]">
          {Array.from({ length: 5 }).map((_, index) => (
            <CarouselItem key={index}>
              <div className="p-1">
                <Card>
                  <CardContent className="flex aspect-square items-center justify-center p-6">
                    <span className="text-4xl font-semibold">{index + 1}</span>
                  </CardContent>
                </Card>
              </div>
            </CarouselItem>
          ))}
        </CarouselContent>
        <CarouselPrevious />
        <CarouselNext />
      </Carousel>
    </div>
  ),
}
