import {
  buildAccountPageContext,
  buildBasketPageContext,
  buildCustomPageContext,
  buildHomepagePageContext,
  buildListingPageContext,
  buildProductPageContext
} from '../theme-page-context';

describe('theme-page-context utils', () => {
  it('builds homepage and custom page contexts', () => {
    const homepage = buildHomepagePageContext();
    const customPage = buildCustomPageContext('summer-campaign');

    expect(homepage.homepage.slug).toBe('homepage');
    expect(customPage.customPage.slug).toBe('summer-campaign');
    expect(customPage.customPage.title).toBe('Summer Campaign');
  });

  it('builds product page context with normalized product aliases', () => {
    const productPageContext = buildProductPageContext({
      product: {
        pk: 1,
        name: 'Spring Sneaker',
        absolute_url: '/products/spring-sneaker',
        productimage_set: [{ image: 'https://cdn.example.com/image.jpg' }],
        price: '199.90',
        retail_price: '249.90',
        currency_type: 'TRY',
        attributes_kwargs: {}
      },
      selected_variant: null,
      variants: []
    } as never);

    expect(productPageContext.product.title).toBe('Spring Sneaker');
    expect(productPageContext.product.url).toBe('/products/spring-sneaker');
    expect(productPageContext.product.images[0].url).toBe(
      'https://cdn.example.com/image.jpg'
    );
  });

  it('builds listing context aliases for category/search/plp style pages', () => {
    const listingContext = buildListingPageContext({
      data: {
        category: {
          name: 'Sneakers',
          absolute_url: '/category/sneakers',
          attributes: {
            category_seo_title: {
              value: {
                text: 'Sneaker landing'
              }
            }
          }
        },
        products: [
          {
            pk: 1,
            name: 'Spring Sneaker',
            absolute_url: '/products/spring-sneaker',
            productimage_set: [{ image: 'https://cdn.example.com/image.jpg' }],
            price: '199.90',
            retail_price: '249.90',
            currency_type: 'TRY',
            attributes_kwargs: {}
          }
        ],
        facets: [{ key: 'color', name: 'Color' }],
        pagination: { count: 12 },
        sorters: [{ label: 'Newest', value: 'newest' }],
        search_text: 'sneaker'
      } as never,
      breadcrumbData: [{ label: 'Sneakers', path: '/category/sneakers' }] as never,
      pageType: 'search',
      path: '/list'
    });

    expect(listingContext.page.type).toBe('search');
    expect(listingContext.category.name).toBe('Sneakers');
    expect(listingContext.search.query).toBe('sneaker');
    expect(listingContext.plp.products).toHaveLength(1);
    expect(listingContext.listing.totalProducts).toBe(12);
  });

  it('builds basket and account contexts from live data', () => {
    const basketContext = buildBasketPageContext({
      basket: {
        pk: 10,
        total_quantity: 3,
        total_amount: '799.90',
        total_product_amount: '899.90',
        total_discount_amount: '100.00',
        voucher_code: 'SPRING',
        discounts: [],
        basketitem_set: [
          {
            id: 1,
            quantity: 2,
            price: '399.95',
            retail_price: '449.95',
            total_amount: '799.90',
            unit_price: '399.95',
            currency_type: 'TRY',
            image: 'https://cdn.example.com/image.jpg',
            attributes_kwargs: {
              color: { label: 'Color', value: 'Black' }
            },
            product: {
              pk: 1,
              name: 'Spring Sneaker',
              absolute_url: '/products/spring-sneaker',
              productimage_set: [
                { image: 'https://cdn.example.com/image.jpg' }
              ],
              price: '399.95',
              retail_price: '449.95',
              currency_type: 'TRY',
              attributes_kwargs: {}
            }
          }
        ]
      } as never,
      path: '/baskets/basket'
    });

    const accountContext = buildAccountPageContext({
      path: '/account/profile',
      profileInfo: {
        first_name: 'Ada',
        last_name: 'Lovelace',
        email: 'ada@example.com'
      },
      sessionUser: {
        name: 'Ada Lovelace',
        email: 'ada@example.com'
      }
    });

    expect(basketContext.basket.totalQuantity).toBe(3);
    expect(basketContext.basket.items[0].title).toBe('Spring Sneaker');
    expect(accountContext.account.user.fullName).toBe('Ada Lovelace');
    expect(accountContext.account.route).toBe('/account/profile');
  });
});
