import { FrameEntryDto, FrameEntryMutationDto } from '@/shared/types/frame';
import { EditableFrameEntry, FrameEntryViewModel } from '@/business/frame/types';

function formatDisplayDate(isoDate: string): string {
  const date = new Date(isoDate);

  if (Number.isNaN(date.getTime())) {
    return isoDate;
  }

  return date.toLocaleDateString('en-US', {
    month: 'long',
    day: 'numeric',
    year: 'numeric',
  });
}

export function toFrameEntryViewModel(dto: FrameEntryDto): FrameEntryViewModel {
  return {
    id: dto.id,
    weekOf: formatDisplayDate(dto.week_of),
    postedDate: formatDisplayDate(dto.posted_date),
    formal: dto.formal,
    recognition: dto.recognition,
    application: dto.application,
    management: dto.management,
    emotional: dto.emotional,
    author: dto.author,
  };
}

export function toFrameEntryMutationDto(entry: EditableFrameEntry): FrameEntryMutationDto {
  return {
    week_of: entry.weekOf,
    posted_date: entry.postedDate,
    formal: entry.formal,
    recognition: entry.recognition,
    application: entry.application,
    management: entry.management,
    emotional: entry.emotional,
    author: entry.author,
  };
}
