import { Spinner } from "./index";

import { DocsPage } from "@shared/stories/DocsTemplate";
import type { Meta, StoryObj } from "@storybook/react";

const meta: Meta<typeof Spinner> = {
  title: "Components/Spinner",
  component: Spinner,
  tags: ["autodocs"],
  parameters: {
    layout: "centered",
    docs: {
      page: DocsPage,
      description: {
        component:
          "A loading spinner component with customizable size, color, and animation.",
      },
    },
  },
  argTypes: {
    size: {
      control: { type: "select" },
      options: ["small", "medium", "large", "xlarge"],
      description: "Spinner size",
    },
    color: {
      control: { type: "color" },
      description: "Spinner color",
    },
    className: {
      control: { type: "text" },
      description: "Additional CSS classes",
    },
  },
  args: {
    size: "medium",
  },
};

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

// Default spinner
export const Default: Story = {
  args: {},
};

// Different sizes
export const Sizes: Story = {
  render: () => (
    <div className="flex items-center gap-8">
      <div className="text-center">
        <Spinner size="small" />
        <p className="mt-2 text-xs">Small</p>
      </div>
      <div className="text-center">
        <Spinner size="medium" />
        <p className="mt-2 text-xs">Medium</p>
      </div>
      <div className="text-center">
        <Spinner size="large" />
        <p className="mt-2 text-xs">Large</p>
      </div>
      <div className="text-center">
        <Spinner size="xlarge" />
        <p className="mt-2 text-xs">XLarge</p>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "All available spinner sizes.",
      },
    },
  },
};

// Colored spinners
export const ColoredSpinners: Story = {
  render: () => (
    <div className="grid grid-cols-4 gap-6">
      <div className="text-center">
        <Spinner size="large" color="#3b82f6" />
        <p className="mt-2 text-sm text-blue-600">Blue</p>
      </div>
      <div className="text-center">
        <Spinner size="large" color="#10b981" />
        <p className="mt-2 text-sm text-green-600">Green</p>
      </div>
      <div className="text-center">
        <Spinner size="large" color="#ef4444" />
        <p className="mt-2 text-sm text-red-600">Red</p>
      </div>
      <div className="text-center">
        <Spinner size="large" color="#f59e0b" />
        <p className="mt-2 text-sm text-yellow-600">Yellow</p>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Spinners with different colors.",
      },
    },
  },
};

// Spinner with text
export const SpinnerWithText: Story = {
  render: () => (
    <div className="space-y-6">
      <div className="flex items-center gap-3">
        <Spinner size="small" />
        <span className="text-sm text-gray-600">Loading...</span>
      </div>
      <div className="flex items-center gap-3">
        <Spinner size="medium" />
        <span className="text-sm text-gray-600">Processing request...</span>
      </div>
      <div className="flex items-center gap-3">
        <Spinner size="large" />
        <span className="text-sm text-gray-600">Saving changes...</span>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Spinners used alongside loading text.",
      },
    },
  },
};

// Button with spinner
export const ButtonWithSpinner: Story = {
  render: () => (
    <div className="space-y-4">
      <button className="flex items-center gap-2 rounded bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 disabled:opacity-50">
        <Spinner size="small" color="white" />
        <span>Loading...</span>
      </button>

      <button className="flex items-center gap-2 rounded bg-gray-600 px-4 py-2 text-white hover:bg-gray-700 disabled:opacity-50">
        <Spinner size="small" color="white" />
        <span>Processing...</span>
      </button>

      <button className="flex items-center gap-2 rounded bg-green-600 px-4 py-2 text-white hover:bg-green-700 disabled:opacity-50">
        <Spinner size="small" color="white" />
        <span>Saving...</span>
      </button>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Spinners integrated into buttons for loading states.",
      },
    },
  },
};

// Card with spinner
export const CardWithSpinner: Story = {
  render: () => (
    <div className="grid grid-cols-2 gap-6">
      <div className="rounded-lg border p-6">
        <div className="flex h-32 items-center justify-center">
          <div className="text-center">
            <Spinner size="large" />
            <p className="mt-3 text-sm text-gray-600">Loading content...</p>
          </div>
        </div>
      </div>

      <div className="rounded-lg border p-6">
        <div className="flex h-32 items-center justify-center">
          <div className="text-center">
            <Spinner size="large" color="#10b981" />
            <p className="mt-3 text-sm text-gray-600">Processing data...</p>
          </div>
        </div>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Spinners in card layouts for content loading states.",
      },
    },
  },
};

