import { describe, expect, it } from 'vitest'
import {
  CommissionRateError,
  VendorAlreadyExistsError,
  VendorAuthorizationError,
  VendorNotFoundError,
  VendorProductNotFoundError,
  VendorStateError,
  isCommissionRateError,
  isVendorAlreadyExistsError,
  isVendorAuthorizationError,
  isVendorNotFoundError,
  isVendorProductNotFoundError,
  isVendorStateError,
} from './errors.js'
import { MarketplaceMigrateError, isMarketplaceMigrateError } from './migrate.js'

describe('error-guards', () => {
  it('matches own class and rejects siblings and plain objects', () => {
    const vendorNotFound = new VendorNotFoundError({ vendorId: 'v1' })
    const vendorProductNotFound = new VendorProductNotFoundError({ productId: 'p1' })
    const vendorAuthorization = new VendorAuthorizationError()
    const vendorState = new VendorStateError({
      vendorId: 'v1',
      from: 'pending',
      to: 'approved',
    })
    const commissionRate = new CommissionRateError({ bps: 10001 })
    const vendorAlreadyExists = new VendorAlreadyExistsError({ ownerUserId: 'u1' })

    expect(isVendorNotFoundError(vendorNotFound)).toBe(true)
    expect(isVendorNotFoundError(vendorProductNotFound)).toBe(false)
    expect(isVendorNotFoundError({ name: 'VendorNotFoundError', code: 'VENDOR_FORBIDDEN' })).toBe(false)

    expect(isVendorProductNotFoundError(vendorProductNotFound)).toBe(true)
    expect(isVendorProductNotFoundError(vendorNotFound)).toBe(false)
    expect(isVendorProductNotFoundError({ name: 'VendorProductNotFoundError', code: 'VENDOR_NOT_FOUND' })).toBe(
      false,
    )

    expect(isVendorAuthorizationError(vendorAuthorization)).toBe(true)
    expect(isVendorAuthorizationError(vendorState)).toBe(false)
    expect(isVendorAuthorizationError({ name: 'VendorAuthorizationError', code: 'VENDOR_STATE' })).toBe(false)

    expect(isVendorStateError(vendorState)).toBe(true)
    expect(isVendorStateError(vendorAuthorization)).toBe(false)
    expect(isVendorStateError({ name: 'VendorStateError', code: 'COMMISSION_RATE_INVALID' })).toBe(false)

    expect(isCommissionRateError(commissionRate)).toBe(true)
    expect(isCommissionRateError(vendorNotFound)).toBe(false)
    expect(isCommissionRateError({ name: 'CommissionRateError', code: 'VENDOR_NOT_FOUND' })).toBe(false)

    expect(isVendorAlreadyExistsError(vendorAlreadyExists)).toBe(true)
    expect(isVendorAlreadyExistsError(vendorNotFound)).toBe(false)
    expect(isVendorAlreadyExistsError({ name: 'VendorAlreadyExistsError', code: 'VENDOR_NOT_FOUND' })).toBe(
      false,
    )

    const marketplaceMigrate = new MarketplaceMigrateError('boom')
    expect(isMarketplaceMigrateError(marketplaceMigrate)).toBe(true)
    expect(isMarketplaceMigrateError(vendorNotFound)).toBe(false)
    expect(isMarketplaceMigrateError({ name: 'MarketplaceMigrateError', code: 'VENDOR_NOT_FOUND' })).toBe(
      false,
    )
  })
})
