/**
 * (auth URL detection; environment flag helpers with module reset).
 */
import { isAuthRequest, AUTH_REQUEST_PATTERNS } from '@/constants/system'

describe('[constants/system]', () => {
  describe('isAuthRequest', () => {
    it('returns true when path includes token segment', () => {
      expect(isAuthRequest('/token')).toBe(true)
      expect(isAuthRequest('/api/v1/token')).toBe(true)
      expect(isAuthRequest('https://api.example.com/token')).toBe(true)
    })

    it('returns false for non-auth paths and undefined', () => {
      expect(isAuthRequest('/application/list')).toBe(false)
      expect(isAuthRequest('/profile')).toBe(false)
      expect(isAuthRequest(undefined)).toBe(false)
    })
  })

  describe('AUTH_REQUEST_PATTERNS', () => {
    it('includes /token pattern marker', () => {
      expect(AUTH_REQUEST_PATTERNS).toContain('/token')
    })
  })

  describe('environment helpers', () => {
    const originalEnv = process.env.NEXT_PUBLIC_ENVIRONMENT

    afterEach(() => {
      process.env.NEXT_PUBLIC_ENVIRONMENT = originalEnv
      jest.resetModules()
    })

    it('detects develop, uat, staging, production', () => {
      process.env.NEXT_PUBLIC_ENVIRONMENT = 'develop' as any
      jest.resetModules()
      const dev = require('@/constants/system') as typeof import('@/constants/system')
      expect(dev.isDev()).toBe(true)
      expect(dev.isLocal()).toBe(false)

      process.env.NEXT_PUBLIC_ENVIRONMENT = 'uat' as any
      jest.resetModules()
      const uat = require('@/constants/system') as typeof import('@/constants/system')
      expect(uat.isUAT()).toBe(true)

      process.env.NEXT_PUBLIC_ENVIRONMENT = 'staging' as any
      jest.resetModules()
      const staging = require('@/constants/system') as typeof import('@/constants/system')
      expect(staging.isStaging()).toBe(true)

      process.env.NEXT_PUBLIC_ENVIRONMENT = 'production' as any
      jest.resetModules()
      const prod = require('@/constants/system') as typeof import('@/constants/system')
      expect(prod.isProd()).toBe(true)
    })

    it('treats local as local and dev', () => {
      process.env.NEXT_PUBLIC_ENVIRONMENT = 'local' as any
      jest.resetModules()
      const local = require('@/constants/system') as typeof import('@/constants/system')
      expect(local.isLocal()).toBe(true)
      expect(local.isDev()).toBe(true)
    })

    it('defaults to production when NEXT_PUBLIC_ENVIRONMENT unset', () => {
      delete (process.env as any).NEXT_PUBLIC_ENVIRONMENT
      jest.resetModules()
      const mod = require('@/constants/system') as typeof import('@/constants/system')
      expect(mod.isProd()).toBe(true)
    })
  })
})