// Inline spinner
export const InlineSpinner: Story = {
  render: () => (
    <div className="space-y-4">
      <p className="text-gray-700">
        This is some text with an inline{" "}
        <Spinner size="small" className="mx-1 inline" /> spinner.
      </p>

      <p className="text-gray-700">
        Another example with a{" "}
        <Spinner size="small" color="#ef4444" className="mx-1 inline" /> colored
        spinner.
      </p>

      <div className="flex items-center gap-2">
        <span>Status:</span>
        <Spinner size="small" />
        <span className="text-sm text-gray-500">Checking...</span>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Inline spinners within text content.",
      },
    },
  },
};

// Loading states
export const LoadingStates: Story = {
  render: () => (
    <div className="space-y-6">
      <div className="rounded-lg border p-4">
        <h3 className="mb-2 font-medium">Initial Loading</h3>
        <div className="flex items-center gap-3">
          <Spinner size="medium" />
          <span className="text-sm text-gray-600">Loading application...</span>
        </div>
      </div>

      <div className="rounded-lg border p-4">
        <h3 className="mb-2 font-medium">Data Fetching</h3>
        <div className="flex items-center gap-3">
          <Spinner size="medium" color="#3b82f6" />
          <span className="text-sm text-gray-600">Fetching user data...</span>
        </div>
      </div>

      <div className="rounded-lg border p-4">
        <h3 className="mb-2 font-medium">Form Submission</h3>
        <div className="flex items-center gap-3">
          <Spinner size="medium" color="#10b981" />
          <span className="text-sm text-gray-600">Submitting form...</span>
        </div>
      </div>

      <div className="rounded-lg border p-4">
        <h3 className="mb-2 font-medium">File Upload</h3>
        <div className="flex items-center gap-3">
          <Spinner size="medium" color="#f59e0b" />
          <span className="text-sm text-gray-600">Uploading file...</span>
        </div>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Different loading states with appropriate messaging.",
      },
    },
  },
};

// All variants showcase
export const AllVariants: Story = {
  render: () => (
    <div className="space-y-8">
      <div>
        <h3 className="mb-4 text-lg font-semibold">Spinner Sizes</h3>
        <div className="flex items-center gap-8">
          <div className="text-center">
            <Spinner size="small" />
            <p className="mt-2 text-xs">Small</p>
          </div>
          <div className="text-center">
            <Spinner size="medium" />
            <p className="mt-2 text-xs">Medium</p>
          </div>
          <div className="text-center">
            <Spinner size="large" />
            <p className="mt-2 text-xs">Large</p>
          </div>
          <div className="text-center">
            <Spinner size="xlarge" />
            <p className="mt-2 text-xs">XLarge</p>
          </div>
        </div>
      </div>

      <div>
        <h3 className="mb-4 text-lg font-semibold">Color Variants</h3>
        <div className="grid grid-cols-5 gap-4">
          <div className="text-center">
            <Spinner size="large" color="#3b82f6" />
            <p className="mt-2 text-xs text-blue-600">Blue</p>
          </div>
          <div className="text-center">
            <Spinner size="large" color="#10b981" />
            <p className="mt-2 text-xs text-green-600">Green</p>
          </div>
          <div className="text-center">
            <Spinner size="large" color="#ef4444" />
            <p className="mt-2 text-xs text-red-600">Red</p>
          </div>
          <div className="text-center">
            <Spinner size="large" color="#f59e0b" />
            <p className="mt-2 text-xs text-yellow-600">Yellow</p>
          </div>
          <div className="text-center">
            <Spinner size="large" color="#8b5cf6" />
            <p className="mt-2 text-xs text-purple-600">Purple</p>
          </div>
        </div>
      </div>
    </div>
  ),
  parameters: {
    docs: {
      description: {
        story: "Complete showcase of all spinner sizes and colors.",
      },
    },
  },
};
