/**
 * Supabase Transform Tests
 * Tests transformSupabaseToMembership for pre-provisioned membership scenarios
 */

import { describe, it, expect } from 'vitest'
import { transformSupabaseToMembership } from '../supabase'
import type { SupabaseOrgMembership } from '../../../../supabase'

describe('transformSupabaseToMembership', () => {
  it('transforms membership with workos_membership_id (normal case)', () => {
    const supabaseData: SupabaseOrgMembership = {
      id: 'supabase-uuid-123',
      user_id: 'user-uuid-456',
      organization_id: 'org-uuid-789',
      workos_membership_id: 'om_workos_abc',
      membership_status: 'active',
      role_slug: 'admin',
      created_at: '2024-01-01T00:00:00Z',
      updated_at: '2024-01-01T00:00:00Z'
    }

    const result = transformSupabaseToMembership(supabaseData)

    expect(result.object).toBe('organization_membership')
    expect(result.id).toBe('om_workos_abc')  // Uses WorkOS ID when available
    expect(result.userId).toBe('user-uuid-456')  // Always Supabase user_id
    expect(result.organizationId).toBe('org-uuid-789')  // Always Supabase org_id
    expect(result.status).toBe('active')
    expect(result.role.slug).toBe('admin')
  })

  it('transforms pre-provisioned membership with NULL workos_membership_id', () => {
    const supabaseData: SupabaseOrgMembership = {
      id: 'supabase-uuid-123',
      user_id: 'user-uuid-456',
      organization_id: 'org-uuid-789',
      workos_membership_id: null,  // Pre-provisioned - not linked yet
      membership_status: 'active',
      role_slug: 'member',
      created_at: '2024-01-01T00:00:00Z',
      updated_at: '2024-01-01T00:00:00Z'
    }

    const result = transformSupabaseToMembership(supabaseData)

    expect(result.object).toBe('organization_membership')
    expect(result.id).toBe('supabase-uuid-123')  // Falls back to Supabase ID
    expect(result.userId).toBe('user-uuid-456')  // CRITICAL: Must be user_id, not null
    expect(result.organizationId).toBe('org-uuid-789')  // CRITICAL: Must be org_id, not null
    expect(result.status).toBe('active')
    expect(result.role.slug).toBe('member')
  })

  it('defaults role_slug to member when null', () => {
    const supabaseData: SupabaseOrgMembership = {
      id: 'supabase-uuid-123',
      user_id: 'user-uuid-456',
      organization_id: 'org-uuid-789',
      workos_membership_id: null,
      membership_status: 'active',
      role_slug: null,
      created_at: null,
      updated_at: null
    }

    const result = transformSupabaseToMembership(supabaseData)

    expect(result.role.slug).toBe('member')
  })

  it('maps inactive status correctly', () => {
    const supabaseData: SupabaseOrgMembership = {
      id: 'supabase-uuid-123',
      user_id: 'user-uuid-456',
      organization_id: 'org-uuid-789',
      workos_membership_id: null,
      membership_status: 'inactive',
      role_slug: 'member',
      created_at: null,
      updated_at: null
    }

    const result = transformSupabaseToMembership(supabaseData)

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