import { describe, it, expect, vi, beforeEach } from "vitest";

/**
 * Tests for PG Soft game launch integration.
 * These tests verify the logic for deriving game codes and building launch URLs.
 */

describe("PG Soft game code derivation", () => {
  it("should derive game code from gameId by stripping pgsoft- prefix", () => {
    const gameId = "pgsoft-fortune-tiger";
    const gameCode = gameId.replace(/^pgsoft-/, "");
    expect(gameCode).toBe("fortune-tiger");
  });

  it("should derive game code for all 11 PG Soft games", () => {
    const games = [
      { gameId: "pgsoft-fortune-tiger", expected: "fortune-tiger" },
      { gameId: "pgsoft-fortune-ox", expected: "fortune-ox" },
      { gameId: "pgsoft-fortune-dragon", expected: "fortune-dragon" },
      { gameId: "pgsoft-fortune-rabbit", expected: "fortune-rabbit" },
      { gameId: "pgsoft-fortune-mouse", expected: "fortune-mouse" },
      { gameId: "pgsoft-bikini-paradise", expected: "bikini-paradise" },
      { gameId: "pgsoft-jungle-delight", expected: "jungle-delight" },
      { gameId: "pgsoft-ganesha-gold", expected: "ganesha-gold" },
      { gameId: "pgsoft-double-fortune", expected: "double-fortune" },
      { gameId: "pgsoft-dragon-tiger-luck", expected: "dragon-tiger-luck" },
      { gameId: "pgsoft-butterfly-blossom", expected: "butterfly-blossom" },
    ];

    for (const { gameId, expected } of games) {
      const gameCode = gameId.replace(/^pgsoft-/, "");
      expect(gameCode).toBe(expected);
    }
  });
});

describe("PG Soft launch URL proxying", () => {
  it("should replace localhost:3001 with the proxied origin path", () => {
    const rawUrl = "http://localhost:3001/126/index.html?operator_token=Zm9saWFiZXQ=&btt=1&t=abc123&or=localhost:3001&api=localhost:3001";
    const origin = "https://coinkrazy-38mmepdj.manus.space";
    const proxiedUrl = rawUrl.replace(
      /http:\/\/localhost:3001\//,
      `${origin}/pgsoft/`
    );
    expect(proxiedUrl).toBe(
      "https://coinkrazy-38mmepdj.manus.space/pgsoft/126/index.html?operator_token=Zm9saWFiZXQ=&btt=1&t=abc123&or=localhost:3001&api=localhost:3001"
    );
  });

  it("should identify PG Soft games by provider name", () => {
    const pgSoftGame = { provider: "PG Soft", gameId: "pgsoft-fortune-tiger" };
    const otherGame = { provider: "NetEnt", gameId: "starburst" };

    expect(pgSoftGame.provider === "PG Soft").toBe(true);
    expect(otherGame.provider === "PG Soft").toBe(false);
  });
});

describe("PG Soft user code generation", () => {
  it("should generate user code from user ID", () => {
    const userId = 42;
    const userCode = `ck_${userId}`;
    expect(userCode).toBe("ck_42");
  });

  it("should generate unique user codes for different users", () => {
    const user1Code = `ck_${1}`;
    const user2Code = `ck_${2}`;
    expect(user1Code).not.toBe(user2Code);
  });
});
