import { test, expect } from '@playwright/test';

test.describe('shop catalog', () => {
  test('products page lists items and the filter controls', async ({ page }) => {
    await page.goto('/company/shop/products');

    await expect(page).toHaveTitle('Light-e-tech Corporation');
    await expect(page.getByRole('heading', { name: 'Products', exact: true })).toBeVisible();
    await expect(page.getByPlaceholder('Search')).toBeVisible();
    await expect(page.locator('select[name="category"]')).toBeVisible();
    await expect(page.locator('select[name="sort"]')).toBeVisible();
    await expect(page.getByRole('button', { name: 'Filter' })).toBeVisible();
    await expect(page.getByRole('heading', { name: '3 in 1 Utility Kit' })).toBeVisible();
    await expect(page.getByRole('heading', { name: 'Tshirt1' })).toBeVisible();
  });

  test('search keeps matching products and drops the rest', async ({ page }) => {
    await page.goto('/company/shop/products');
    await page.getByPlaceholder('Search').fill('Tshirt');
    await page.getByRole('button', { name: 'Filter' }).click();

    await expect(page).toHaveURL(/q=Tshirt/);
    await expect(page.getByRole('heading', { name: 'Tshirt1' })).toBeVisible();
    await expect(page.getByRole('heading', { name: '3 in 1 Utility Kit' })).toHaveCount(0);

    await page.getByPlaceholder('Search').fill('nomatchxyz');
    await page.getByRole('button', { name: 'Filter' }).click();
    await expect(page.getByRole('heading', { name: 'Tshirt1' })).toHaveCount(0);
    await expect(page.getByRole('heading', { name: '3 in 1 Utility Kit' })).toHaveCount(0);
  });

  test('sort and category filters stay on the products page', async ({ page }) => {
    await page.goto('/company/shop/products');

    await page.locator('select[name="sort"]').selectOption('name');
    await page.getByRole('button', { name: 'Filter' }).click();
    await expect(page).toHaveURL(/sort=name/);
    await expect(page.getByRole('heading', { name: 'Products', exact: true })).toBeVisible();
    await expect(page.locator('select[name="sort"]')).toHaveValue('name');

    const category = page.locator('select[name="category"]');
    const optionValue = await category.locator('option').nth(1).getAttribute('value');
    expect(optionValue).toBeTruthy();
    await category.selectOption(optionValue!);
    await page.getByRole('button', { name: 'Filter' }).click();
    await expect(page).toHaveURL(new RegExp(`category=${optionValue}`));
    await expect(page.locator('select[name="category"]')).toHaveValue(optionValue!);
  });

  test('a product page shows the name, price, and add-to-cart action', async ({ page }) => {
    await page.goto('/company/shop/products');
    await page.locator('.home_service_tile', { hasText: 'Tshirt1' }).getByRole('link', { name: 'View' }).click();

    await expect(page).toHaveURL(/\/company\/shop\/product\//);
    await expect(page.locator('section').getByRole('heading', { name: 'Tshirt1' }).first()).toBeVisible();
    await expect(page.locator('section').getByText(/USD\s+30\.00/).first()).toBeVisible();
    await expect(page.getByRole('button', { name: 'Add to cart' }).first()).toBeVisible();
  });

  test('an empty cart and an empty checkout explain that nothing is selected', async ({ page }) => {
    await page.goto('/company/shop/cart');
    await expect(page.getByRole('heading', { name: 'Cart', exact: true }).first()).toBeVisible();
    await expect(page.getByText('Your cart is empty.')).toBeVisible();
    await expect(page.getByRole('link', { name: 'Continue shopping' })).toBeVisible();

    await page.goto('/company/shop/checkout');
    await expect(page.getByRole('heading', { name: 'Checkout', exact: true }).first()).toBeVisible();
    await expect(page.locator('.alert-warning').getByText('Your cart is empty')).toBeVisible();
    await expect(page.getByRole('link', { name: 'Back to cart' }).first()).toBeVisible();
  });

  test('confirmation without an order explains that the order was not found', async ({ page }) => {
    await page.goto('/company/shop/confirmation');
    await expect(page.getByRole('heading', { name: 'Thank you' }).first()).toBeVisible();
    await expect(page.locator('section').getByText('We could not find this order.')).toBeVisible();
    await page.locator('a.btn', { hasText: 'Home' }).click();
    await expect(page).toHaveURL(/\/company\/home$/);
  });
});

test.describe('shop cart', () => {
  test('adding a product reaches checkout, then the item can be removed', async ({ page }) => {
    await page.goto('/company/shop/products?q=Tshirt');
    await page.getByRole('button', { name: 'Add to cart' }).click();

    await expect(page).toHaveURL(/\/company\/shop\/cart$/);
    await expect(page.getByRole('cell', { name: 'Tshirt1' })).toBeVisible();
    await expect(page.getByRole('link', { name: 'Checkout' })).toBeVisible();

    await page.getByRole('link', { name: 'Checkout' }).click();
    await expect(page).toHaveURL(/\/company\/shop\/checkout$/);
    await expect(page.getByText(/Total due:/)).toBeVisible();
    await expect(page.getByText(/USD\s+30\.00/)).toBeVisible();
    await expect(page.getByPlaceholder('Name')).toBeVisible();
    await expect(page.getByPlaceholder('Email')).toBeVisible();
    await expect(page.getByRole('button', { name: 'Pay with Stripe' })).toBeVisible();
    await expect(page.getByText('Card details are never stored here.')).toBeVisible();

    await page.goto('/company/shop/cart');
    await page.getByRole('button', { name: 'Remove' }).click();
    await expect(page.getByText('Your cart is empty.')).toBeVisible();
  });
});
