/**
 * (Zod: filter, sort, pagination, base API envelope — happy + BVA/negative).
 */
import {
  FilterSchema,
  FilterQuerySchema,
  FilterSortSchema,
  PaginationSchema,
  BaseAPIResponseSchema,
} from '@/models/api/common'

describe('[models/api/common]', () => {
  describe('FilterSchema', () => {
    it('parses full filter with keyword and sort', () => {
      const result = FilterSchema.safeParse({
        limit: 10,
        offset: 0,
        keyword: 'test',
        sort: { name: 'created', dir: 'asc' },
      })
      expect(result.success).toBe(true)
    })

    it('applies default limit and offset when omitted', () => {
      const result = FilterSchema.safeParse({})
      expect(result.success).toBe(true)
      if (result.success) {
        expect(result.data.limit).toBe(5)
        expect(result.data.offset).toBe(0)
      }
    })
  })

  describe('FilterQuerySchema', () => {
    it.each([
      ['eq', { name: 'a', type: 'eq' as const, value: 'x' }],
      ['ne', { name: 'a', type: 'ne' as const, value: 'x' }],
      ['like', { name: 'a', type: 'like' as const, value: 'x' }],
    ] as const)('accepts type %s', (_label, payload) => {
      expect(FilterQuerySchema.safeParse(payload).success).toBe(true)
    })
  })

  describe('FilterSortSchema', () => {
    it('accepts asc and desc only', () => {
      expect(FilterSortSchema.safeParse({ name: 'x', dir: 'asc' }).success).toBe(
        true,
      )
      expect(FilterSortSchema.safeParse({ name: 'x', dir: 'desc' }).success).toBe(
        true,
      )
      expect(
        FilterSortSchema.safeParse({ name: 'x', dir: 'invalid' }).success,
      ).toBe(false)
    })
  })

  describe('PaginationSchema', () => {
    it('parses offset/limit/total and nav cursors', () => {
      const result = PaginationSchema.safeParse({
        offset: 0,
        limit: 10,
        total: 100,
        next: '',
        previous: '',
      })
      expect(result.success).toBe(true)
    })
  })

  describe('BaseAPIResponseSchema', () => {
    it('parses minimal success envelope', () => {
      const result = BaseAPIResponseSchema.safeParse({
        message: 'OK',
        code: '200',
      })
      expect(result.success).toBe(true)
    })
  })
})
