import { NodeFileSystem } from "@effect/platform-node"
import { FetchHttpClient } from "effect/unstable/http"
import { describe, expect, test } from "bun:test"
import { afterEach, beforeAll, afterAll } from "bun:test"
import { Effect, Layer } from "effect"
import { Agent as AgentSvc } from "../../src/agent/agent"
import { Bus } from "../../src/bus"
import { Command } from "../../src/command"
import { Config } from "../../src/config"
import { LSP } from "../../src/lsp"
import { MCP } from "../../src/mcp"
import { Permission } from "../../src/permission"
import { Plugin } from "../../src/plugin"
import { Provider as ProviderSvc } from "../../src/provider"
import { Env } from "../../src/env"
import { ModelID, ProviderID } from "../../src/provider/schema"
import { Question } from "../../src/question"
import { Todo } from "../../src/session/todo"
import { Session } from "../../src/session"
import { LLM } from "../../src/session/llm"
import { AppFileSystem } from "@woozlit/shared/filesystem"
import { SessionPrune } from "../../src/session/prune"
import { SessionSummary } from "../../src/session/summary"
import { Instruction } from "../../src/session/instruction"
import { SessionProcessor } from "../../src/session/processor"
import { SessionPrompt } from "../../src/session/prompt"
import { SessionRevert } from "../../src/session/revert"
import { SessionRunState } from "../../src/session/run-state"
import { SessionStatus } from "../../src/session/status"
import { Skill } from "../../src/skill"
import { SystemPrompt } from "../../src/session/system"
import { Snapshot } from "../../src/snapshot"
import { ToolRegistry } from "../../src/tool"
import { Truncate } from "../../src/tool"
import { ActorRegistry } from "../../src/actor/registry"
import { ActorWaiter } from "../../src/actor/waiter"
import { Actor } from "../../src/actor/spawn"
import { Memory } from "../../src/memory"
import { History } from "../../src/history"
import { Team } from "../../src/team"
import { SessionCheckpoint } from "../../src/session/checkpoint"
import { SessionCompaction } from "../../src/session/compaction"
import { Goal } from "../../src/session/goal"
import { TaskGateState } from "../../src/task/gate-state"
import { TaskRegistry } from "../../src/task/registry"
import { Auth } from "../../src/auth"
import { Instance } from "../../src/project/instance"
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
import { Ripgrep } from "../../src/file/ripgrep"
import { Format } from "../../src/format"
import { provideTmpdirServer } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
import { reply, TestLLMServer } from "../lib/llm-server"
import { Inbox } from "../../src/inbox"
import { Worktree } from "../../src/worktree"
import { WorkflowRuntime } from "../../src/workflow/runtime"
import { WorkflowTool } from "../../src/tool/workflow"
import { parameters as workflowParameters } from "../../src/tool/workflow"
import { Tool } from "../../src/tool"
import { MessageV2 } from "../../src/session/message-v2"
import { Flag } from "../../src/flag/flag"

afterEach(async () => {
  await Instance.disposeAll()
})

const summary = Layer.succeed(
  SessionSummary.Service,
  SessionSummary.Service.of({
    summarize: () => Effect.void,
    diff: () => Effect.succeed([]),
    computeDiff: () => Effect.succeed([]),
  }),
)

const mcp = Layer.succeed(
  MCP.Service,
  MCP.Service.of({
    status: () => Effect.succeed({}),
    clients: () => Effect.succeed({}),
    tools: () => Effect.succeed({}),
    prompts: () => Effect.succeed({}),
    resources: () => Effect.succeed({}),
    add: () => Effect.succeed({ status: { status: "disabled" as const } }),
    connect: () => Effect.void,
    disconnect: () => Effect.void,
    getPrompt: () => Effect.succeed(undefined),
    readResource: () => Effect.succeed(undefined),
    startAuth: () => Effect.die("unexpected MCP auth in workflow tests"),
    authenticate: () => Effect.die("unexpected MCP auth in workflow tests"),
    finishAuth: () => Effect.die("unexpected MCP auth in workflow tests"),
    removeAuth: () => Effect.void,
    supportsOAuth: () => Effect.succeed(false),
    hasStoredTokens: () => Effect.succeed(false),
    getAuthStatus: () => Effect.succeed("not_authenticated" as const),
  }),
)

const lsp = Layer.succeed(
  LSP.Service,
  LSP.Service.of({
    init: () => Effect.void,
    status: () => Effect.succeed([]),
    hasClients: () => Effect.succeed(false),
    touchFile: () => Effect.void,
    diagnostics: () => Effect.succeed({}),
    hover: () => Effect.succeed(undefined),
    definition: () => Effect.succeed([]),
    references: () => Effect.succeed([]),
    implementation: () => Effect.succeed([]),
    documentSymbol: () => Effect.succeed([]),
    workspaceSymbol: () => Effect.succeed([]),
    prepareCallHierarchy: () => Effect.succeed([]),
    incomingCalls: () => Effect.succeed([]),
    outgoingCalls: () => Effect.succeed([]),
  }),
)

