import { describe, expect, test } from "bun:test"

describe("Auto mode flag", () => {
  test("auto mode should create session with allow-all permissions except questions", () => {
    // When --auto flag is set, the session should be created with:
    // 1. Wildcard allow rule for all permissions
    // 2. Explicit deny rule for questions (to prevent user interaction)

    const autoPermissions = [
      {
        permission: "*",
        action: "allow" as const,
        pattern: "*",
      },
      {
        permission: "question",
        action: "deny" as const,
        pattern: "*",
      },
    ]

    expect(autoPermissions).toHaveLength(2)

    // First rule: allow all
    expect(autoPermissions[0].permission).toBe("*")
    expect(autoPermissions[0].action).toBe("allow")
    expect(autoPermissions[0].pattern).toBe("*")

    // Second rule: deny questions (comes after wildcard to override it)
    expect(autoPermissions[1].permission).toBe("question")
    expect(autoPermissions[1].action).toBe("deny")
    expect(autoPermissions[1].pattern).toBe("*")
  })

  test("non-auto mode should not set allow-all permissions", () => {
    // When --auto flag is NOT set, permissions should be undefined or default
    const normalPermissions = undefined

    expect(normalPermissions).toBeUndefined()
  })

  test("permission evaluation order matters (findLast behavior)", () => {
    // The permission system uses findLast, so the last matching rule wins
    // This test verifies that the question deny rule comes AFTER the wildcard

    const autoPermissions = [
      { permission: "*", action: "allow" as const, pattern: "*" },
      { permission: "question", action: "deny" as const, pattern: "*" },
    ]

    // Simulate findLast behavior
    const findLastMatch = (permission: string) => {
      for (let i = autoPermissions.length - 1; i >= 0; i--) {
        if (permission === autoPermissions[i].permission || autoPermissions[i].permission === "*") {
          return autoPermissions[i]
        }
      }
      return null
    }

    // Test that "question" permission resolves to "deny"
    const questionRule = findLastMatch("question")
    expect(questionRule?.action).toBe("deny")

    // Test that other permissions resolve to "allow"
    const bashRule = findLastMatch("bash")
    expect(bashRule?.action).toBe("allow")

    const editRule = findLastMatch("edit")
    expect(editRule?.action).toBe("allow")

    const externalDirRule = findLastMatch("external_directory")
    expect(externalDirRule?.action).toBe("allow")
  })
})
