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

test.describe("database connection", () => {
  test("tests the configured Local DB connection", async ({ page }) => {
    await page.goto("/");
    await page.locator("#settingsButton").click();

    await expect(page.locator('input[name="databaseMode"][value="local"]')).toBeChecked();
    await page.locator("#localDbHost").fill("127.0.0.1");
    await page.locator("#localDbPort").fill("3306");
    await page.locator("#localDbName").fill("9bplus");
    await page.locator("#localDbUser").fill("root");
    await page.locator("#localDbPassword").fill("root");

    await page.locator("#testLocalDbButton").click();
    await expect(page.locator("#localDbTestResult")).toContainText("Connected:");
  });

  test("keeps the scrape database as a separate local mode with its own permissions", async ({ page }) => {
    await page.goto("/");
    await page.locator("#settingsButton").click();

    await expect(page.locator('input[name="databaseMode"][value="local"] + span')).toHaveText("Local 9bplus DB");
    await expect(page.locator('input[name="databaseMode"][value="live"] + span')).toHaveText("Live 9bplus DB");
    await page.locator('input[name="databaseMode"][value="scrape"]').check();
    await expect(page.locator("#databaseModeNote")).toContainText("Local scrape DB selected");
    await expect(page.locator("#allowInsertToggle")).toBeEnabled();
    await expect(page.locator("#allowUpdateToggle")).toBeEnabled();
    await expect(page.locator("#allowDeleteToggle")).toBeEnabled();
    await page.locator("#allowUpdateToggle").check();

    await page.reload();
    await page.locator("#settingsButton").click();
    await page.locator('input[name="databaseMode"][value="scrape"]').check();
    await expect(page.locator("#allowUpdateToggle")).toBeChecked();

    await page.locator('input[name="databaseMode"][value="local"]').check();
    await expect(page.locator("#allowInsertToggle")).not.toBeChecked();
    await expect(page.locator("#allowUpdateToggle")).not.toBeChecked();
    await expect(page.locator("#allowDeleteToggle")).not.toBeChecked();

    await page.locator('input[name="databaseMode"][value="live"]').check();
    await expect(page.locator("#allowInsertToggle")).toBeDisabled();
    await expect(page.locator("#allowUpdateToggle")).toBeDisabled();
    await expect(page.locator("#allowDeleteToggle")).toBeDisabled();
  });

  test("loads manufacturers from Local DB without Live endpoint fallback", async ({ page }) => {
    const manufacturersResponse = page.waitForResponse("**/api/manufacturers");
    await page.goto("/");

    const response = await manufacturersResponse;
    expect(response.ok()).toBeTruthy();
    const data = await response.json();
    expect(data.items.length).toBeGreaterThan(0);
    expect(data.items.some((item) => item.name === "La Sportiva")).toBeTruthy();
  });

  test("persists Local 9bplus write permissions across reloads", async ({ page }) => {
    await page.goto("/");
    await page.locator("#settingsButton").click();
    await page.locator('input[name="databaseMode"][value="local"]').check();
    await page.locator("#allowInsertToggle").check();
    await page.locator("#allowUpdateToggle").check();
    await page.locator("#allowDeleteToggle").check();

    await page.reload();
    await page.locator("#settingsButton").click();

    await expect(page.locator('input[name="databaseMode"][value="local"]')).toBeChecked();
    await expect(page.locator("#allowInsertToggle")).toBeChecked();
    await expect(page.locator("#allowUpdateToggle")).toBeChecked();
    await expect(page.locator("#allowDeleteToggle")).toBeChecked();
  });
});
