/**
 * (loading / error redirect / PENDING success copy + CTA contract).
 */
jest.mock('@/services/auth/auth.query-options', () => ({
  useParseToken: jest.fn(),
}))

jest.mock('@/share/layout/end-user/header/hook', () => ({
  useGetUrlLoginMutation: () => ({ mutate: jest.fn() }),
}))

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => {
    const copy: Record<string, string> = {
      'status.create.title': 'Join Techcombank Open API',
      'status.create.description':
        'Great job finishing everything! One last step: open your email and tap the verification link to confirm.',
      'btn.done': 'Back to Log in',
    }
    return copy[key] ?? key
  },
}))

import React from 'react'
import { render, screen } from '@testing-library/react'
import SignUpStatus from '../index'

const replace = jest.fn()

jest.mock('@/i18n/navigation', () => ({
  Link: ({ href, children }: any) => <a href={href}>{children}</a>,
  useRouter: () => ({ replace: replace }),
}))
jest.mock('@/share/ui/button', () => ({
  Button: ({ children, asChild }: any) =>
    asChild ? <>{children}</> : <button>{children}</button>,
}))
jest.mock('@/share/ui/card', () => ({
  Card: ({ children }: any) => <div>{children}</div>,
  CardContent: ({ children }: any) => <div>{children}</div>,
}))
jest.mock('@/share/icons', () => ({
  ToastSuccess: () => <span>ToastSuccess</span>,
  ToastInfo: () => <span>ToastInfo</span>,
}))
jest.mock('@/share/components/full-page/loading', () => ({
  __esModule: true,
  default: () => <div data-testid="signup-status-loading">Loading</div>,
}))

const { useParseToken } = require('@/services/auth/auth.query-options')

describe('[components/auth/sign-up/status] SignUpStatus', () => {
  beforeEach(() => {
    replace.mockClear()
    useParseToken.mockReset()
  })

  describe('loading', () => {
    it('shows full-page loading while token query runs', () => {
      useParseToken.mockReturnValue({
        isLoading: true,
        error: null,
        isSuccess: false,
      })
      render(<SignUpStatus token="t" />)
      expect(screen.getByTestId('signup-status-loading')).toBeInTheDocument()
    })
  })

  describe('errors — parse token', () => {
    it('redirects to /signup when parse fails', () => {
      useParseToken.mockReturnValue({
        isLoading: false,
        error: new Error('bad'),
        isSuccess: false,
      })
      render(<SignUpStatus token="t" />)
      expect(replace).toHaveBeenCalledWith('/signup')
      expect(replace).toHaveBeenCalledTimes(1)
    })
  })

  describe('happy path — CREATE', () => {
    it('renders create title, body copy and back-to-login control', () => {
      useParseToken.mockReturnValue({
        isLoading: false,
        error: null,
        isSuccess: true,
        data: {
          message: 'OK',
          code: '200',
          data: { status: 'CREATE' },
        },
      })
      render(<SignUpStatus token="t" />)
      expect(screen.getByText('Join Techcombank Open API')).toBeInTheDocument()
      expect(
        screen.getByText(/Great job finishing everything/),
      ).toBeInTheDocument()
      expect(
        screen.getByRole('button', { name: /Back to Log in/i }),
      ).toBeInTheDocument()
    })
  })
})
