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

describe('transformWorkOSInvitation', () => {
  it('transforms pending WorkOS invitation with all fields', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_123',
      email: 'newuser@example.com',
      organizationId: 'org_456',
      inviterUserId: 'user_789',
      state: 'pending',
      token: 'token_abc123',
      acceptInvitationUrl: 'https://example.com/accept/token_abc123',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result).toEqual({
      id: 'inv_123',
      workosInvitationId: 'inv_123',
      email: 'newuser@example.com',
      organizationId: 'org_456',
      inviterUserId: 'user_789',
      invitationState: 'pending',
      roleSlug: null, // WorkOS doesn't include role in invitation response
      invitationToken: 'token_abc123',
      acceptInvitationUrl: 'https://example.com/accept/token_abc123',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    })
  })

  it('transforms accepted invitation with acceptedAt timestamp', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_accepted',
      email: 'accepted@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'accepted',
      token: 'token_accepted',
      acceptInvitationUrl: 'https://example.com/accept/token_accepted',
      acceptedAt: '2025-01-20T14:30:00Z',
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-20T14:30:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.invitationState).toBe('accepted')
    expect(result.acceptedAt).toBe('2025-01-20T14:30:00Z')
    expect(result.revokedAt).toBeNull()
  })

  it('transforms revoked invitation with revokedAt timestamp', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_revoked',
      email: 'revoked@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'revoked',
      token: 'token_revoked',
      acceptInvitationUrl: 'https://example.com/accept/token_revoked',
      acceptedAt: null,
      revokedAt: '2025-01-18T09:15:00Z',
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-18T09:15:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.invitationState).toBe('revoked')
    expect(result.acceptedAt).toBeNull()
    expect(result.revokedAt).toBe('2025-01-18T09:15:00Z')
  })

  it('transforms expired invitation', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_expired',
      email: 'expired@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'expired',
      token: 'token_expired',
      acceptInvitationUrl: 'https://example.com/accept/token_expired',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-01-01T00:00:00Z', // Past date
      createdAt: '2024-12-15T10:00:00Z',
      updatedAt: '2025-01-02T00:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.invitationState).toBe('expired')
    expect(result.expiresAt).toBe('2025-01-01T00:00:00Z')
  })

  it('sets roleSlug to null (not provided by WorkOS)', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_no_role',
      email: 'test@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'pending',
      token: 'token_123',
      acceptInvitationUrl: 'https://example.com/accept',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.roleSlug).toBeNull()
  })

  it('preserves email address exactly', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_email',
      email: 'Test.User+Tag@Example.COM',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'pending',
      token: 'token_123',
      acceptInvitationUrl: 'https://example.com/accept',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.email).toBe('Test.User+Tag@Example.COM')
  })

  it('duplicates id to workosInvitationId', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_duplicate_id',
      email: 'test@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'pending',
      token: 'token_123',
      acceptInvitationUrl: 'https://example.com/accept',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.id).toBe('inv_duplicate_id')
    expect(result.workosInvitationId).toBe('inv_duplicate_id')
  })

  it('preserves invitation token and URL', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_token_test',
      email: 'test@example.com',
      organizationId: 'org_123',
      inviterUserId: 'user_456',
      state: 'pending',
      token: 'super_secret_token_xyz',
      acceptInvitationUrl: 'https://app.example.com/invitations/accept?token=super_secret_token_xyz',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.invitationToken).toBe('super_secret_token_xyz')
    expect(result.acceptInvitationUrl).toBe('https://app.example.com/invitations/accept?token=super_secret_token_xyz')
  })

  it('handles null organizationId', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_no_org',
      email: 'test@example.com',
      organizationId: null,
      inviterUserId: 'user_456',
      state: 'pending',
      token: 'token_123',
      acceptInvitationUrl: 'https://example.com/accept',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.organizationId).toBeNull()
  })

  it('handles null inviterUserId', () => {
    const workosInvitation: WorkOSInvitation = {
      id: 'inv_no_inviter',
      email: 'test@example.com',
      organizationId: 'org_123',
      inviterUserId: null,
      state: 'pending',
      token: 'token_123',
      acceptInvitationUrl: 'https://example.com/accept',
      acceptedAt: null,
      revokedAt: null,
      expiresAt: '2025-12-31T23:59:59Z',
      createdAt: '2025-01-15T10:00:00Z',
      updatedAt: '2025-01-15T10:00:00Z'
    }

    const result = transformWorkOSInvitation(workosInvitation)

    expect(result.inviterUserId).toBeNull()
  })
})
