/**
 * (Zod: login URL, signup, token, auth info, refresh/logout).
 */
import {
  GetUrlLoginResponseSchema,
  SignUpRequestSchema,
  GetTokenRequestSchema,
  AuthInfoBaseSchema,
  AuthInfoBaseResponseSchema,
  LogoutRequestSchema,
  AuthInfoFullSchema,
} from '../auth.schema'
import { MOCK_AUTH_INFO_BASE } from '@/_tests_/mocks'

describe('[services/auth] auth.schema', () => {
  describe('GetUrlLoginResponseSchema', () => {
    it('parse login url response', () => {
      expect(
        GetUrlLoginResponseSchema.safeParse({
          message: 'OK',
          code: '200',
          data: 'https://example.com/login',
        }).success
      ).toBe(true)
    })
  })

  describe('SignUpRequestSchema', () => {
    const validSignUpBody = {
      full_name: 'Nguyen Van A',
      password: 'ValidPass1!',
      account_type: 'BUSINESS_OWNER' as const,
      company_name: 'Company',
      company_email: 'co@company.com',
      business_type: 'FINANCE',
      phone_number: '09012345678',
      tax_code: '0123456789',
      recaptchaToken: 'recaptcha-jwt-from-client',
    }

    it('chấp nhận payload API đăng ký (snake_case, có recaptchaToken, không confirm/tnc)', () => {
      const result = SignUpRequestSchema.safeParse(validSignUpBody)
      expect(result.success).toBe(true)
    })

    it('từ chối khi thiếu recaptchaToken hoặc token rỗng', () => {
      const { recaptchaToken: _r, ...withoutToken } = validSignUpBody
      expect(SignUpRequestSchema.safeParse(withoutToken).success).toBe(false)
      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          recaptchaToken: '',
        }).success
      ).toBe(false)
    })

    it('từ chối khi thiếu field bắt buộc hoặc format sai', () => {
      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          company_email: 'not-an-email',
        }).success
      ).toBe(false)
      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          tax_code: '123',
        }).success
      ).toBe(false)
    })

    it('full_name: regex 3–50 ký tự áp dụng sau khi trim khoảng trắng đầu cuối', () => {
      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          full_name: '  Nguyen Van A  ',
        }).success,
      ).toBe(true)

      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          full_name: '  ab  ',
        }).success,
      ).toBe(false)
    })

    it('email, phone, tax, company_name, password: regex kiểm tra sau trim hai đầu', () => {
      expect(
        SignUpRequestSchema.safeParse({
          ...validSignUpBody,
          company_email: '  co@company.com  ',
          phone_number: '  09012345678  ',
          tax_code: '  0123456789  ',
          company_name: '  Company  ',
          password: '  ValidPass1!  ',
        }).success,
      ).toBe(true)
    })
  })

  describe('GetTokenRequestSchema', () => {
    it('cần code và redirect_uri', () => {
      expect(GetTokenRequestSchema.safeParse({ code: 'c', redirect_uri: 'https://a.com' }).success).toBe(true)
      expect(GetTokenRequestSchema.safeParse({ code: 'c' }).success).toBe(false)
    })
  })

  describe('AuthInfoBaseSchema', () => {
    it('parse auth response từ server', () => {
      const result = AuthInfoBaseSchema.safeParse(MOCK_AUTH_INFO_BASE)
      expect(result.success).toBe(true)
    })
  })

  describe('AuthInfoBaseResponseSchema', () => {
    it('parse auth base response wrapper', () => {
      expect(
        AuthInfoBaseResponseSchema.safeParse({
          message: 'OK',
          code: '200',
          data: MOCK_AUTH_INFO_BASE,
        }).success
      ).toBe(true)
    })
  })

  describe('LogoutRequestSchema', () => {
    it('requires callback', () => {
      expect(
        LogoutRequestSchema.safeParse({
          callback: 'https://app.example.com',
        }).success
      ).toBe(true)
      expect(LogoutRequestSchema.safeParse({}).success).toBe(false)
    })
  })

  describe('AuthInfoFullSchema', () => {
    it('transforms auth info and injects refresh_at + role from scope', () => {
      const result = AuthInfoFullSchema.parse(MOCK_AUTH_INFO_BASE)

      expect(result.refresh_at).toBeGreaterThan(Date.now())
      expect(result.user_info).toEqual(MOCK_AUTH_INFO_BASE.user_info)
      expect(result.role).toBeUndefined()
    })

    it('detects role from scope', () => {
      const result = AuthInfoFullSchema.parse({
        ...MOCK_AUTH_INFO_BASE,
        scope: 'apim:business openid',
      })

      expect(result.role).toBe('BUSINESS_OWNER')
    })
  })
})
