import { describe, expect, test } from "bun:test"
import { WoozlitPlugin } from "../../src/plugin/woozlit"
import type { PluginInput } from "@woozlit/plugin"

const fakeInput = {
  client: {
    auth: {
      get: async () => ({
        data: {
          auth: {
            type: "oauth",
            access: "mock-access-token",
            refresh: "mock-refresh-token",
            expires: Date.now() + 1000 * 60 * 60,
            metadata: {
              email: "test@woozlit.com",
              displayName: "Test User"
            }
          }
        }
      })
    }
  },
  project: {},
  worktree: "",
  directory: "",
  experimental_workspace: { register() {} },
  serverUrl: new URL("http://localhost:4096"),
  $: undefined,
} as unknown as PluginInput

describe("WoozlitPlugin", () => {
  describe("config hook", () => {
    test("registers woozlit provider with correct name and models", async () => {
      const hooks = await WoozlitPlugin(fakeInput)
      const cfg: any = {}
      await hooks.config!(cfg)
      expect(cfg.provider.woozlit.name).toBe("Woozlit")
      expect(cfg.provider.woozlit.models.panko).toBeDefined()
    })
  })

  describe("auth loader", () => {
    test("loads credentials and populates user info metadata", async () => {
      const hooks = await WoozlitPlugin(fakeInput)
      expect(hooks.auth).toBeDefined()
      expect(hooks.auth!.provider).toBe("woozlit")

      const loaded = await hooks.auth!.loader!(
        async () => ({
          type: "oauth",
          access: "mock-access-token",
          refresh: "mock-refresh-token",
          expires: Date.now() + 1000 * 60 * 60,
          metadata: {
            email: "test@woozlit.com",
            displayName: "Test User"
          }
        }),
        {} as any
      )

      expect(loaded.apiKey).toBe("mock-access-token")
      expect(loaded.email).toBe("test@woozlit.com")
      expect(loaded.displayName).toBe("Test User")
    })
  })
})
