import { describe, it, expect } from 'vitest';
import { formatTryOnDate } from './dateLabels';
import { en } from '../i18n/en';
import { it as itTranslations } from '../i18n/it';

const now = new Date(2026, 6, 30, 14, 0, 0); // 30 July 2026, local time

describe('formatTryOnDate', () => {
  it('says today for a try-on made earlier the same day', () => {
    const earlierToday = new Date(2026, 6, 30, 1, 0, 0).toISOString();
    expect(formatTryOnDate(earlierToday, en.gallery, now)).toBe('Today');
    expect(formatTryOnDate(earlierToday, itTranslations.gallery, now)).toBe('Oggi');
  });

  it('says yesterday across the calendar boundary, not after 24 hours', () => {
    // Two hours earlier in absolute terms, but a different calendar day.
    const lateLastNight = new Date(2026, 6, 29, 23, 30, 0).toISOString();
    expect(formatTryOnDate(lateLastNight, en.gallery, now)).toBe('Yesterday');
    expect(formatTryOnDate(lateLastNight, itTranslations.gallery, now)).toBe('Ieri');
  });

  it('writes older try-ons the way each locale writes dates', () => {
    const inJune = new Date(2026, 5, 12, 9, 0, 0).toISOString();
    expect(formatTryOnDate(inJune, en.gallery, now)).toBe('Jun 12, 2026');
    expect(formatTryOnDate(inJune, itTranslations.gallery, now)).toBe('12 giu 2026');
  });

  // Same day number, same year, in both languages.
  it('keeps the numbers identical across languages', () => {
    const inJune = new Date(2026, 5, 12, 9, 0, 0).toISOString();
    const english = formatTryOnDate(inJune, en.gallery, now);
    const italian = formatTryOnDate(inJune, itTranslations.gallery, now);
    for (const number of ['12', '2026']) {
      expect(english).toContain(number);
      expect(italian).toContain(number);
    }
  });

  it('returns nothing for an unreadable timestamp instead of showing "Invalid Date"', () => {
    expect(formatTryOnDate('not-a-date', en.gallery, now)).toBe('');
  });
});
