import { render, screen } from '@testing-library/react';
import dayjs from 'dayjs';
import { ExpirationBadge } from './ExpirationBadge';

describe('ExpirationBadge', () => {
  describe('when no expiration date is provided', () => {
    it('renders nothing for null', () => {
      const { container } = render(<ExpirationBadge expirationDate={null} />);
      expect(container.firstChild).toBeNull();
    });

    it('renders nothing for undefined', () => {
      const { container } = render(<ExpirationBadge expirationDate={undefined} />);
      expect(container.firstChild).toBeNull();
    });

    it('renders nothing for empty string', () => {
      const { container } = render(<ExpirationBadge expirationDate="" />);
      expect(container.firstChild).toBeNull();
    });
  });

  describe('when document is expired', () => {
    it('shows "Expired" badge for past dates', () => {
      const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={yesterday} />);
      expect(screen.getByText('Expired')).toBeInTheDocument();
    });

    it('shows "Expired" badge for dates far in the past', () => {
      const pastDate = dayjs().subtract(1, 'year').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={pastDate} />);
      expect(screen.getByText('Expired')).toBeInTheDocument();
    });

    it('has red styling for expired badge', () => {
      const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={yesterday} />);
      const badge = screen.getByText('Expired');
      expect(badge).toHaveClass('bg-red-100', 'text-red-800');
    });
  });

  describe('when document expires today', () => {
    it('shows "Expires today" badge', () => {
      const today = dayjs().format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={today} />);
      expect(screen.getByText('Expires today')).toBeInTheDocument();
    });

    it('has amber styling for expiring soon badge', () => {
      const today = dayjs().format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={today} />);
      const badge = screen.getByText('Expires today');
      expect(badge).toHaveClass('bg-amber-100', 'text-amber-800');
    });
  });

  describe('when document expires tomorrow', () => {
    it('shows "Expires tomorrow" badge', () => {
      const tomorrow = dayjs().add(1, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={tomorrow} />);
      expect(screen.getByText('Expires tomorrow')).toBeInTheDocument();
    });
  });

  describe('when document expires within 30 days', () => {
    it('shows "Expires in X days" badge for 15 days', () => {
      const in15Days = dayjs().add(15, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={in15Days} />);
      expect(screen.getByText('Expires in 15 days')).toBeInTheDocument();
    });

    it('shows "Expires in X days" badge for 30 days', () => {
      const in30Days = dayjs().add(30, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={in30Days} />);
      expect(screen.getByText('Expires in 30 days')).toBeInTheDocument();
    });

    it('shows "Expires in X days" badge for 2 days', () => {
      const in2Days = dayjs().add(2, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={in2Days} />);
      expect(screen.getByText('Expires in 2 days')).toBeInTheDocument();
    });
  });

  describe('when document expires after 30 days', () => {
    it('renders nothing for 31 days out', () => {
      const in31Days = dayjs().add(31, 'day').format('YYYY-MM-DD');
      const { container } = render(<ExpirationBadge expirationDate={in31Days} />);
      expect(container.firstChild).toBeNull();
    });

    it('renders nothing for dates far in the future', () => {
      const nextYear = dayjs().add(1, 'year').format('YYYY-MM-DD');
      const { container } = render(<ExpirationBadge expirationDate={nextYear} />);
      expect(container.firstChild).toBeNull();
    });
  });

  describe('className prop', () => {
    it('applies custom className to the badge', () => {
      const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD');
      render(<ExpirationBadge expirationDate={yesterday} className="custom-class" />);
      const badge = screen.getByText('Expired');
      expect(badge).toHaveClass('custom-class');
    });
  });
});
