import type { SessionSnapshot } from "../shared/types";

export type ScreenshotScene = "start" | "lobby" | "home" | "transfers" | "chat";

export type ScreenshotFixture = {
  scene: ScreenshotScene;
  sessionId: string;
  sessionCode: string;
  playerId: string;
  session: SessionSnapshot | null;
  navigationTarget: {
    root: "EntryLanding" | "Lobby" | "PlayerTabs";
    params?: Record<string, unknown>;
  };
  transferDraft?: {
    targetId: string;
    amount: string;
    note: string;
  };
};

const bankerId = "banker-ngozi";
const meId = "player-awa";
const malikId = "player-malik";
const inesId = "player-ines";
const yemiId = "player-yemi";
const activeSessionId = "screenshot-active-session";
const lobbySessionId = "screenshot-lobby-session";
const sessionCode = "MABLI";
const baseTime = new Date("2026-03-30T09:41:00+02:00").getTime();

function createActiveSession(): SessionSnapshot {
  return {
    id: activeSessionId,
    code: sessionCode,
    status: "active",
    createdAt: baseTime - 60 * 60 * 1000,
    bankerId,
    blackoutActive: false,
    blackoutReason: null,
    players: [
      {
        id: bankerId,
        name: "Ngozi",
        role: "banker",
        balance: 0,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 60 * 60 * 1000,
        lastActiveAt: baseTime - 2 * 60 * 1000,
      },
      {
        id: meId,
        name: "Awa",
        role: "player",
        balance: 1840,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 55 * 60 * 1000,
        lastActiveAt: baseTime - 60 * 1000,
      },
      {
        id: malikId,
        name: "Malik",
        role: "player",
        balance: 1325,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 53 * 60 * 1000,
        lastActiveAt: baseTime - 90 * 1000,
      },
      {
        id: inesId,
        name: "Ines",
        role: "player",
        balance: 960,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 51 * 60 * 1000,
        lastActiveAt: baseTime - 4 * 60 * 1000,
      },
      {
        id: yemiId,
        name: "Yemi",
        role: "player",
        balance: 780,
        connected: false,
        isDummy: true,
        joinedAt: baseTime - 49 * 60 * 1000,
        lastActiveAt: baseTime - 20 * 60 * 1000,
      },
    ],
    transactions: [
      {
        id: "tx-bank-boost",
        kind: "banker_adjust",
        fromId: null,
        toId: meId,
        amount: 1200,
        note: "Opening allowance",
        createdAt: baseTime - 46 * 60 * 1000,
        initiatedBy: "banker",
      },
      {
        id: "tx-street-food",
        kind: "transfer",
        fromId: meId,
        toId: malikId,
        amount: 90,
        note: "Street food",
        createdAt: baseTime - 16 * 60 * 1000,
        initiatedBy: "player",
      },
      {
        id: "tx-rent-share",
        kind: "transfer",
        fromId: inesId,
        toId: meId,
        amount: 240,
        note: "Rent split",
        createdAt: baseTime - 12 * 60 * 1000,
        initiatedBy: "player",
      },
      {
        id: "tx-taxi",
        kind: "transfer",
        fromId: meId,
        toId: inesId,
        amount: 45,
        note: "Taxi",
        createdAt: baseTime - 8 * 60 * 1000,
        initiatedBy: "player",
      },
      {
        id: "tx-voucher",
        kind: "banker_adjust",
        fromId: null,
        toId: meId,
        amount: 535,
        note: "District voucher",
        createdAt: baseTime - 3 * 60 * 1000,
        initiatedBy: "banker",
      },
    ],
    groups: [
      {
        id: "group-direct-malik",
        name: "Awa & Malik",
        memberIds: [meId, malikId],
        createdAt: baseTime - 20 * 60 * 1000,
        createdBy: meId,
      },
      {
        id: "group-vendors",
        name: "Market run",
        memberIds: [meId, malikId, inesId],
        createdAt: baseTime - 25 * 60 * 1000,
        createdBy: inesId,
      },
    ],
    chats: [
      {
        id: "chat-global-1",
        fromId: bankerId,
        body: "Agency is open. Keep your receipts for every transfer.",
        createdAt: baseTime - 28 * 60 * 1000,
        groupId: null,
      },
      {
        id: "chat-group-1",
        fromId: inesId,
        body: "Meeting at the riverside market in ten minutes.",
        createdAt: baseTime - 18 * 60 * 1000,
        groupId: "group-vendors",
      },
      {
        id: "chat-direct-1",
        fromId: malikId,
        body: "I covered the snacks. Send me 90 when you can.",
        createdAt: baseTime - 11 * 60 * 1000,
        groupId: "group-direct-malik",
      },
      {
        id: "chat-direct-2",
        fromId: meId,
        body: "Done. I am also paying for the taxi with Ines.",
        createdAt: baseTime - 9 * 60 * 1000,
        groupId: "group-direct-malik",
      },
      {
        id: "chat-direct-3",
        fromId: malikId,
        body: "Perfect. Meet us near the orange stand.",
        createdAt: baseTime - 6 * 60 * 1000,
        groupId: "group-direct-malik",
      },
    ],
    takeoverRequests: [],
  };
}

