import { describe, expect, it } from 'vitest'
import { presentCompanySignup, presentEmployeeSession, presentRenterProfile } from './auth.presenter'

describe('auth presenters', () => {
  it('presents employee sessions without leaking company internals or requiring a token', () => {
    const employee = {
      id: 'employee_1',
      email: 'owner@example.com',
      firstName: 'Aya',
      lastName: 'Benali',
      role: 'OWNER',
      preferredLanguage: 'fr',
      companyId: 'company_1',
      company: { name: 'Atlas Cars', slug: 'atlas-cars' },
    }

    expect(presentEmployeeSession(employee)).toEqual({
      employee: {
        id: 'employee_1',
        email: 'owner@example.com',
        firstName: 'Aya',
        lastName: 'Benali',
        role: 'OWNER',
        preferredLanguage: 'fr',
        companyId: 'company_1',
        companyName: 'Atlas Cars',
        companySlug: 'atlas-cars',
      },
    })
  })

  it('adds the token only when one is supplied', () => {
    const employee = {
      id: 'employee_1',
      email: 'owner@example.com',
      firstName: 'Aya',
      lastName: 'Benali',
      role: 'OWNER',
      preferredLanguage: null,
      companyId: 'company_1',
      company: { name: 'Atlas Cars', slug: 'atlas-cars' },
    }

    expect(presentEmployeeSession(employee, 'jwt-token')).toEqual(expect.objectContaining({
      token: 'jwt-token',
      employee: expect.objectContaining({ id: 'employee_1' }),
    }))
  })

  it('presents company signup with a deterministic next step and fallback email delivery state', () => {
    expect(presentCompanySignup({
      company: { id: 'company_1', name: 'Atlas Cars', slug: 'atlas-cars' },
      trialEndAt: new Date('2026-09-01T00:00:00.000Z'),
    })).toEqual({
      companyId: 'company_1',
      companyName: 'Atlas Cars',
      slug: 'atlas-cars',
      invitationId: null,
      trialEndsAt: '2026-09-01T00:00:00.000Z',
      nextStep: 'workspace_created',
      emailDelivery: { attempted: false, success: false, error: null },
    })
  })

  it('hydrates saved renter companies in renter save order and leaves missing brand data explicit', () => {
    const renter = {
      id: 'renter_1',
      firstName: 'Youssef',
      lastName: 'Amrani',
      email: 'youssef@example.com',
      phone: null,
      preferredLocale: 'fr',
      preferredCurrency: 'MAD',
      emailVerified: true,
      savedCompanies: [{ companyId: 'company_2' }, { companyId: 'company_missing' }, { companyId: 'company_1' }],
    }

    const result = presentRenterProfile(renter, [
      { id: 'company_1', brand: { displayName: 'Atlas', subdomain: 'atlas', logoUrl: '/atlas.png' } },
      { id: 'company_2', brand: null },
    ])

    expect(result.savedCompanies).toEqual([
      { id: 'company_2', brand: null },
      { id: 'company_missing', brand: null },
      { id: 'company_1', brand: { displayName: 'Atlas', subdomain: 'atlas', logoUrl: '/atlas.png' } },
    ])
  })
})
