import { describe, expect, it } from 'vitest'
import {
  cancelSchema,
  capturePaypalSchema,
  changePlanSchema,
  checkoutSchema,
  reactivateSchema,
  startTrialSchema,
} from './subscription.schemas'

describe('subscription.schemas edge contracts', () => {
  it('accepts checkout only for MAD and valid hosted payment urls', () => {
    const payload = {
      plan: 'PRO',
      billingPeriod: 'ANNUAL',
      currency: 'MAD',
      provider: 'PAYPAL',
      successUrl: 'https://app.example.test/success',
      failureUrl: 'https://app.example.test/failure',
    }

    expect(checkoutSchema.parse(payload)).toEqual(payload)
    expect(checkoutSchema.safeParse({ ...payload, currency: 'USD' }).success).toBe(false)
    expect(checkoutSchema.safeParse({ ...payload, successUrl: '/relative' }).success).toBe(false)
  })

  it('allows plan changes across supported currencies while trial and reactivation stay MAD-only', () => {
    expect(changePlanSchema.parse({ plan: 'STARTER', billingPeriod: 'MONTHLY', currency: 'USD' })).toEqual({
      plan: 'STARTER',
      billingPeriod: 'MONTHLY',
      currency: 'USD',
    })

    expect(startTrialSchema.parse({ plan: 'GROWTH', billingPeriod: 'MONTHLY' })).toEqual({
      plan: 'GROWTH',
      billingPeriod: 'MONTHLY',
      currency: 'MAD',
    })

    expect(reactivateSchema.safeParse({
      plan: 'GROWTH',
      billingPeriod: 'MONTHLY',
      currency: 'EUR',
      provider: 'AMANPAY',
      successUrl: 'https://app.example.test/success',
      failureUrl: 'https://app.example.test/failure',
    }).success).toBe(false)
  })

  it('defaults cancellation to period-end and caps cancellation reasons', () => {
    expect(cancelSchema.parse({})).toEqual({ mode: 'period_end' })
    expect(cancelSchema.parse({ mode: 'immediate', reason: 'closing account' })).toEqual({ mode: 'immediate', reason: 'closing account' })
    expect(cancelSchema.safeParse({ mode: 'tomorrow' }).success).toBe(false)
    expect(cancelSchema.safeParse({ reason: 'x'.repeat(501) }).success).toBe(false)
  })

  it('requires a PayPal order id for manual capture', () => {
    expect(capturePaypalSchema.parse({ paypalOrderId: 'order_1' })).toEqual({ paypalOrderId: 'order_1' })
    expect(capturePaypalSchema.safeParse({}).success).toBe(false)
  })
})
