import { describe, it, expect } from 'vitest'
import { transformMembershipToTableRow } from '../membership'
import type { MembershipWithDetails } from '../membership'

describe('transformMembershipToTableRow', () => {
  it('transforms complete membership with all user and org details', () => {
    const membership: MembershipWithDetails = {
      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',
      user: {
        id: 'user_456',
        email: 'john@example.com',
        firstName: 'John',
        lastName: 'Doe',
        profilePictureUrl: 'https://example.com/avatar.jpg'
      },
      organization: {
        id: 'org_789',
        name: 'Acme Corp',
        workos_org_id: 'org_workos_123',
        primaryDomain: 'acme.com'
      }
    }

    const result = transformMembershipToTableRow(membership)

    expect(result).toEqual({
      id: 'mem_123',
      userId: 'user_456',
      organizationId: 'org_789',
      userEmail: 'john@example.com',
      userFullName: 'John Doe',
      organizationName: 'Acme Corp',
      role: 'admin',
      status: 'active',
      statusBadge: 'active',
      joinedAt: new Date('2025-01-01T00:00:00Z'),
      updatedAt: new Date('2025-01-10T00:00:00Z'),
      canEdit: true,
      canRemove: true
    })
  })

  it('handles missing user details with fallbacks', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_no_user',
      userId: 'user_unknown',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.userEmail).toBe('Unknown')
    expect(result.userFullName).toBe('Unknown User')
  })

  it('uses email as fallback when firstName or lastName missing', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_partial',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      user: {
        id: 'user_123',
        email: 'jane@example.com',
        firstName: 'Jane'
        // lastName missing
      }
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.userFullName).toBe('jane@example.com')
  })

  it('handles missing organization details', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_no_org',
      userId: 'user_123',
      organizationId: 'org_unknown',
      role: { slug: 'admin' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      user: {
        id: 'user_123',
        email: 'test@example.com'
      }
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.organizationName).toBe('Unknown Organization')
  })

  it('converts date strings to Date objects', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_dates',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-15T10:30:00Z',
      updatedAt: '2025-01-20T15:45:00Z'
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.joinedAt).toBeInstanceOf(Date)
    expect(result.updatedAt).toBeInstanceOf(Date)
    expect(result.joinedAt.toISOString()).toBe('2025-01-15T10:30:00.000Z')
    expect(result.updatedAt.toISOString()).toBe('2025-01-20T15:45:00.000Z')
  })

  it('sets canEdit to true only for active memberships', () => {
    const activeMembership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_active',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const inactiveMembership: MembershipWithDetails = {
      ...activeMembership,
      id: 'mem_inactive',
      status: 'inactive'
    }

    expect(transformMembershipToTableRow(activeMembership).canEdit).toBe(true)
    expect(transformMembershipToTableRow(inactiveMembership).canEdit).toBe(false)
  })

  it('always sets canRemove to true', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_remove',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'admin' },
      status: 'inactive',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.canRemove).toBe(true)
  })

  it('preserves role slug exactly', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_role',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'custom-role-name' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.role).toBe('custom-role-name')
  })

  it('sets statusBadge same as status', () => {
    const activeM: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_1',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const inactiveM: MembershipWithDetails = { ...activeM, id: 'mem_2', status: 'inactive' }

    expect(transformMembershipToTableRow(activeM).statusBadge).toBe('active')
    expect(transformMembershipToTableRow(inactiveM).statusBadge).toBe('inactive')
  })

  it('handles user with only email (no names)', () => {
    const membership: MembershipWithDetails = {
      object: 'organization_membership',
      id: 'mem_email_only',
      userId: 'user_123',
      organizationId: 'org_123',
      role: { slug: 'member' },
      status: 'active',
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z',
      user: {
        id: 'user_123',
        email: 'emailonly@example.com'
      }
    }

    const result = transformMembershipToTableRow(membership)

    expect(result.userEmail).toBe('emailonly@example.com')
    expect(result.userFullName).toBe('emailonly@example.com')
  })
})