const status = SessionStatus.layer.pipe(Layer.provideMerge(Bus.layer))
const run = SessionRunState.layer.pipe(Layer.provide(status))
const infra = Layer.mergeAll(NodeFileSystem.layer, CrossSpawnSpawner.defaultLayer)

function makeLayer() {
  const deps = Layer.mergeAll(
    Session.defaultLayer,
    Snapshot.defaultLayer,
    LLM.defaultLayer,
    Env.defaultLayer,
    AgentSvc.defaultLayer,
    Command.defaultLayer,
    Permission.defaultLayer,
    Plugin.defaultLayer,
    Config.defaultLayer,
    ProviderSvc.defaultLayer,
    lsp,
    mcp,
    AppFileSystem.defaultLayer,
    status,
  ).pipe(Layer.provideMerge(infra))
  const question = Question.layer.pipe(Layer.provideMerge(deps))
  const todo = Todo.layer.pipe(Layer.provideMerge(deps))
  const checkpoint = SessionCheckpoint.defaultLayer
  const taskRegistry = ActorRegistry.defaultLayer
  const taskWaiter = ActorWaiter.defaultLayer
  const team = Team.defaultLayer
  const registry = ToolRegistry.layer.pipe(
    Layer.provide(Skill.defaultLayer),
    Layer.provide(FetchHttpClient.layer),
    Layer.provide(CrossSpawnSpawner.defaultLayer),
    Layer.provide(Ripgrep.defaultLayer),
    Layer.provide(Format.defaultLayer),
    Layer.provide(taskRegistry),
    Layer.provide(taskWaiter),
    Layer.provide(team),
    Layer.provide(checkpoint),
    Layer.provide(Memory.defaultLayer),
    Layer.provide(History.defaultLayer),
    Layer.provide(TaskRegistry.defaultLayer),
    Layer.provide(Auth.defaultLayer),
    Layer.provideMerge(todo),
    Layer.provideMerge(question),
    Layer.provideMerge(deps),
  )
  const trunc = Truncate.layer.pipe(Layer.provideMerge(deps))
  const proc = SessionProcessor.layer.pipe(Layer.provide(summary), Layer.provideMerge(deps))
  const prune = SessionPrune.layer.pipe(Layer.provide(checkpoint), Layer.provideMerge(deps))
  const prompt = SessionPrompt.layer.pipe(
    Layer.provide(Goal.defaultLayer),
    Layer.provide(TaskGateState.defaultLayer),
    Layer.provide(SessionRevert.defaultLayer),
    Layer.provide(summary),
    Layer.provide(checkpoint),
    Layer.provide(SessionCompaction.defaultLayer),
    Layer.provide(team),
    Layer.provide(taskRegistry),
    Layer.provideMerge(run),
    Layer.provideMerge(prune),
    Layer.provideMerge(proc),
    Layer.provideMerge(registry),
    Layer.provideMerge(trunc),
    Layer.provide(Instruction.defaultLayer),
    Layer.provide(SystemPrompt.defaultLayer),
    Layer.provide(Inbox.defaultLayer),
    Layer.provideMerge(deps),
  )
  const actor = Actor.layer.pipe(
    Layer.provideMerge(prompt),
    Layer.provideMerge(taskRegistry),
    // dev's Actor.layer now resolves TaskRegistry.Service (spawn.ts) — provide it
    // here too, matching test/actor/spawn.test.ts.
    Layer.provide(TaskRegistry.defaultLayer),
    // provideMerge (not provide) so Inbox.Service stays in the output context for
    // WorkflowRuntime.layer, which now resolves Inbox to notify the parent on
    // terminal. Mirrors prod WorkflowRuntime.defaultLayer providing Inbox.
    Layer.provideMerge(Inbox.defaultLayer),
  )
  return Layer.mergeAll(
    TestLLMServer.layer,
    Truncate.layer.pipe(Layer.provideMerge(deps)),
    AgentSvc.defaultLayer.pipe(Layer.provideMerge(deps)),
    WorkflowRuntime.layer.pipe(
      Layer.provideMerge(actor),
      Layer.provide(Worktree.defaultLayer),
    ),
  ).pipe(Layer.provide(summary))
}

const it = testEffect(makeLayer())

const ref = {
  providerID: ProviderID.make("test"),
  modelID: ModelID.make("test-model"),
}

