import { render, screen } from '@testing-library/react';
import { DocumentCardMetadata } from './DocumentCardMetadata';
import type { DocumentRecord } from '../types/documents.types';

const baseDocument: DocumentRecord = {
  id: 'doc-1',
  userId: 'user-1',
  vehicleId: 'vehicle-1',
  documentType: 'insurance',
  title: 'Test Document',
  sharedVehicleIds: [],
  createdAt: '2024-01-01T00:00:00Z',
  updatedAt: '2024-01-01T00:00:00Z',
};

describe('DocumentCardMetadata', () => {
  describe('insurance documents', () => {
    it('displays expiration date', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('06/15/2025')).toBeInTheDocument();
      expect(screen.getByText('Expires:')).toBeInTheDocument();
    });

    it('displays policy number', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        details: { policyNumber: 'POL-12345' },
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('POL-12345')).toBeInTheDocument();
      expect(screen.getByText('Policy #:')).toBeInTheDocument();
    });

    it('displays insurance company', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        details: { insuranceCompany: 'Acme Insurance' },
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('Acme Insurance')).toBeInTheDocument();
      expect(screen.getByText('Company:')).toBeInTheDocument();
    });

    it('limits to 3 fields in card variant', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
        issuedDate: '2024-06-15',
        details: {
          policyNumber: 'POL-12345',
          insuranceCompany: 'Acme Insurance',
          premium: 1200.5,
        },
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      // Should show first 3: expiration, policy, company
      expect(screen.getByText('06/15/2025')).toBeInTheDocument();
      expect(screen.getByText('POL-12345')).toBeInTheDocument();
      expect(screen.getByText('Acme Insurance')).toBeInTheDocument();
      // Premium should not be shown in card variant (4th item)
      expect(screen.queryByText('$1200.50')).not.toBeInTheDocument();
    });

    it('shows all fields in detail variant', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
        issuedDate: '2024-06-15',
        details: {
          policyNumber: 'POL-12345',
          insuranceCompany: 'Acme Insurance',
          bodilyInjuryPerson: '$50,000',
          bodilyInjuryIncident: '$100,000',
          propertyDamage: '$25,000',
          premium: 1200.5,
        },
      };
      render(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText('06/15/2025')).toBeInTheDocument();
      expect(screen.getByText('POL-12345')).toBeInTheDocument();
      expect(screen.getByText('Acme Insurance')).toBeInTheDocument();
      expect(screen.getByText('$50,000')).toBeInTheDocument();
      expect(screen.getByText('$100,000')).toBeInTheDocument();
      expect(screen.getByText('$25,000')).toBeInTheDocument();
      expect(screen.getByText('$1200.50')).toBeInTheDocument();
    });
  });

  describe('registration documents', () => {
    it('displays expiration date', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'registration',
        expirationDate: '2025-12-31',
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('12/31/2025')).toBeInTheDocument();
    });

    it('displays license plate', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'registration',
        details: { licensePlate: 'ABC 123' },
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('ABC 123')).toBeInTheDocument();
      expect(screen.getByText('Plate:')).toBeInTheDocument();
    });

    it('shows cost in detail variant only', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'registration',
        details: { cost: 150.0 },
      };
      const { rerender } = render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.queryByText('$150.00')).not.toBeInTheDocument();

      rerender(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText('$150.00')).toBeInTheDocument();
    });
  });

  describe('manual documents', () => {
    it('displays issued date if set', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'manual',
        issuedDate: '2020-01-15',
      };
      render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.getByText('01/15/2020')).toBeInTheDocument();
      expect(screen.getByText('Issued:')).toBeInTheDocument();
    });

    it('shows notes preview in detail variant only', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'manual',
        notes: 'This is a test note about the manual',
      };
      const { rerender } = render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(screen.queryByText(/This is a test note/)).not.toBeInTheDocument();

      rerender(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText('This is a test note about the manual')).toBeInTheDocument();
    });

    it('truncates long notes in detail variant', () => {
      const longNote = 'A'.repeat(150);
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'manual',
        notes: longNote,
      };
      render(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText(/^A{100}\.\.\.$/)).toBeInTheDocument();
    });
  });

  describe('empty states', () => {
    it('returns null when no metadata to display', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'manual',
        details: null,
      };
      const { container } = render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(container.firstChild).toBeNull();
    });

    it('handles missing details gracefully', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        details: undefined,
      };
      const { container } = render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(container.firstChild).toBeNull();
    });
  });

  describe('variant styling', () => {
    it('uses text-xs for mobile variant', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
      };
      const { container } = render(<DocumentCardMetadata doc={doc} variant="mobile" />);
      expect(container.firstChild).toHaveClass('text-xs');
    });

    it('uses text-sm for card variant', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
      };
      const { container } = render(<DocumentCardMetadata doc={doc} variant="card" />);
      expect(container.firstChild).toHaveClass('text-sm');
    });

    it('uses grid layout for detail variant', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        expirationDate: '2025-06-15',
      };
      const { container } = render(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(container.firstChild).toHaveClass('grid');
    });
  });

  describe('currency formatting', () => {
    it('formats premium correctly', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'insurance',
        details: { premium: 1234.56 },
      };
      render(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText('$1234.56')).toBeInTheDocument();
    });

    it('handles string numbers', () => {
      const doc: DocumentRecord = {
        ...baseDocument,
        documentType: 'registration',
        details: { cost: '99.99' },
      };
      render(<DocumentCardMetadata doc={doc} variant="detail" />);
      expect(screen.getByText('$99.99')).toBeInTheDocument();
    });
  });
});
