import { afterAll, beforeEach, describe, expect, it } from 'vitest'
import { presentCustomer, presentCustomerList } from './customer.presenter'

describe('customer.presenter', () => {
  const originalDashboardUrl = process.env.DASHBOARD_URL
  const originalPublicDashboardUrl = process.env.NEXT_PUBLIC_DASHBOARD_URL

  beforeEach(() => {
    process.env.DASHBOARD_URL = 'https://dashboard.example.test'
    process.env.NEXT_PUBLIC_DASHBOARD_URL = ''
  })

  afterAll(() => {
    process.env.DASHBOARD_URL = originalDashboardUrl
    process.env.NEXT_PUBLIC_DASHBOARD_URL = originalPublicDashboardUrl
  })

  it('rewrites raw customer license images to the protected dashboard endpoint', () => {
    expect(presentCustomer({
      id: 'customer_1',
      firstName: 'Aya',
      licenseImageUrl: '/storage/companies/company_1/customers/customer_1/license.jpg',
    })).toMatchObject({
      id: 'customer_1',
      firstName: 'Aya',
      licenseImageUrl: 'https://dashboard.example.test/api/v1/customers/customer_1/license-image',
    })
  })

  it('keeps missing license images null and wraps customer lists with metadata', () => {
    expect(presentCustomer({ id: 'customer_1', licenseImageUrl: null }).licenseImageUrl).toBeNull()
    expect(presentCustomerList([{ id: 'customer_1' }], { total: 1, page: 1, pageSize: 20, totalPages: 1 })).toEqual({
      data: [{ id: 'customer_1' }],
      total: 1,
      page: 1,
      pageSize: 20,
      totalPages: 1,
    })
  })
})
