import { describe, expect, it } from 'vitest'
import {
  ClientDetailResponseSchema,
  ClientListResponseSchema,
  ClientStatusResponseSchema,
  CreateClientRequestSchema,
  ListClientsQuerySchema,
  UpdateClientRequestSchema
} from './api-schemas'

const VALID_UUID = '00000000-0000-4000-8000-000000000001'
const ISO_TS = '2026-05-08T00:00:00.000Z'

describe('client API schemas', () => {
  it('coerces list pagination and accepts status/search filters', () => {
    const result = ListClientsQuerySchema.safeParse({
      status: 'active',
      source: 'acquisition',
      search: 'Acme',
      limit: '10',
      offset: '20'
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data).toEqual({
        status: 'active',
        source: 'acquisition',
        search: 'Acme',
        limit: 10,
        offset: 20
      })
    }
  })

  it('rejects unknown list query fields', () => {
    expect(ListClientsQuerySchema.safeParse({ includeDeals: true }).success).toBe(false)
  })

  it('accepts client list responses', () => {
    expect(
      ClientListResponseSchema.safeParse({
        data: [
          {
            id: VALID_UUID,
            organizationId: VALID_UUID,
            name: 'Acme',
            status: 'active',
            source: 'manual',
            sourceDealId: null,
            primaryCompanyId: null,
            primaryContactId: null,
            convertedAt: null,
            metadata: {},
            createdAt: ISO_TS,
            updatedAt: ISO_TS
          }
        ],
        total: 1,
        limit: 50,
        offset: 0
      }).success
    ).toBe(true)
  })

  it('accepts client detail lineage refs', () => {
    expect(
      ClientDetailResponseSchema.safeParse({
        id: VALID_UUID,
        organizationId: VALID_UUID,
        name: 'Acme',
        status: 'onboarding',
        source: 'acquisition',
        sourceDealId: VALID_UUID,
        primaryCompanyId: VALID_UUID,
        primaryContactId: VALID_UUID,
        convertedAt: ISO_TS,
        metadata: { externalProjectSlug: 'developer-workspace-slug' },
        createdAt: ISO_TS,
        updatedAt: ISO_TS,
        lineage: {
          deals: [
            {
              id: VALID_UUID,
              contactEmail: 'lead@example.com',
              stageKey: 'closed_won',
              stateKey: null,
              updatedAt: ISO_TS
            }
          ],
          projects: [{ id: VALID_UUID, name: 'Delivery', kind: 'client_engagement', status: 'active', updatedAt: ISO_TS }],
          companies: [{ id: VALID_UUID, name: 'Acme Co', domain: 'acme.test', status: 'active' }],
          contacts: [
            {
              id: VALID_UUID,
              email: 'lead@example.com',
              firstName: 'Ada',
              lastName: null,
              title: 'CEO',
              companyId: VALID_UUID,
              status: 'active'
            }
          ]
        }
      }).success
    ).toBe(true)
  })

  it('accepts direct word-of-mouth client create requests with metadata for workspace details', () => {
    expect(
      CreateClientRequestSchema.safeParse({
        name: 'Byron for Irvine',
        status: 'onboarding',
        source: 'word_of_mouth',
        metadata: {
          externalProjectSlug: 'developer-workspace-slug',
          campaignSlug: 'byron-for-irvine'
        }
      }).success
    ).toBe(true)
  })

  it('rejects invalid client source format', () => {
    expect(
      CreateClientRequestSchema.safeParse({
        name: 'Byron for Irvine',
        source: 'Word of mouth'
      }).success
    ).toBe(false)
  })

  it('accepts source-only client update requests', () => {
    expect(UpdateClientRequestSchema.safeParse({ source: 'acquisition' }).success).toBe(true)
  })

  it('accepts client portfolio status responses', () => {
    expect(
      ClientStatusResponseSchema.safeParse({
        totalClients: 1,
        byStatus: { active: 1, onboarding: 0, paused: 0, completed: 0, churned: 0 },
        linkedDeals: 1,
        linkedProjects: 1,
        linkedCompanies: 1,
        linkedContacts: 1
      }).success
    ).toBe(true)
  })
})
