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

describe('transformWorkOSMembership', () => {
  it('transforms WorkOS membership with all fields', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_123',
      userId: 'user_456',
      organizationId: 'org_789',
      role: { slug: 'admin' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-10T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result).toEqual({
      object: 'organization_membership',
      id: 'mem_123',
      userId: 'user_456',
      organizationId: 'org_789',
      role: { slug: 'admin' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-10T00:00:00Z'
    })
  })

  it('handles inactive status', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_inactive',
      userId: 'user_123',
      organizationId: 'org_456',
      role: { slug: 'member' },
      status: 'inactive',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-15T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result.status).toBe('inactive')
    expect(result.id).toBe('mem_inactive')
  })

  it('maps pending status to inactive (pending not supported in our system)', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_pending',
      userId: 'user_789',
      organizationId: 'org_123',
      role: { slug: 'guest' },
      status: 'pending',
      createdAt: '2025-01-20T00:00:00Z',
      updatedAt: '2025-01-20T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    // WorkOS 'pending' is mapped to 'inactive' in our system
    expect(result.status).toBe('inactive')
    expect(result.role.slug).toBe('guest')
  })

  it('preserves role slug exactly', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_custom_role',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'custom-billing-admin' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result.role.slug).toBe('custom-billing-admin')
  })

  it('preserves all ID fields correctly', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_abc123',
      userId: 'user_xyz789',
      organizationId: 'org_def456',
      role: { slug: 'owner' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-05T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result.id).toBe('mem_abc123')
    expect(result.userId).toBe('user_xyz789')
    expect(result.organizationId).toBe('org_def456')
  })

  it('preserves timestamps as ISO strings', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_dates',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-15T14:30:00.000Z',
      updatedAt: '2025-01-20T18:45:30.000Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result.createdAt).toBe('2025-01-15T14:30:00.000Z')
    expect(result.updatedAt).toBe('2025-01-20T18:45:30.000Z')
  })

  it('preserves object type', () => {
    const workosData: WorkOSOrganizationMembership = {
      object: 'organization_membership',
      id: 'mem_obj',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'admin' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSMembership(workosData)

    expect(result.object).toBe('organization_membership')
  })
})