const cfg = {
  provider: {
    test: {
      name: "Test",
      id: "test",
      env: [],
      npm: "@ai-sdk/openai-compatible",
      models: {
        "test-model": {
          id: "test-model",
          name: "Test Model",
          attachment: false,
          reasoning: false,
          temperature: false,
          tool_call: true,
          release_date: "2025-01-01",
          limit: { context: 100000, output: 10000 },
          cost: { input: 0, output: 0 },
          options: {},
        },
      },
      options: {
        apiKey: "test-key",
        baseURL: "http://localhost:1/v1",
      },
    },
  },
}

function providerCfg(url: string) {
  return {
    ...cfg,
    provider: {
      ...cfg.provider,
      test: {
        ...cfg.provider.test,
        options: {
          ...cfg.provider.test.options,
          baseURL: url,
        },
      },
    },
  }
}

describe("WorkflowTool run", () => {
  // The workflow tool is flag-gated (src/tool/registry.ts) — it only appears in
  // the agent's tool list when this is true. The agent-loop test below needs it.
  const originalFlag = Flag.WOOZLIT_CODE_EXPERIMENTAL_WORKFLOW_TOOL
  beforeAll(() => {
    Flag.WOOZLIT_CODE_EXPERIMENTAL_WORKFLOW_TOOL = true
  })
  afterAll(() => {
    Flag.WOOZLIT_CODE_EXPERIMENTAL_WORKFLOW_TOOL = originalFlag
  })

  it.live("run returns a run_id immediately, then completes in the background", () =>
    provideTmpdirServer(
      Effect.fnUntraced(function* ({ llm }) {
        const def = yield* Tool.init(yield* WorkflowTool)
        const session = yield* Session.Service
        const parent = yield* session.create({
          title: "wf tool",
          permission: [{ permission: "*", pattern: "*", action: "allow" }],
        })
        yield* llm.text("done")
        const script = [
          `export const meta = { name: "t", description: "d" }`,
          `return await agent("hello")`,
        ].join("\n")
        const ctx = {
          sessionID: parent.id,
          messageID: "msg_test",
          agent: "main",
          abort: new AbortController().signal,
          messages: [],
          metadata: () => Effect.void,
          ask: () => Effect.void,
        }
        const res = yield* def.execute({ operation: "run", script }, ctx as any)
        expect(res.metadata.runID).toBeDefined()
        expect(res.output).toContain("run_id")
      }),
      { git: true, config: providerCfg },
    ),
  )

  it.live("run by name resolves a built-in workflow and starts it", () =>
    provideTmpdirServer(
      Effect.fnUntraced(function* ({ llm }) {
        const def = yield* Tool.init(yield* WorkflowTool)
        const session = yield* Session.Service
        const parent = yield* session.create({
          title: "wf by name",
          permission: [{ permission: "*", pattern: "*", action: "allow" }],
        })
        yield* llm.text("done")
        const ctx = {
          sessionID: parent.id,
          messageID: "msg_test",
          agent: "main",
          abort: new AbortController().signal,
          messages: [],
          metadata: () => Effect.void,
          ask: () => Effect.void,
        }
        const res = yield* def.execute({ operation: "run", name: "deep-research", args: "Q?" }, ctx as any)
        expect(res.metadata.runID).toBeDefined()
        expect(res.output).toContain("run_id")
      }),
      { git: true, config: providerCfg },
    ),
  )

  it.live("run rejects when BOTH name and script are given", () =>
    provideTmpdirServer(
      Effect.fnUntraced(function* ({ llm }) {
        const def = yield* Tool.init(yield* WorkflowTool)
        const session = yield* Session.Service
        const parent = yield* session.create({
          title: "wf both",
          permission: [{ permission: "*", pattern: "*", action: "allow" }],
        })
        yield* llm.text("done")
        const ctx = {
          sessionID: parent.id,
          messageID: "msg_test",
          agent: "main",
          abort: new AbortController().signal,
          messages: [],
          metadata: () => Effect.void,
          ask: () => Effect.void,
        }
        // Handler enforces the xor (schema keeps both optional); execute() orDies
        // the Effect.fail into a defect, so assert via exit that it did NOT succeed.
        const exit = yield* Effect.exit(
          def.execute(
            { operation: "run", name: "deep-research", script: "export const meta = { name: 'x', description: 'd' }" },
            ctx as any,
          ),
        )
        expect(exit._tag).toBe("Failure")
      }),
      { git: true, config: providerCfg },
    ),
  )

  // The hop all prior tests skipped: a real agent turn — model emits a `workflow`
  // tool-call → ToolRegistry dispatches it → WorkflowRuntime runs it. Earlier tests
  // either drove the runtime directly or called execute() by hand; none proved the
  // agent loop actually invokes the tool.
  it.live("an agent given the workflow tool calls it through the loop and the runtime runs it", () =>
    provideTmpdirServer(
      Effect.fnUntraced(function* ({ llm }) {
        const prompt = yield* SessionPrompt.Service
        const sessions = yield* Session.Service
        const session = yield* sessions.create({
          title: "wf via agent loop",
          permission: [{ permission: "*", pattern: "*", action: "allow" }],
        })
        // Seed the user turn.
        yield* prompt.prompt({
          sessionID: session.id,
          agent: "build",
          noReply: true,
          parts: [{ type: "text", text: "run a workflow that fans out one agent" }],
        })
        const script = [`export const meta = { name: "t", description: "d" }`, `return await agent("hi")`].join("\n")
        // Turn 1: the model emits a workflow tool-call.
        yield* llm.push(reply().tool("workflow", { operation: "run", script }))
        // The workflow's inner agent("hi") spawn needs its own reply.
        yield* llm.text("done")
        // Turn 2: the model's final text reply after the tool result.
        yield* llm.text("workflow started")

        const result = yield* prompt.loop({ sessionID: session.id })
        expect(result.info.role).toBe("assistant")

        // PROOF 1: the agent actually invoked the `workflow` tool — a completed tool
        // part named "workflow" exists in the conversation.
        type CompletedToolPart = MessageV2.ToolPart & { state: MessageV2.ToolStateCompleted }
        const msgs = yield* MessageV2.filterCompactedEffect(session.id)
        const toolPart = msgs
          .flatMap((m) => m.parts)
          .find(
            (p): p is CompletedToolPart =>
              p.type === "tool" && p.tool === "workflow" && p.state.status === "completed",
          )
        expect(toolPart).toBeDefined()
        expect(toolPart?.state.output).toContain("run_id")

        // PROOF 2: the tool actually started a run in the runtime — a persisted run
        // record exists for this session.
        const runtime = yield* WorkflowRuntime.Service
        const runs = yield* runtime.list({ sessionID: session.id })
        expect(runs.length).toBeGreaterThanOrEqual(1)
      }),
      { git: true, config: providerCfg },
    ),
    // Real agent loop + an inner workflow spawn through the full stack legitimately
    // exceeds the 5s default on a cold provider/server warmup (it's order-dependent
    // otherwise — green only when a prior test warms the layer). Give it headroom.
    30000,
  )

  it.live("run op accepts an explicit workspace and the script's file ops are jailed to it", () =>
    provideTmpdirServer(
      Effect.fnUntraced(function* ({ dir }) {
        const def = yield* Tool.init(yield* WorkflowTool)
        const session = yield* Session.Service
        const parent = yield* session.create({
          title: "wf tool ws",
          permission: [{ permission: "*", pattern: "*", action: "allow" }],
        })
        const { mkdirSync } = yield* Effect.promise(() => import("fs"))
        const path = yield* Effect.promise(() => import("path"))
        const sub = path.join(dir, "scratch")
        mkdirSync(sub, { recursive: true })
        // The script writes a file then checks it exists — all within the workspace.
        const script = [
          `export const meta = { name: "t", description: "d" }`,
          `await writeFile("out.txt", "hi")`,
          `return await exists("out.txt")`,
        ].join("\n")
        const ctx = {
          sessionID: parent.id,
          messageID: "msg_test",
          agent: "main",
          abort: new AbortController().signal,
          messages: [],
          metadata: () => Effect.void,
          ask: () => Effect.void,
        }
        const res = yield* def.execute({ operation: "run", script, workspace: sub }, ctx as any)
        expect(res.metadata.runID).toBeDefined()
        // Wait for completion + assert the file landed under the chosen workspace (sub), not the worktree root.
        const runtime = yield* WorkflowRuntime.Service
        const outcome = yield* runtime.wait({ runID: res.metadata.runID as string })
        expect(outcome.status).toBe("completed")
        expect((outcome as { result: boolean }).result).toBe(true)
        const wrote = yield* Effect.promise(() => Bun.file(path.join(sub, "out.txt")).text())
        expect(wrote).toBe("hi")
      }),
      { git: true, config: providerCfg },
    ),
  )
})

describe("WorkflowTool parameters schema", () => {
  test("run schema accepts name and accepts script", () => {
    // Option A (handler-enforced xor): both `name` and `script` are optional at
    // the zod level, so each single-field run variant parses on its own. The
    // run handler rejects the neither-case at runtime (Effect.fail listing known
    // built-ins), which Effect.orDie surfaces to the model as a defect — so the
    // empty-run case is intentionally NOT asserted here.
    expect(workflowParameters.safeParse({ operation: "run", name: "deep-research" }).success).toBe(true)
    expect(workflowParameters.safeParse({ operation: "run", script: "export const meta = {}" }).success).toBe(true)
  })
})
