import React from 'react';
import { FaArrowLeft, FaCartPlus } from 'react-icons/fa';
import styled from 'styled-components';
import { CTAButton } from '../shared/CTAButton/CTAButton';
import { IItemPack, IStoreItem } from './StoreTypes';

interface IStoreItemDetailsProps {
  item: IStoreItem | (IItemPack & { name: string; texturePath: string });
  imageUrl: string | { src: string; default?: string };
  onBack: () => void;
  onAddToCart: (item: IStoreItem) => void;
}

export const StoreItemDetails: React.FC<IStoreItemDetailsProps> = ({
  item,
  onBack,
  onAddToCart,
  imageUrl,
}) => {
  const getImageSrc = () => {
    if (!imageUrl) return '/placeholder-thumbnail.png';
    if (typeof imageUrl === 'string') return imageUrl;
    return (
      (imageUrl as { default?: string; src: string }).default ||
      (imageUrl as { src: string }).src
    );
  };

  return (
    <Container>
      <Header>
        <BackButton onClick={onBack}>
          <FaArrowLeft />
          <span>Back</span>
        </BackButton>
      </Header>

      <Content>
        <DetailsGrid>
          <ItemIcon>
            <img src={getImageSrc()} alt={item.name} />
          </ItemIcon>
          <ItemInfo>
            <ItemName>{item.name}</ItemName>
            <ItemPrice>
              ${'priceUSD' in item ? item.priceUSD : item.price}
            </ItemPrice>
            <Description>{item.description}</Description>
          </ItemInfo>
        </DetailsGrid>

        <Actions>
          <CTAButton
            icon={<FaCartPlus />}
            label="Add to Cart"
            onClick={() => onAddToCart(item as IStoreItem)}
            fullWidth
          />
        </Actions>
      </Content>
    </Container>
  );
};

const Container = styled.div`
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 1.5rem;
  height: 100%;
`;

const Header = styled.div`
  display: flex;
  align-items: center;
  gap: 1rem;
`;

const BackButton = styled.button`
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: none;
  border: none;
  color: #ffffff;
  font-family: 'Press Start 2P', cursive;
  font-size: 0.875rem;
  cursor: pointer;
  padding: 0.5rem;
  transition: opacity 0.2s;

  &:hover {
    opacity: 0.8;
  }
`;

const Content = styled.div`
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  overflow-y: auto;
`;

const DetailsGrid = styled.div`
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 2rem;
  align-items: start;

  @media (max-width: 768px) {
    grid-template-columns: 1fr;
  }
`;

const ItemIcon = styled.div`
  width: 100%;
  aspect-ratio: 1;
  background: rgba(0, 0, 0, 0.2);
  overflow: hidden;
  border-radius: 4px;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
`;

const ItemInfo = styled.div`
  display: flex;
  flex-direction: column;
  gap: 1rem;
`;

const ItemName = styled.h2`
  margin: 0;
  font-family: 'Press Start 2P', cursive;
  font-size: 1.25rem;
  color: #fef08a;
`;

const ItemPrice = styled.div`
  font-family: 'Press Start 2P', cursive;
  font-size: 1rem;
  color: #fef08a;
`;

const Description = styled.p`
  margin: 0;
  font-family: 'Press Start 2P', cursive;
  font-size: 0.875rem;
  line-height: 1.6;
  color: #ffffff;
`;

const Actions = styled.div`
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
`;
