import { describe, expect, it } from 'vitest';
import {
  toFrameEntryMutationDto,
  toFrameEntryViewModel,
} from '@/business/frame/mappers';
import type { EditableFrameEntry } from '@/business/frame/types';
import type { FrameEntryDto } from '@/shared/types/frame';

function dto(overrides: Partial<FrameEntryDto> = {}): FrameEntryDto {
  return {
    id: 'frame-1',
    week_of: '2026-06-07', // a Sunday (canonical week start)
    week_label: 'Spring Break week',
    posted_date: '2026-06-08',
    formal: 'Formal note',
    recognition: 'Recognition note',
    application: 'Application note',
    management: 'Management note',
    emotional: 'Emotional note',
    author: 'Director',
    organizationId: 'org-1',
    campusId: 'campus-1',
    createdAt: '2026-06-08T10:00:00.000Z',
    updatedAt: '2026-06-08T10:00:00.000Z',
    ...overrides,
  };
}

describe('frame mappers', () => {
  it('maps the DTO into the view model (ISO week start + display + label)', () => {
    expect(toFrameEntryViewModel(dto())).toMatchObject({
      id: 'frame-1',
      weekStart: '2026-06-07',
      weekLabel: 'Spring Break week',
      weekOf: 'June 7, 2026',
      formal: 'Formal note',
      author: 'Director',
    });
  });

  it('defaults a null label to an empty string', () => {
    expect(toFrameEntryViewModel(dto({ week_label: null })).weekLabel).toBe('');
  });

  it('maps editable state back into the mutation DTO and omits a blank label', () => {
    const entry: EditableFrameEntry = {
      weekStart: '2026-06-07',
      weekLabel: '   ',
      postedDate: '2026-06-09',
      formal: 'Formal',
      recognition: 'Recognition',
      application: 'Application',
      management: 'Management',
      emotional: 'Emotional',
      author: 'Office Manager',
    };

    expect(toFrameEntryMutationDto(entry)).toEqual({
      week_of: '2026-06-07',
      week_label: undefined,
      posted_date: '2026-06-09',
      formal: 'Formal',
      recognition: 'Recognition',
      application: 'Application',
      management: 'Management',
      emotional: 'Emotional',
      author: 'Office Manager',
    });
  });

  it('trims and keeps a non-blank label', () => {
    const entry: EditableFrameEntry = {
      weekStart: '2026-06-07',
      weekLabel: '  Holiday week  ',
      postedDate: '2026-06-09',
      formal: 'a',
      recognition: 'b',
      application: 'c',
      management: 'd',
      emotional: 'e',
      author: 'Director',
    };
    expect(toFrameEntryMutationDto(entry).week_label).toBe('Holiday week');
  });
});
