/**
 * E2E Tests: Quotation Lifecycle
 *
 * Covers:
 * 1. Portal page rendering (invalid tokens → "Quotation Not Found")
 * 2. Payment success page (English + Arabic)
 * 3. Portal API integration (GET/POST with invalid tokens)
 * 4. Dashboard quotations API (auth required)
 * 5. Trash tab filter logic
 */

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

// ---------------------------------------------------------------------------
// 1. Portal Page Rendering
// ---------------------------------------------------------------------------
test.describe("Quotation Portal", () => {
  test('shows "Quotation Not Found" for invalid token', async ({ page }) => {
    await page.goto("/quotation/invalidtoken123");
    // Page loads a client component that fetches the API, then renders error
    await expect(page.getByText("Quotation Not Found")).toBeVisible({
      timeout: 15_000,
    });
  });

  test('shows "Quotation Not Found" for short token', async ({ page }) => {
    await page.goto("/quotation/abc");
    // Short token triggers 400 from API, client renders not-found state
    await expect(page.getByText("Quotation Not Found")).toBeVisible({
      timeout: 15_000,
    });
  });
});

// ---------------------------------------------------------------------------
// 2. Payment Success Page
// ---------------------------------------------------------------------------
test.describe("Payment Success Page", () => {
  test("shows success for quotation payment", async ({ page }) => {
    await page.goto("/en/payment/success?type=quotation");
    await expect(page.getByText("Payment Successful")).toBeVisible({
      timeout: 15_000,
    });
  });

  test("shows success for Arabic with no params", async ({ page }) => {
    await page.goto("/ar/payment/success");
    await expect(page.getByText("تم الدفع بنجاح")).toBeVisible({
      timeout: 15_000,
    });
  });

  test("shows success for English with no params", async ({ page }) => {
    await page.goto("/en/payment/success");
    await expect(page.getByText("Payment Successful")).toBeVisible({
      timeout: 15_000,
    });
  });
});

// ---------------------------------------------------------------------------
// 3. Portal API Integration
// ---------------------------------------------------------------------------
test.describe("Portal API", () => {
  test("GET portal with invalid token returns 404", async ({ request }) => {
    const res = await request.get(
      "/api/quotations/portal/nonexistenttoken99",
    );
    expect(res.status()).toBe(404);
    const data = await res.json();
    expect(data.success).toBe(false);
  });

  test("GET portal with short token returns 400", async ({ request }) => {
    const res = await request.get("/api/quotations/portal/ab");
    expect(res.status()).toBe(400);
    const data = await res.json();
    expect(data.success).toBe(false);
    expect(data.error).toBe("Invalid token");
  });

  test("POST accept with invalid token returns 404", async ({ request }) => {
    const res = await request.post(
      "/api/quotations/portal/nonexistenttoken99/accept",
    );
    expect(res.status()).toBe(404);
    const data = await res.json();
    expect(data.success).toBe(false);
  });

  test("POST decline with invalid token returns 404", async ({ request }) => {
    const res = await request.post(
      "/api/quotations/portal/nonexistenttoken99/decline",
    );
    expect(res.status()).toBe(404);
    const data = await res.json();
    expect(data.success).toBe(false);
  });

  test("POST pay with invalid token returns 400 (short token)", async ({
    request,
  }) => {
    // Short token triggers the < 10 char validation before any DB lookup
    const res = await request.post("/api/quotations/portal/abc/pay", {
      data: {
        successUrl: "http://localhost:9001/success",
        cancelUrl: "http://localhost:9001/cancel",
      },
    });
    expect(res.status()).toBe(400);
    const data = await res.json();
    expect(data.success).toBe(false);
    expect(data.error).toBe("Invalid token");
  });

  test("POST pay with valid-length but nonexistent token returns error", async ({
    request,
  }) => {
    const res = await request.post(
      "/api/quotations/portal/nonexistenttoken99/pay",
      {
        data: {
          successUrl: "http://localhost:9001/success",
          cancelUrl: "http://localhost:9001/cancel",
        },
      },
    );
    // Could be 400 (invalid redirect URL) or 404/500 depending on config
    expect([400, 404, 500]).toContain(res.status());
    const data = await res.json();
    expect(data.success).toBe(false);
  });
});

// ---------------------------------------------------------------------------
// 4. Dashboard Quotations (auth required)
// ---------------------------------------------------------------------------
test.describe("Dashboard Quotations", () => {
  test("quotations list API returns unauthorized without auth", async ({
    request,
  }) => {
    const res = await request.get("/api/dashboard/quotations");
    expect(res.status()).toBe(401);
    const data = await res.json();
    expect(data.success).toBe(false);
  });

  test("quotation detail API returns unauthorized without auth", async ({
    request,
  }) => {
    const res = await request.get("/api/dashboard/quotations/fake-id");
    expect(res.status()).toBe(401);
    const data = await res.json();
    expect(data.success).toBe(false);
  });

  test("quotation delete API returns unauthorized without auth", async ({
    request,
  }) => {
    const res = await request.delete("/api/dashboard/quotations/fake-id");
    expect(res.status()).toBe(401);
  });
});

// ---------------------------------------------------------------------------
// 5. Trash Tab Filter Logic
// ---------------------------------------------------------------------------
test.describe("Trash Tab", () => {
  test("GET quotations with status=CANCELLED returns 401 without auth", async ({
    request,
  }) => {
    const res = await request.get(
      "/api/dashboard/quotations?status=CANCELLED",
    );
    expect(res.status()).toBe(401);
    const data = await res.json();
    expect(data.success).toBe(false);
  });
});
