// Scratch driver for issue 44106. Not committed.
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NetService from "@t3tools/shared/Net";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { Command, Flag } from "effect/cli";
import { projectLocationFlags } from "./config.ts";
import { withThreadTools } from "./thread.ts";

const Any = Schema.Unknown;
const repro = Command.make("repro", {
  ...projectLocationFlags,
  provider: Flag.String("provider"),
  model: Flag.String("model"),
  message: Flag.String("message"),
  polls: Flag.Int("polls").pipe(Flag.withDefault(60)),
}).pipe(
  Command.withHandler((flags) =>
    withThreadTools({ ...flags, json: false }, (tools) =>
      Effect.gen(function* () {
        const projects: any = yield* tools.call("t3_project_list", {}, Any);
        const projectId = (projects.projects ?? projects)[0].projectId ?? (projects.projects ?? projects)[0].id;
        yield* Console.log(`project ${projectId}`);
        const launched: any = yield* tools.call(
          "t3_thread_launch",
          {
            projectId,
            title: "repro 44106",
            workspaceStrategy: { type: "worktree", baseRef: "origin/main", startFromOrigin: true },
            modelSelection: { instanceId: flags.provider, model: flags.model },
          },
          Any,
        );
        yield* Console.log(`${new Date().toISOString()} launch -> ${JSON.stringify(launched)}`);
        const read0: any = yield* tools.call("t3_thread_read", { threadId: launched.threadId }, Any);
        yield* Console.log(`${new Date().toISOString()} worktreePath at send = ${JSON.stringify(read0.thread?.worktreePath ?? read0.worktreePath ?? null)}`);
        const sent: any = yield* tools.call(
          "t3_thread_send",
          { threadId: launched.threadId, message: flags.message },
          Any,
        );
        yield* Console.log(`${new Date().toISOString()} send -> ${JSON.stringify(sent)}`);
        for (let i = 0; i < flags.polls; i++) {
          yield* Effect.sleep("5 seconds");
          const r: any = yield* tools.call("t3_thread_read", { threadId: launched.threadId }, Any);
          const t = r.thread ?? r;
          yield* Console.log(
            `${new Date().toISOString()} status=${t.status} worktreePath=${t.worktreePath} runs=${JSON.stringify((r.runs ?? t.runs ?? []).map((x: any) => ({ id: x.runId ?? x.id, status: x.status })))}`,
          );
        }
        const final: any = yield* tools.call("t3_thread_read", { threadId: launched.threadId }, Any);
        yield* Console.log(JSON.stringify(final, null, 1).slice(0, 6000));
      }),
    ),
  ),
);

Command.run(repro, { version: "0" }).pipe(
  Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)),
  NodeRuntime.runMain,
);
