import { describe, expect, it } from 'vitest'
import {
  menuItemSchema,
  menuPlanAssignmentsSchema,
  menuCompanyAssignmentsSchema,
  menuPreviewSchema,
  promotionCreateSchema,
  promotionUpdateSchema,
} from './admin.schemas'

describe('admin menu and promotion schemas', () => {
  it('trims labels and applies safe menu item defaults', () => {
    expect(menuItemSchema.parse({ label: ' Fleet ', itemType: 'INTERNAL_PAGE' })).toMatchObject({
      label: 'Fleet',
      itemType: 'INTERNAL_PAGE',
      displayOrder: 0,
      openInNewTab: false,
      isRequired: false,
      isActive: true,
      roles: [],
      subscriptionPlans: [],
      companyAssignments: [],
    })
  })

  it('rejects negative menu ordering and invalid role previews', () => {
    expect(() => menuItemSchema.parse({ label: 'Fleet', itemType: 'INTERNAL_PAGE', displayOrder: -1 })).toThrow()
    expect(() => menuPreviewSchema.parse({ companyId: 'company_1', role: 'SUPER_ADMIN' })).toThrow()
  })

  it('requires at least one plan or company assignment', () => {
    expect(() => menuPlanAssignmentsSchema.parse({ assignments: [] })).toThrow()
    expect(() => menuCompanyAssignmentsSchema.parse({ assignments: [] })).toThrow()
    expect(menuPlanAssignmentsSchema.parse({ assignments: [{ plan: 'PRO' }] })).toEqual({ assignments: [{ plan: 'PRO' }] })
  })

  it('accepts only uppercase promotion codes and preserves update partiality', () => {
    const valid = {
      code: 'SUMMER_26',
      name: 'Summer 2026',
      discountType: 'PERCENTAGE',
      discountValue: 20,
      plans: ['STARTER', 'GROWTH'],
      periods: ['MONTHLY'],
      validFrom: '2026-07-01T00:00:00.000Z',
    }

    expect(promotionCreateSchema.parse(valid)).toMatchObject({ ...valid, isActive: true })
    expect(() => promotionCreateSchema.parse({ ...valid, code: 'summer' })).toThrow()
    expect(promotionUpdateSchema.parse({ name: 'Updated name' })).toEqual({ name: 'Updated name' })
  })
})
