import { readFile, writeFile } from "node:fs/promises";
import { randomUUID } from "node:crypto";
import { join } from "node:path";
import { CommandId, ThreadId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { APP_DIR, MOBILE, MOBILE_T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise } from "./t3.ts";

const { state: t3 } = makeState(MOBILE_T3_HOME, { T3CODE_PORT: String(MOBILE.serverPort) });
async function seed() {
    const project = await t3.project("demo-project");
    const thread = await t3.thread({ project, title: "Tall question demo", messages: ["Ask four detailed questions"], model: "question" });
    const { cli } = makeState(MOBILE_T3_HOME, { T3CODE_PORT: String(MOBILE.serverPort) });
    await runPromise(cli(["thread", "send", thread.threadId, "Follow up after the answers"]));
    await runPromise(Effect.gen(function* () {
      const client = yield* rpcClient(MOBILE_T3_HOME, `http://127.0.0.1:${MOBILE.serverPort}`);
      const side = ThreadId.make(randomUUID());
      yield* client["orchestration.dispatchCommand"]({ type: "thread.fork", createdBy: "user", creationSource: "web", commandId: CommandId.make(randomUUID()), sourceThreadId: ThreadId.make(thread.threadId), targetThreadId: side, sourcePoint: { type: "latest_stable" }, title: "Side chat", sideChat: true });
      yield* client["orchestration.dispatchCommand"]({ type: "message.dispatch", createdBy: "user", creationSource: "web", commandId: CommandId.make(randomUUID()), threadId: side, messageId: randomUUID(), text: "Discuss the demo", attachments: [], dispatchMode: { type: "start_immediately" }, modelSelection: { instanceId: "devReplay", model: "multi-turn" } });
    }).pipe(Effect.scoped));
    console.log(JSON.stringify(thread));

}
await seed();
