import { describe, expect, it, vi } from 'vitest'

vi.mock('../../lib/prisma', () => ({ prisma: {} }))

import { createCompanySignup } from './auth.company.repo'

function makeDb() {
  return {
    company: { create: vi.fn().mockResolvedValue({ id: 'company_1', name: 'Atlas Cars', slug: 'atlas-cars' }) },
    brandSettings: { create: vi.fn().mockResolvedValue({}) },
    contractSettings: { create: vi.fn().mockResolvedValue({}) },
    subscription: { create: vi.fn().mockResolvedValue({}) },
    employee: { create: vi.fn().mockResolvedValue({ id: 'employee_1', email: 'owner@example.com' }) },
  }
}

const baseInput = {
  companyName: 'Atlas Cars',
  legalName: 'Atlas Cars SARL',
  slug: 'atlas-cars',
  ownerEmail: 'owner@example.com',
  companyEmail: 'contact@example.com',
  companyPhone: '+212600000000',
  streetAddress: '1 Fleet Street',
  city: 'Casablanca',
  country: 'Morocco',
  zipCode: '20000',
  legalForm: 'SARL',
  managerName: 'Aya Benali',
  iceNumber: 'ICE123',
  taxId: 'TAX123',
  operatingLicenseNumber: 'LIC123',
  operatingLicenseIssuedAt: '2026-01-01',
  operatingLicenseIssuedBy: 'Transport Authority',
  fax: '',
  yearsActive: '5',
  representativeName: '',
  representativeTitle: '',
  responsibleName: 'Omar Alaoui',
  responsibleRole: 'Manager',
  responsibleIdentityNumber: 'ID123',
  responsibleQualification: '',
  responsiblePhone: '+212611111111',
  responsibleEmail: 'responsible@example.com',
  currency: 'MAD' as const,
  registrationNumber: 'REG123',
  plan: 'GROWTH' as const,
  billingPeriod: 'ANNUAL' as const,
  preferredLanguage: 'fr' as const,
  firstName: 'Aya',
  lastName: 'Benali',
  passwordHash: 'hashed-password',
  now: new Date('2026-06-01T00:00:00.000Z'),
  trialEndAt: new Date('2026-08-30T00:00:00.000Z'),
}

describe('auth.company.repo.createCompanySignup', () => {
  it('creates the company, tenant settings, trial subscription, and active owner in one transaction client', async () => {
    const db = makeDb()

    await expect(createCompanySignup(db as never, baseInput)).resolves.toEqual({
      company: { id: 'company_1', name: 'Atlas Cars', slug: 'atlas-cars' },
      employee: { id: 'employee_1', email: 'owner@example.com' },
    })

    expect(db.company.create).toHaveBeenCalledWith({ data: expect.objectContaining({
      name: 'Atlas Cars',
      slug: 'atlas-cars',
      email: 'contact@example.com',
      status: 'TRIALING',
      address: expect.objectContaining({
        legalName: 'Atlas Cars SARL',
        companyEmail: 'contact@example.com',
        fax: null,
        representativeName: null,
        responsibleEmail: 'responsible@example.com',
      }),
    }) })

    expect(db.brandSettings.create).toHaveBeenCalledWith({ data: expect.objectContaining({
      companyId: 'company_1',
      displayName: 'Atlas Cars',
      subdomain: 'atlas-cars',
      defaultLocale: 'fr',
      defaultCurrency: 'MAD',
    }) })

    expect(db.contractSettings.create).toHaveBeenCalledWith({ data: expect.objectContaining({
      companyId: 'company_1',
      registrationNumber: 'REG123',
      taxId: 'TAX123',
    }) })

    expect(db.subscription.create).toHaveBeenCalledWith({ data: expect.objectContaining({
      companyId: 'company_1',
      plan: 'GROWTH',
      billingPeriod: 'ANNUAL',
      status: 'TRIALING',
      trialStartAt: baseInput.now,
      trialEndAt: baseInput.trialEndAt,
    }) })

    expect(db.employee.create).toHaveBeenCalledWith({ data: expect.objectContaining({
      companyId: 'company_1',
      clerkUserId: 'local_owner_company_1',
      email: 'owner@example.com',
      emailVerified: baseInput.now,
      role: 'OWNER',
      preferredLanguage: 'fr',
      isActive: true,
    }) })
  })
})
