import type { WorkOSOrganizationMembership } from './workos'
import type { OrganizationMembership, MembershipStatus } from '../membership'

/**
 * Map WorkOS status to internal status.
 * WorkOS may return 'pending' but our system only uses 'active' | 'inactive'.
 * Pending memberships from WorkOS are treated as inactive until fully provisioned.
 */
function mapWorkOSStatus(workosStatus: string): MembershipStatus {
  if (workosStatus === 'active') return 'active'
  return 'inactive'
}

/**
 * Transform WorkOS membership to internal format
 */
export function transformWorkOSMembership(
  workosData: WorkOSOrganizationMembership
): OrganizationMembership {
  return {
    object: 'organization_membership',
    id: workosData.id,
    userId: workosData.userId,
    organizationId: workosData.organizationId,
    role: {
      slug: workosData.role.slug
    },
    status: mapWorkOSStatus(workosData.status),
    createdAt: workosData.createdAt,
    updatedAt: workosData.updatedAt
  }
}
