import type { Meta, StoryObj } from '@storybook/react-vite';
import { http, HttpResponse } from 'msw';
import { AdminTransfersView } from './AdminTransfersView';

const meta = {
  title: 'Components/Admin/AdminTransfersView',
  component: AdminTransfersView,
  tags: ['autodocs'],
} satisfies Meta<typeof AdminTransfersView>;

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

/** Default state with mocked transfers (MSW handlers-admin) */
export const Default: Story = {};

/** Empty state — no transfers */
export const Empty: Story = {
  parameters: {
    msw: {
      handlers: [
        http.get('*/api/v1/admin/transfers', () =>
          HttpResponse.json({
            success: true,
            data: { transfers: [], total: 0 },
          })
        ),
      ],
    },
  },
};

/** Only failed transfers (Retry button visible) */
export const WithFailedTransfers: Story = {
  parameters: {
    msw: {
      handlers: [
        http.get('*/api/v1/admin/transfers', () =>
          HttpResponse.json({
            success: true,
            data: {
              transfers: [
                {
                  id: 'tr-fail-1',
                  seller_id: 'seller-1',
                  order_id: 'ord-1',
                  amount_cents: 2699,
                  platform_fee_cents: 300,
                  currency: 'EUR',
                  status: 'failed',
                  error_message: 'Stripe API error',
                  retry_count: 1,
                  next_retry_at: new Date(Date.now() + 600000).toISOString(),
                  created_at: new Date(Date.now() - 3600000).toISOString(),
                },
                {
                  id: 'tr-fail-2',
                  seller_id: 'seller-2',
                  order_id: 'ord-2',
                  amount_cents: 4500,
                  platform_fee_cents: 500,
                  currency: 'EUR',
                  status: 'failed',
                  error_message: 'Insufficient funds',
                  retry_count: 2,
                  next_retry_at: new Date(Date.now() + 1200000).toISOString(),
                  created_at: new Date(Date.now() - 86400000).toISOString(),
                },
              ],
              total: 2,
            },
          })
        ),
      ],
    },
  },
};

/** Error state — API returns error */
export const Error: Story = {
  parameters: {
    msw: {
      handlers: [
        http.get('*/api/v1/admin/transfers', () =>
          HttpResponse.json({ success: false, error: 'Internal server error' }, { status: 500 })
        ),
      ],
    },
  },
};

/** Loading state — delayed response shows skeleton */
export const Loading: Story = {
  parameters: {
    msw: {
      handlers: [
        http.get('*/api/v1/admin/transfers', async () => {
          await new Promise((r) => setTimeout(r, 2000));
          return HttpResponse.json({
            success: true,
            data: {
              transfers: [
                {
                  id: 'tr-admin-1',
                  seller_id: 'seller-1',
                  order_id: 'ord-1',
                  amount_cents: 2699,
                  platform_fee_cents: 300,
                  currency: 'EUR',
                  status: 'completed',
                  retry_count: 0,
                  created_at: new Date().toISOString(),
                },
              ],
              total: 1,
            },
          });
        }),
      ],
    },
  },
};
