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

test.describe("Cart Flow", () => {
  test("should render warenkorb page with heading", async ({ page }) => {
    await page.goto("/warenkorb");
    await expect(page.locator("h1")).toContainText("Warenkorb");
  });

  test("should show empty state when cart is empty", async ({ page }) => {
    await page.goto("/warenkorb");
    await expect(page.getByTestId("warenkorb-empty")).toBeVisible();
    await expect(page.getByTestId("warenkorb-empty-cta")).toBeVisible();
  });

  test("should have link to mietkatalog from empty state", async ({ page }) => {
    await page.goto("/warenkorb");
    const cta = page.getByTestId("warenkorb-empty-cta");
    await expect(cta).toHaveAttribute("href", "/mietkatalog");
  });

  test("should show header cart icon", async ({ page }) => {
    await page.goto("/");
    await expect(page.getByTestId("header-cart-button")).toBeVisible();
  });

  test("should open cart drawer when header cart icon clicked", async ({ page }) => {
    await page.goto("/");
    await page.getByTestId("header-cart-button").click();
    await expect(page.getByTestId("cart-drawer")).toBeVisible();
  });

  test("should show empty state in cart drawer", async ({ page }) => {
    await page.goto("/");
    await page.getByTestId("header-cart-button").click();
    await expect(page.getByTestId("cart-drawer")).toBeVisible();
    await expect(page.locator("text=Ihr Warenkorb ist leer")).toBeVisible();
  });

  test("should close cart drawer on backdrop click", async ({ page }) => {
    await page.goto("/");
    await page.getByTestId("header-cart-button").click();
    await expect(page.getByTestId("cart-drawer")).toBeVisible();
    await page.getByTestId("cart-drawer-backdrop").click();
    await expect(page.getByTestId("cart-drawer")).not.toBeVisible();
  });

  test("should have checkout link in cart drawer", async ({ page }) => {
    await page.goto("/");
    await page.getByTestId("header-cart-button").click();
    await expect(page.getByTestId("cart-drawer")).toBeVisible();
    const checkout = page.getByTestId("cart-drawer-checkout");
    await expect(checkout).toHaveAttribute("href", "/mietanfrage");
  });

  test("should have view cart link in cart drawer", async ({ page }) => {
    await page.goto("/");
    await page.getByTestId("header-cart-button").click();
    await expect(page.getByTestId("cart-drawer")).toBeVisible();
    const viewCart = page.getByTestId("cart-drawer-view-cart");
    await expect(viewCart).toHaveAttribute("href", "/warenkorb");
  });

  test("should have mobile cart button", async ({ page }) => {
    await page.goto("/");
    await page.setViewportSize({ width: 375, height: 667 });
    await expect(page.getByTestId("header-cart-button-mobile")).toBeVisible();
  });
});
