import { UserAccountTypes } from '@rpg-engine/shared';
import type { Meta, StoryObj } from '@storybook/react';
import React from 'react';
import { RPGUIRoot } from '../../../components/RPGUI/RPGUIRoot';
import customSkinImage from '../../../components/Store/sections/images/custom-skin.png';
import { Store } from '../../../components/Store/Store';
import { IItemPack, IPurchase, IStoreItem } from '../../../components/Store/StoreTypes';
import itemsAtlasJSON from '../../../mocks/atlas/items/items.json';
import itemsAtlasIMG from '../../../mocks/atlas/items/items.png';
import { mockItems } from '../../../mocks/informationCenter.mocks';

const meta = {
  title: 'Features/Store',
  component: Store,
  parameters: {
    layout: 'centered',
  },
  decorators: [
    Story => (
      <RPGUIRoot>
        <Story />
      </RPGUIRoot>
    ),
  ],
} satisfies Meta<typeof Store>;

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

// Create mock items once, with fixed stock values
const storeItems: IStoreItem[] = mockItems.map((item, index) => ({
  _id: `original-${item.key}-${index}`,
  key: `original-${item.key}-${index}`,
  name: item.name,
  description: item.description,
  fullDescription: item.description,
  texturePath: item.texturePath,
  textureAtlas: 'items',
  textureKey: item.texturePath,
  price: item.basePrice,
  stock: 5 + (index % 5), // Fixed stock values based on index
  type: item.type,
  subType: item.subType,
  attack: item.attack || 0,
  defense: item.defense || 0,
  weight: item.weight,
  rarity: item.rarity,
  isEquipable: true,
  isStackable: item.maxStackSize > 1,
  isUsable: true,
  isStorable: true,
  hasUseWith: false,
  isSolid: false,
  isTwoHanded: false,
  isItemContainer: false,
  layer: 1,
  allowedEquipSlotType: item.allowedEquipSlotType || [],
  maxStackSize: item.maxStackSize || 1,
  usableEffectDescription: item.usableEffectDescription || '',
  canSell: item.canSell ?? true,
  rangeType: item.rangeType,
  entityEffects: item.entityEffects || [],
  entityEffectChance: item.entityEffectChance || 0,
  equippedBuff: item.equippedBuff || [],
  equippedBuffDescription: item.equippedBuffDescription || '',
  requiredAccountType: item.rarity === 'Legendary' ? [UserAccountTypes.PremiumGold] : [],
}));

// Create duplicated items once with unique keys
const duplicatedItems: IStoreItem[] = [
  ...storeItems,
  ...storeItems.map((item, index) => ({
    ...item,
    _id: `copy1-${item.key}-${index}`,
    key: `copy1-${item.key}-${index}`,
  })),
  ...storeItems.map((item, index) => ({
    ...item,
    _id: `copy2-${item.key}-${index}`,
    key: `copy2-${item.key}-${index}`,
  })),
  ...storeItems.map((item, index) => ({
    ...item,
    _id: `copy3-${item.key}-${index}`,
    key: `copy3-${item.key}-${index}`,
  })),
];

// Mock packs data
const mockPacks: IItemPack[] = [
  // Regular Packs
  {
    key: 'starter-pack',
    title: 'Starter Pack',
    description: 'Perfect for beginners! Get essential items to start your adventure.',
    priceUSD: 4.99,
    image: customSkinImage,
  },
  {
    key: 'custom-skin-pack',
    title: 'Custom Skin Pack',
    description: 'Get a unique look with this custom skin pack! Stand out from the crowd.',
    priceUSD: 7.99,
    image: customSkinImage,
  },

   {
    key: 'ultimate-pack',
    title: '👑 Ultimate Premium Pack',
    description: 'The most exclusive collection of items, effects, and perks. Includes everything from previous tiers plus legendary items!',
    priceUSD: 99.99,
    image: customSkinImage,
  },
    {
    key: 'gold-pack',
    title: '🥇 Gold Premium Pack',
    description: 'The ultimate premium experience with rare items, unique effects, and exclusive content. For true champions!',
    priceUSD: 49.99,
    image: customSkinImage,
  },  
  {
    key: 'silver-pack',
    title: '🥈 Silver Premium Pack',
    description: 'Enhanced premium features with more exclusive items and special effects. For dedicated adventurers.',
    priceUSD: 24.99,
    image: customSkinImage,
  },
  {
    key: 'bronze-pack',
    title: '🥉 Bronze Premium Pack',
    description: 'Start your premium journey with exclusive items and basic perks. Perfect for casual players.',
    priceUSD: 14.99,
    image: customSkinImage,
  },

];

// Create the story with the static mock data
export const Default: Story = {
  render: () => (
    <Store
      items={duplicatedItems}
      packs={mockPacks}
      userAccountType={UserAccountTypes.Free}
      onPurchase={(purchase: IPurchase) => {
        console.log('Purchase details:', purchase);
      }}
      atlasJSON={itemsAtlasJSON}
      atlasIMG={itemsAtlasIMG}
    />
  ),
};
