import request from 'supertest'
import { createApp } from '../../app'

const app = createApp()

function uniqueEmail(prefix: string) {
  return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}@test.com`
}

describe('Company signup API', () => {
  it('creates new accounts with a 30-day trial period', async () => {
    const startedAt = Date.now()

    const res = await request(app)
      .post('/api/v1/auth/company/signup')
      .send({
        firstName: 'Trial',
        lastName: 'Owner',
        email: uniqueEmail('trial-owner'),
        password: 'Password123!',
        companyName: `Trial Cars ${Date.now()}`,
        legalName: `Trial Cars SARL ${Date.now()}`,
        legalForm: 'LLC',
        registrationNumber: `REG-${Date.now()}`,
        iceNumber: `ICE-${Date.now()}`,
        taxId: `IF-${Date.now()}`,
        operatingLicenseNumber: `AGR-${Date.now()}`,
        operatingLicenseIssuedAt: '2026-01-15',
        operatingLicenseIssuedBy: 'Wilaya de Casablanca',
        streetAddress: '123 Main Street',
        city: 'Casablanca',
        country: 'Morocco',
        zipCode: '20000',
        companyPhone: '+212600000000',
        companyEmail: uniqueEmail('trial-company'),
        yearsActive: '1',
        representativeName: 'Trial Representative',
        representativeTitle: 'Managing Director',
        responsibleName: 'Operations Lead',
        responsibleRole: 'Responsible Manager',
        responsibleIdentityNumber: `CIN-${Date.now()}`,
        responsibleQualification: '10 years fleet operations experience',
        responsiblePhone: '+212611111111',
        responsibleEmail: uniqueEmail('trial-responsible'),
        preferredLanguage: 'en',
        plan: 'STARTER',
        billingPeriod: 'MONTHLY',
        currency: 'MAD',
        paymentProvider: 'AMANPAY',
      })

    expect(res.status).toBe(201)

    const trialEndsAt = new Date(res.body.data.trialEndsAt).getTime()
    const trialDurationDays = (trialEndsAt - startedAt) / (24 * 60 * 60 * 1000)

    expect(trialDurationDays).toBeGreaterThan(29.9)
    expect(trialDurationDays).toBeLessThan(30.1)
  })
})
