import { describe, expect, it } from 'vitest'
import { companySignupSchema } from './auth.company.schemas'
import { employeeForgotPasswordSchema, employeeLanguageSchema, employeeLoginSchema, employeeResetPasswordSchema } from './auth.employee.schemas'
import { renterFcmTokenSchema, renterUpdateSchema } from './auth.renter.schemas'

const validCompanySignup = {
  firstName: 'Aya',
  lastName: 'Haddad',
  email: 'owner@example.test',
  password: 'securePass123',
  companyName: 'Atlas Cars',
  legalName: 'Atlas Cars SARL',
  legalForm: 'SARL',
  registrationNumber: 'RC-1',
  iceNumber: 'ICE-1',
  taxId: 'TAX-1',
  operatingLicenseNumber: 'LIC-1',
  operatingLicenseIssuedAt: '2025-01-01',
  operatingLicenseIssuedBy: 'Transport Authority',
  streetAddress: '1 Main Street',
  city: 'Casablanca',
  country: 'Morocco',
  zipCode: '20000',
  companyPhone: '+212600000000',
  companyEmail: 'company@example.test',
  yearsActive: '3',
  responsibleName: 'Aya Haddad',
  responsibleRole: 'Owner',
  responsibleIdentityNumber: 'ID-1',
  responsiblePhone: '+212600000001',
  responsibleEmail: 'responsible@example.test',
  plan: 'GROWTH',
  billingPeriod: 'MONTHLY',
  currency: 'MAD',
  paymentProvider: 'AMANPAY',
}

describe('role-specific auth schema contracts', () => {
  it('defaults company signup language and keeps billing provider constrained', () => {
    expect(companySignupSchema.parse(validCompanySignup)).toMatchObject({ preferredLanguage: 'en', paymentProvider: 'AMANPAY' })
    expect(() => companySignupSchema.parse({ ...validCompanySignup, paymentProvider: 'WIRE_TRANSFER' })).toThrow()
    expect(() => companySignupSchema.parse({ ...validCompanySignup, currency: 'EUR' })).toThrow()
  })

  it('normalizes employee email fields and validates reset password strength', () => {
    expect(employeeLoginSchema.parse({ email: 'OWNER@EXAMPLE.TEST', password: 'secret' })).toEqual({
      email: 'owner@example.test',
      password: 'secret',
    })
    expect(employeeForgotPasswordSchema.parse({ email: 'HELP@EXAMPLE.TEST' })).toEqual({ email: 'help@example.test' })
    expect(employeeResetPasswordSchema.parse({ token: 'token_1', password: 'new-pass-123' })).toEqual({ token: 'token_1', password: 'new-pass-123' })
    expect(() => employeeResetPasswordSchema.parse({ token: '', password: 'short' })).toThrow()
  })

  it('limits employee language changes to supported locales', () => {
    expect(employeeLanguageSchema.parse({ language: 'fr' })).toEqual({ language: 'fr' })
    expect(() => employeeLanguageSchema.parse({ language: 'es' })).toThrow()
  })

  it('normalizes renter profile updates while keeping MAD as the only supported currency', () => {
    expect(renterUpdateSchema.parse({ firstName: '  Salma ', lastName: '  Idrissi ', preferredLocale: 'ar', preferredCurrency: 'MAD' })).toEqual({
      firstName: 'Salma',
      lastName: 'Idrissi',
      preferredLocale: 'ar',
      preferredCurrency: 'MAD',
    })
    expect(() => renterUpdateSchema.parse({ preferredCurrency: 'EUR' })).toThrow()
  })

  it('requires a renter FCM token payload key', () => {
    expect(renterFcmTokenSchema.parse({ fcmToken: 'token_abc' })).toEqual({ fcmToken: 'token_abc' })
    expect(() => renterFcmTokenSchema.parse({ token: 'token_abc' })).toThrow()
  })
})
