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'

describe('auth schemas', () => {
  const validCompanySignup = {
    firstName: 'Aya',
    lastName: 'Benali',
    email: 'owner@example.test',
    password: 'safe-password',
    companyName: 'Atlas Cars',
    legalName: 'Atlas Cars SARL',
    legalForm: 'SARL',
    registrationNumber: 'RC-1',
    iceNumber: 'ICE-1',
    taxId: 'TAX-1',
    operatingLicenseNumber: 'LIC-1',
    operatingLicenseIssuedAt: '2024-01-01',
    operatingLicenseIssuedBy: 'Rabat',
    streetAddress: '1 Avenue',
    city: 'Rabat',
    country: 'Morocco',
    zipCode: '10000',
    companyPhone: '+212600000000',
    companyEmail: 'company@example.test',
    yearsActive: '3',
    responsibleName: 'Aya Benali',
    responsibleRole: 'Owner',
    responsibleIdentityNumber: 'ID-1',
    responsiblePhone: '+212600000001',
    responsibleEmail: 'owner@example.test',
    plan: 'GROWTH',
    billingPeriod: 'MONTHLY',
    currency: 'MAD',
    paymentProvider: 'AMANPAY',
  } as const

  it('defaults company signup language and rejects unsupported commercial choices', () => {
    const parsed = companySignupSchema.parse(validCompanySignup)

    expect(parsed.preferredLanguage).toBe('en')
    expect(companySignupSchema.safeParse({ ...validCompanySignup, plan: 'ENTERPRISE' }).success).toBe(false)
    expect(companySignupSchema.safeParse({ ...validCompanySignup, currency: 'EUR' }).success).toBe(false)
    expect(companySignupSchema.safeParse({ ...validCompanySignup, paymentProvider: 'STRIPE' }).success).toBe(false)
  })

  it('normalizes employee auth fields and rejects weak reset payloads', () => {
    expect(employeeLoginSchema.parse({ email: 'Agent@Example.COM', password: 'password' })).toEqual({
      email: 'agent@example.com',
      password: 'password',
    })
    expect(employeeForgotPasswordSchema.parse({ email: 'Reset@Example.COM' }).email).toBe('reset@example.com')
    expect(employeeLanguageSchema.safeParse({ language: 'ar' }).success).toBe(true)
    expect(employeeLanguageSchema.safeParse({ language: 'es' }).success).toBe(false)
    expect(employeeResetPasswordSchema.safeParse({ token: '', password: 'long-enough' }).success).toBe(false)
    expect(employeeResetPasswordSchema.safeParse({ token: 'token_1', password: 'short' }).success).toBe(false)
  })

  it('trims renter profile updates and keeps renter push token required', () => {
    expect(renterUpdateSchema.parse({ firstName: ' Aya ', lastName: ' Benali ', phone: ' 0600000000 ', preferredLocale: 'fr', preferredCurrency: 'MAD' })).toEqual({
      firstName: 'Aya',
      lastName: 'Benali',
      phone: '0600000000',
      preferredLocale: 'fr',
      preferredCurrency: 'MAD',
    })
    expect(renterUpdateSchema.safeParse({ preferredLocale: 'es' }).success).toBe(false)
    expect(renterUpdateSchema.safeParse({ preferredCurrency: 'EUR' }).success).toBe(false)
    expect(renterFcmTokenSchema.safeParse({ fcmToken: 'token_1' }).success).toBe(true)
    expect(renterFcmTokenSchema.safeParse({}).success).toBe(false)
  })
})
