import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import { makeState, rpcClient, runPromise } from "./scenarios/t3.ts";
const [home, origin] = process.argv.slice(2);
const project = await makeState(home).state.project("demo");
    await runPromise(
      Effect.gen(function* () {
        const fs = yield* FileSystem.FileSystem;
        const path = yield* Path.Path;
        const sessionDir = path.join(home, "pi-history");
        const timestamp = new Date().toISOString();
        yield* fs.makeDirectory(sessionDir, { recursive: true });
        for (let i = 0; i < 1234; i++) {
          yield* fs.writeFileString(path.join(sessionDir, `session-${i}.jsonl`), [
            { type: "session", version: 3, id: `synthetic-${i}`, timestamp, cwd: project },
            { type: "message", id: `message-${i}`, parentId: null, timestamp,
              message: { role: "assistant", provider: "anthropic", model: "claude-sonnet-4-5",
                usage: { input: 42, cost: { total: 0.02 / 1234 } } } }
          ].map(entry => JSON.stringify(entry)).join("\n") + "\n");
        }
        const client = yield* rpcClient(home, origin);
        yield* client["server.updateSettings"]({
          patch: {},
          providerInstanceMutation: {
            operation: "upsert",
            instanceId: ProviderInstanceId.make("pi"),
            instance: {
              driver: ProviderDriverKind.make("pi"),
              enabled: true,
              usageAccess: "allowed",
              config: {
                binaryPath: path.join(home, "bin", "no-model"),
                launchArgs: "--session-dir " + sessionDir.replaceAll(" ", "\\ "),
              },
              environment: [
                { name: "PI_CODING_AGENT_SESSION_DIR", value: sessionDir, sensitive: false },
              ],
            },
          },
        });
      }).pipe(Effect.scoped),
    );

