import { render } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import type { components } from '@/lib/gen';

import MarketplaceProjectRow from './MarketplaceProjectRow';

type MarketplaceProject = components['schemas']['ProjectResponse'];

describe('MarketplaceProjectRow', () => {
  // Mock toLocaleDateString to return consistent values across timezones
  const originalToLocaleDateString = Date.prototype.toLocaleDateString;

  beforeEach(() => {
    Date.prototype.toLocaleDateString = function () {
      return '1/1/2024';
    };
  });

  afterEach(() => {
    Date.prototype.toLocaleDateString = originalToLocaleDateString;
  });

  const baseProject: MarketplaceProject = {
    id: 'test-id-123',
    title: 'Test Project',
    description: 'This is a test project description',
    credit_bounty: 100,
    credits_held_in_escrow: 0,
    status: 'OPEN',
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z',
    creator_id: 'creator-123',
    creator_handle: 'testcreator',
    creator_display_name: 'Test Creator',
    applicant_count: 5,
  };

  it('renders with basic props', () => {
    const { asFragment } = render(
      <MarketplaceProjectRow project={baseProject} />
    );
    expect(asFragment()).toMatchSnapshot();
  });

  describe('status variants', () => {
    const statuses = [
      'DRAFT',
      'OPEN',
      'IN_PROGRESS',
      'PENDING_REVIEW',
      'COMPLETED',
      'CANCELLED',
    ];

    statuses.forEach((status) => {
      it(`renders ${status} status correctly`, () => {
        const { asFragment } = render(
          <MarketplaceProjectRow
            project={{ ...baseProject, status: status as any }}
          />
        );
        expect(asFragment()).toMatchSnapshot();
      });
    });
  });

  it('renders without applicant count', () => {
    const { asFragment } = render(
      <MarketplaceProjectRow
        project={{ ...baseProject, applicant_count: undefined }}
      />
    );
    expect(asFragment()).toMatchSnapshot();
  });

  it('renders with zero applicants', () => {
    const { asFragment } = render(
      <MarketplaceProjectRow
        project={{ ...baseProject, applicant_count: 0, status: 'OPEN' }}
      />
    );
    expect(asFragment()).toMatchSnapshot();
  });

  it('renders with one applicant', () => {
    const { asFragment } = render(
      <MarketplaceProjectRow
        project={{ ...baseProject, applicant_count: 1, status: 'OPEN' }}
      />
    );
    expect(asFragment()).toMatchSnapshot();
  });

  it('renders without description', () => {
    const { asFragment } = render(
      <MarketplaceProjectRow project={{ ...baseProject, description: '' }} />
    );
    expect(asFragment()).toMatchSnapshot();
  });
});
