import { describe, expect, it } from 'vitest'
import { computeRequiresDraftFlow } from './DictionaryEditorDialog'

const base = {
  isEdit: true,
  initialApprovalRequired: true,
  initialMinRole: 'ordinis:reviewer' as string | null,
  currentApprovalRequired: true,
  currentMinRole: 'ordinis:reviewer' as string | null,
  schemaChangeCount: 0,
}

describe('computeRequiresDraftFlow', () => {
  it('returns false in create mode (no initial dict)', () => {
    expect(computeRequiresDraftFlow({ ...base, isEdit: false })).toBe(false)
  })

  it('returns false for non-approval-required dicts even with schema changes', () => {
    expect(
      computeRequiresDraftFlow({
        ...base,
        initialApprovalRequired: false,
        schemaChangeCount: 5,
      }),
    ).toBe(false)
  })

  it('returns false for pure metadata edits on approval-required dict', () => {
    // Editing display name / description / locales doesn't touch schema or
    // approval config; backend allows inline. UX should NOT block Save here.
    expect(computeRequiresDraftFlow(base)).toBe(false)
  })

  it('returns true when schema changed on approval-required dict', () => {
    expect(
      computeRequiresDraftFlow({ ...base, schemaChangeCount: 1 }),
    ).toBe(true)
  })

  it('returns true when approvalRequired toggled off (backdoor block)', () => {
    expect(
      computeRequiresDraftFlow({ ...base, currentApprovalRequired: false }),
    ).toBe(true)
  })

  it('returns true when approvalMinRole changed', () => {
    expect(
      computeRequiresDraftFlow({ ...base, currentMinRole: 'ordinis:admin' }),
    ).toBe(true)
  })

  it('returns true when approvalMinRole cleared', () => {
    // Edge case from MR !182 audit: clearing the min-role would let any
    // generic reviewer approve restricted-dict drafts. Must route through
    // draft workflow.
    expect(
      computeRequiresDraftFlow({ ...base, currentMinRole: null }),
    ).toBe(true)
  })

  it('handles all three clauses together (worst-case schema rewrite + governance flip)', () => {
    expect(
      computeRequiresDraftFlow({
        ...base,
        schemaChangeCount: 3,
        currentApprovalRequired: false,
        currentMinRole: null,
      }),
    ).toBe(true)
  })
})
