import { describe, it, expect } from 'vitest'
import { transformWorkOSOrganization } from '../server/transforms'
import type { WorkOSOrganization } from '../server/workos'

describe('transformWorkOSOrganization', () => {
  it('transforms WorkOS organization with verified domains', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_123',
      name: 'Acme Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-02T00:00:00Z',
      stripeCustomerId: 'cus_123',
      externalId: 'ext_123',
      metadata: { foo: 'bar' },
      domains: [
        {
          id: 'domain_1',
          domain: 'acme.com',
          state: 'verified',
          verificationStrategy: 'dns',
          verificationToken: 'token1'
        },
        {
          id: 'domain_2',
          domain: 'acme.io',
          state: 'verified',
          verificationStrategy: 'dns',
          verificationToken: 'token2'
        }
      ]
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result).toEqual({
      workosId: 'org_123',
      name: 'Acme Corp',
      domains: ['acme.com', 'acme.io'],
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-02T00:00:00Z',
      stripeCustomerId: 'cus_123',
      externalId: 'ext_123',
      workosMetadata: { foo: 'bar' },
      primaryDomain: 'acme.com',
      verifiedDomains: ['acme.com', 'acme.io'],
      pendingDomains: []
    })
  })

  it('transforms organization with pending domains', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_456',
      name: 'Beta Inc',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: [
        {
          id: 'domain_1',
          domain: 'beta.com',
          state: 'pending',
          verificationStrategy: 'dns',
          verificationToken: 'token1'
        }
      ]
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.verifiedDomains).toEqual([])
    expect(result.pendingDomains).toEqual(['beta.com'])
    expect(result.primaryDomain).toBe('beta.com') // Falls back to first domain
  })

  it('handles legacy_verified domain state', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_789',
      name: 'Legacy Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: [
        {
          id: 'domain_1',
          domain: 'legacy.com',
          state: 'legacy_verified',
          verificationStrategy: 'dns',
          verificationToken: 'token1'
        }
      ]
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.verifiedDomains).toEqual(['legacy.com'])
    expect(result.primaryDomain).toBe('legacy.com')
  })

  it('handles mixed verified and pending domains', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_mixed',
      name: 'Mixed Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: [
        {
          id: 'domain_1',
          domain: 'verified.com',
          state: 'verified',
          verificationStrategy: 'dns',
          verificationToken: 'token1'
        },
        {
          id: 'domain_2',
          domain: 'pending.com',
          state: 'pending',
          verificationStrategy: 'dns',
          verificationToken: 'token2'
        },
        {
          id: 'domain_3',
          domain: 'legacy.com',
          state: 'legacy_verified',
          verificationStrategy: 'dns',
          verificationToken: 'token3'
        }
      ]
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.domains).toEqual(['verified.com', 'pending.com', 'legacy.com'])
    expect(result.verifiedDomains).toEqual(['verified.com', 'legacy.com'])
    expect(result.pendingDomains).toEqual(['pending.com'])
    expect(result.primaryDomain).toBe('verified.com') // First verified domain
  })

  it('handles organization with no domains', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_empty',
      name: 'Empty Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: []
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.domains).toEqual([])
    expect(result.verifiedDomains).toEqual([])
    expect(result.pendingDomains).toEqual([])
    expect(result.primaryDomain).toBeUndefined()
  })

  it('preserves all WorkOS metadata', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_meta',
      name: 'Metadata Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: 'cus_stripe',
      externalId: 'external_id_123',
      metadata: {
        customField1: 'value1',
        customField2: 'value2',
        tier: 'enterprise'
      },
      domains: []
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.workosMetadata).toEqual({
      customField1: 'value1',
      customField2: 'value2',
      tier: 'enterprise'
    })
    expect(result.stripeCustomerId).toBe('cus_stripe')
    expect(result.externalId).toBe('external_id_123')
  })

  it('handles null externalId correctly', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_null',
      name: 'Null External',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: []
    }

    const result = transformWorkOSOrganization(workosOrg)

    expect(result.externalId).toBeNull()
  })

  it('prioritizes verified domains over pending for primaryDomain', () => {
    const workosOrg: WorkOSOrganization = {
      id: 'org_priority',
      name: 'Priority Corp',
      allowProfilesOutsideOrganization: false,
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      stripeCustomerId: undefined,
      externalId: null,
      metadata: {},
      domains: [
        {
          id: 'domain_1',
          domain: 'pending-first.com',
          state: 'pending',
          verificationStrategy: 'dns',
          verificationToken: 'token1'
        },
        {
          id: 'domain_2',
          domain: 'verified-second.com',
          state: 'verified',
          verificationStrategy: 'dns',
          verificationToken: 'token2'
        }
      ]
    }

    const result = transformWorkOSOrganization(workosOrg)

    // Should use verified domain as primary, not the first domain in array
    expect(result.primaryDomain).toBe('verified-second.com')
  })
})
