import { describe, expect, it } from 'vitest'
import { createSchema as complaintCreateSchema, listQuerySchema as complaintListQuerySchema, updateSchema as complaintUpdateSchema } from './complaints/complaint.schemas'
import { historyQuerySchema, preferencesSchema, unreadQuerySchema } from './notifications/notification.schemas'
import { offerSchema } from './offers/offer.schemas'
import { cancelSchema, checkoutSchema, startTrialSchema } from './subscriptions/subscription.schemas'
import { inviteSchema, roleSchema } from './team/team.schemas'
import { listQuerySchema as reviewListQuerySchema, replySchema } from './reviews/review.schemas'
import { reportQuerySchema, summaryQuerySchema } from './analytics/analytics.schemas'

describe('operational schemas', () => {
  it('validates offers, complaints, and reviews without inventing impossible statuses', () => {
    expect(offerSchema.parse({
      title: 'Summer',
      type: 'PERCENTAGE',
      discountValue: 15,
      validFrom: '2026-07-01T00:00:00.000Z',
      validUntil: '2026-08-01T00:00:00.000Z',
    })).toMatchObject({ appliesToAll: true, categories: [], vehicleIds: [], isActive: true, isPublic: true })
    expect(offerSchema.safeParse({ title: '', type: 'PERCENTAGE', discountValue: 15, validFrom: 'bad', validUntil: 'bad' }).success).toBe(false)

    expect(complaintCreateSchema.parse({ category: 'BILLING', subject: 'Wrong invoice' })).toMatchObject({ severity: 'LEVEL_1' })
    expect(complaintUpdateSchema.safeParse({ status: 'IGNORED' }).success).toBe(false)
    expect(complaintListQuerySchema.parse({ page: '2', pageSize: '25' })).toMatchObject({ page: 2, pageSize: 25 })
    expect(replySchema.safeParse({ companyReply: '' }).success).toBe(false)
    expect(reviewListQuerySchema.safeParse({ rating: '6' }).success).toBe(false)
  })

  it('validates subscriptions, team roles, notifications, and analytics query defaults', () => {
    expect(checkoutSchema.safeParse({ plan: 'PRO', billingPeriod: 'MONTHLY', currency: 'MAD', provider: 'PAYPAL', successUrl: 'https://ok.example.test', failureUrl: 'https://fail.example.test' }).success).toBe(true)
    expect(startTrialSchema.parse({ plan: 'STARTER', billingPeriod: 'ANNUAL' })).toMatchObject({ currency: 'MAD' })
    expect(cancelSchema.parse({})).toEqual({ mode: 'period_end' })
    expect(inviteSchema.safeParse({ firstName: 'A', lastName: 'B', email: 'agent@example.test', role: 'OWNER' }).success).toBe(false)
    expect(roleSchema.safeParse({ role: 'AGENT' }).success).toBe(true)

    expect(preferencesSchema.safeParse([{ notificationType: 'BOOKING', channel: 'EMAIL', enabled: true }]).success).toBe(true)
    expect(unreadQuerySchema.parse({ unread: 'true' })).toEqual({ unread: 'true' })
    expect(historyQuerySchema.parse({ limit: '100' })).toEqual({ limit: 100 })
    expect(historyQuerySchema.safeParse({ limit: '501' }).success).toBe(false)
    expect(summaryQuerySchema.parse({})).toEqual({ period: '30d' })
    expect(reportQuerySchema.parse({})).toEqual({ format: 'JSON' })
  })
})
