import { describe, expect, it } from 'vitest';
import {
  toSafetyProtocolMutationDto,
  toSafetyProtocolViewModel,
} from '@/business/safety-protocols/mappers';
import type { SafetyProtocolFormInput } from '@/business/safety-protocols/types';
import {
  POLICY_DATE_NOT_RECORDED_LABEL,
  POLICY_DOCUMENT_PAGE_CATEGORY,
  POLICY_UPDATED_BY_LABEL,
} from '@/shared/constants/policies';
import type { PolicyDocumentDto } from '@/shared/types/policyDocuments';

function dto(overrides: Partial<PolicyDocumentDto> = {}): PolicyDocumentDto {
  return {
    id: 'safety-1',
    title: 'Fire Drill Procedures',
    body: null,
    category: 'safety_protocol',
    tag: 'fire',
    author: 'Dr. Sarah Williams',
    steps: ['Hear alarm', 'Line up', 'Walk to assembly point'],
    autism_considerations: ['Use visual card', 'Offer headphones'],
    version: 1,
    active: true,
    organizationId: 'org-1',
    campusId: 'campus-1',
    createdAt: '2026-06-07T08:00:00.000Z',
    updatedAt: '2026-06-08T09:30:00.000Z',
    ...overrides,
  };
}

describe('safety protocol mappers', () => {
  it('maps a safety_protocol document into the view model (steps + considerations)', () => {
    expect(toSafetyProtocolViewModel(dto())).toEqual({
      id: 'safety-1',
      title: 'Fire Drill Procedures',
      tag: 'fire',
      steps: ['Hear alarm', 'Line up', 'Walk to assembly point'],
      autismConsiderations: ['Use visual card', 'Offer headphones'],
      version: 1,
      lastUpdated: '2026-06-08',
      author: 'Dr. Sarah Williams',
    });
  });

  it('defaults null arrays/author/date to empty values', () => {
    expect(
      toSafetyProtocolViewModel(
        dto({ steps: null, autism_considerations: null, author: null, updatedAt: '' }),
      ),
    ).toEqual({
      id: 'safety-1',
      title: 'Fire Drill Procedures',
      tag: 'fire',
      steps: [],
      autismConsiderations: [],
      version: 1,
      lastUpdated: POLICY_DATE_NOT_RECORDED_LABEL,
      author: POLICY_UPDATED_BY_LABEL,
    });
  });

  it('maps form input into a safety_protocol mutation DTO (trims, drops blanks)', () => {
    const input: SafetyProtocolFormInput = {
      title: '  Lockdown  ',
      tag: 'lockdown',
      steps: [' Lock door ', '', 'Cover window'],
      autismConsiderations: ['Pre-teach', '  '],
    };
    expect(toSafetyProtocolMutationDto(input)).toEqual({
      title: 'Lockdown',
      category: POLICY_DOCUMENT_PAGE_CATEGORY.safetyProtocols,
      tag: 'lockdown',
      steps: ['Lock door', 'Cover window'],
      autism_considerations: ['Pre-teach'],
    });
  });
});