function createLobbySession(): SessionSnapshot {
  return {
    id: lobbySessionId,
    code: sessionCode,
    status: "lobby",
    createdAt: baseTime - 20 * 60 * 1000,
    bankerId,
    blackoutActive: false,
    blackoutReason: null,
    players: [
      {
        id: bankerId,
        name: "Ngozi",
        role: "banker",
        balance: 0,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 20 * 60 * 1000,
        lastActiveAt: baseTime - 60 * 1000,
      },
      {
        id: meId,
        name: "Awa",
        role: "player",
        balance: 1500,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 18 * 60 * 1000,
        lastActiveAt: baseTime - 90 * 1000,
      },
      {
        id: malikId,
        name: "Malik",
        role: "player",
        balance: 1500,
        connected: true,
        isDummy: false,
        joinedAt: baseTime - 15 * 60 * 1000,
        lastActiveAt: baseTime - 4 * 60 * 1000,
      },
      {
        id: yemiId,
        name: "Yemi",
        role: "player",
        balance: 1500,
        connected: false,
        isDummy: true,
        joinedAt: baseTime - 10 * 60 * 1000,
        lastActiveAt: baseTime - 10 * 60 * 1000,
      },
    ],
    transactions: [],
    groups: [],
    chats: [],
    takeoverRequests: [],
  };
}

export function normalizeScreenshotScene(value: string | null | undefined): ScreenshotScene | null {
  if (!value) return null;
  const normalized = value.trim().toLowerCase();
  if (normalized === "start") return "start";
  if (normalized === "lobby") return "lobby";
  if (normalized === "home") return "home";
  if (normalized === "transfers") return "transfers";
  if (normalized === "chat") return "chat";
  return null;
}

export function buildScreenshotFixture(scene: ScreenshotScene): ScreenshotFixture {
  if (scene === "start") {
    return {
      scene,
      sessionId: "",
      sessionCode: "",
      playerId: "",
      session: null,
      navigationTarget: { root: "EntryLanding" },
    };
  }

  if (scene === "lobby") {
    return {
      scene,
      sessionId: lobbySessionId,
      sessionCode,
      playerId: meId,
      session: createLobbySession(),
      navigationTarget: { root: "Lobby" },
    };
  }

  if (scene === "home") {
    return {
      scene,
      sessionId: activeSessionId,
      sessionCode,
      playerId: meId,
      session: createActiveSession(),
      navigationTarget: {
        root: "PlayerTabs",
        params: { screen: "PlayerHome" },
      },
    };
  }

  if (scene === "transfers") {
    return {
      scene,
      sessionId: activeSessionId,
      sessionCode,
      playerId: meId,
      session: createActiveSession(),
      navigationTarget: {
        root: "PlayerTabs",
        params: { screen: "PlayerTransfers" },
      },
      transferDraft: {
        targetId: malikId,
        amount: "90",
        note: "Street food",
      },
    };
  }

  return {
    scene,
    sessionId: activeSessionId,
    sessionCode,
    playerId: meId,
    session: createActiveSession(),
    navigationTarget: {
      root: "PlayerTabs",
      params: {
        screen: "PlayerChat",
        params: {
          screen: "ChatThread",
          params: { chatId: "group-direct-malik" },
        },
      },
    },
  };
}
