import { beforeEach, describe, expect, it, vi } from "vitest";
import type { AgentSession } from "@mosaic/shared";
import type { PrismaService } from "../../prisma/prisma.service";
import { AgentProviderRegistry } from "../agents/agent-provider.registry";
import { MissionControlController } from "./mission-control.controller";
import { MissionControlService } from "./mission-control.service";

describe("MissionControlController", () => {
  let controller: MissionControlController;
  let registry: {
    listAllSessions: ReturnType<typeof vi.fn>;
    getProviderForSession: ReturnType<typeof vi.fn>;
  };

  beforeEach(() => {
    registry = {
      listAllSessions: vi.fn(),
      getProviderForSession: vi.fn(),
    };

    const prisma = {
      operatorAuditLog: {
        create: vi.fn().mockResolvedValue(undefined),
      },
    };

    const service = new MissionControlService(
      registry as unknown as AgentProviderRegistry,
      prisma as unknown as PrismaService
    );

    controller = new MissionControlController(service);
  });

  it("Phase 1 gate: unified sessions endpoint returns internal provider sessions", async () => {
    const internalSession: AgentSession = {
      id: "session-internal-1",
      providerId: "internal",
      providerType: "internal",
      status: "active",
      createdAt: new Date("2026-03-07T20:00:00.000Z"),
      updatedAt: new Date("2026-03-07T20:01:00.000Z"),
    };

    const externalSession: AgentSession = {
      id: "session-openclaw-1",
      providerId: "openclaw",
      providerType: "external",
      status: "active",
      createdAt: new Date("2026-03-07T20:02:00.000Z"),
      updatedAt: new Date("2026-03-07T20:03:00.000Z"),
    };

    registry.listAllSessions.mockResolvedValue([internalSession, externalSession]);

    const response = await controller.listSessions();

    expect(registry.listAllSessions).toHaveBeenCalledTimes(1);
    expect(response.sessions).toEqual([internalSession, externalSession]);
    expect(response.sessions).toContainEqual(
      expect.objectContaining({
        id: "session-internal-1",
        providerId: "internal",
      })
    );
  });
});
