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

describe('transformWorkOSUser', () => {
  it('transforms complete WorkOS user with all fields', () => {
    const workosUser: WorkOSUser = {
      id: 'user_123',
      email: 'john@example.com',
      firstName: 'John',
      lastName: 'Doe',
      emailVerified: true,
      profilePictureUrl: 'https://example.com/avatar.jpg',
      lastSignInAt: '2025-01-15T10:30:00Z',
      externalId: 'ext_user_123',
      metadata: {
        department: 'Engineering',
        role: 'Senior Developer'
      },
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-10T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result).toEqual({
      id: 'user_123',
      email: 'john@example.com',
      firstName: 'John',
      lastName: 'Doe',
      emailVerified: true,
      profilePictureUrl: 'https://example.com/avatar.jpg',
      lastSignInAt: new Date('2025-01-15T10:30:00Z'),
      externalId: 'ext_user_123',
      metadata: {
        department: 'Engineering',
        role: 'Senior Developer'
      },
      createdAt: new Date('2025-01-01T00:00:00Z'),
      updatedAt: new Date('2025-01-10T00:00:00Z')
    })
  })

  it('handles null optional fields correctly', () => {
    const workosUser: WorkOSUser = {
      id: 'user_456',
      email: 'jane@example.com',
      firstName: null,
      lastName: null,
      emailVerified: false,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: {},
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.firstName).toBeNull()
    expect(result.lastName).toBeNull()
    expect(result.profilePictureUrl).toBeNull()
    expect(result.lastSignInAt).toBeUndefined()
    expect(result.externalId).toBeNull()
    expect(result.metadata).toEqual({})
  })

  it('converts date strings to Date objects', () => {
    const workosUser: WorkOSUser = {
      id: 'user_date',
      email: 'date@example.com',
      firstName: 'Date',
      lastName: 'Tester',
      emailVerified: true,
      profilePictureUrl: null,
      lastSignInAt: '2025-01-20T15:45:30Z',
      externalId: null,
      metadata: {},
      createdAt: '2025-01-01T08:00:00Z',
      updatedAt: '2025-01-21T12:30:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.createdAt).toBeInstanceOf(Date)
    expect(result.updatedAt).toBeInstanceOf(Date)
    expect(result.lastSignInAt).toBeInstanceOf(Date)
    expect(result.createdAt.toISOString()).toBe('2025-01-01T08:00:00.000Z')
    expect(result.updatedAt.toISOString()).toBe('2025-01-21T12:30:00.000Z')
    expect(result.lastSignInAt!.toISOString()).toBe('2025-01-20T15:45:30.000Z')
  })

  it('handles undefined lastSignInAt', () => {
    const workosUser: WorkOSUser = {
      id: 'user_no_signin',
      email: 'new@example.com',
      firstName: 'New',
      lastName: 'User',
      emailVerified: false,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: {},
      createdAt: '2025-01-22T00:00:00Z',
      updatedAt: '2025-01-22T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.lastSignInAt).toBeUndefined()
  })

  it('handles undefined metadata by using empty object', () => {
    const workosUser: WorkOSUser = {
      id: 'user_no_meta',
      email: 'nometa@example.com',
      firstName: 'No',
      lastName: 'Meta',
      emailVerified: true,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: undefined as unknown, // Simulating missing metadata
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.metadata).toEqual({})
  })

  it('preserves complex metadata structures', () => {
    const workosUser: WorkOSUser = {
      id: 'user_complex',
      email: 'complex@example.com',
      firstName: 'Complex',
      lastName: 'User',
      emailVerified: true,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: {
        preferences: {
          theme: 'dark',
          notifications: true
        },
        permissions: ['read', 'write'],
        lastActivity: '2025-01-20T00:00:00Z'
      },
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.metadata).toEqual({
      preferences: {
        theme: 'dark',
        notifications: true
      },
      permissions: ['read', 'write'],
      lastActivity: '2025-01-20T00:00:00Z'
    })
  })

  it('handles unverified email correctly', () => {
    const workosUser: WorkOSUser = {
      id: 'user_unverified',
      email: 'unverified@example.com',
      firstName: 'Unverified',
      lastName: 'Email',
      emailVerified: false,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: {},
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    expect(result.emailVerified).toBe(false)
  })

  it('preserves exact email address', () => {
    const workosUser: WorkOSUser = {
      id: 'user_email',
      email: 'Test.User+tag@Example.COM',
      firstName: 'Test',
      lastName: 'User',
      emailVerified: true,
      profilePictureUrl: null,
      lastSignInAt: undefined,
      externalId: null,
      metadata: {},
      createdAt: '2025-01-01T00:00:00Z',
      updatedAt: '2025-01-01T00:00:00Z'
    }

    const result = transformWorkOSUser(workosUser)

    // Should preserve exact casing and format
    expect(result.email).toBe('Test.User+tag@Example.COM')
  })
})
