import { describe, expect, it } from 'vitest';
import {
  toPolicyDocumentMutationDto,
  toPolicyViewModel,
} from '@/business/policies/mappers';
import type { PolicyFormInput } from '@/business/policies/types';
import {
  POLICY_DATE_NOT_RECORDED_LABEL,
  POLICY_DEFAULT_CATEGORY,
  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: 'policy-1',
    title: 'Incident Response',
    body: 'Use the approved incident response process.',
    category: 'handbook_policy',
    tag: 'Safety',
    author: 'Dr. Williams',
    steps: null,
    autism_considerations: null,
    version: 2,
    active: true,
    organizationId: 'org-1',
    campusId: null,
    createdAt: '2026-06-07T08:00:00.000Z',
    updatedAt: '2026-06-08T09:30:00.000Z',
    ...overrides,
  };
}

describe('policy mappers', () => {
  it('maps a policy_documents row (handbook_policy) into the view model; tag → category', () => {
    expect(toPolicyViewModel(dto())).toEqual({
      id: 'policy-1',
      title: 'Incident Response',
      category: 'Safety',
      content: 'Use the approved incident response process.',
      lastUpdated: '2026-06-08',
      updatedBy: 'Dr. Williams',
    });
  });

  it('defaults missing/invalid fields into explicit view model values', () => {
    expect(
      toPolicyViewModel(
        dto({ title: '', body: null, tag: 'Unknown Tag', author: null, updatedAt: '' }),
      ),
    ).toEqual({
      id: 'policy-1',
      title: '',
      category: POLICY_DEFAULT_CATEGORY,
      content: '',
      lastUpdated: POLICY_DATE_NOT_RECORDED_LABEL,
      updatedBy: POLICY_UPDATED_BY_LABEL,
    });
  });

  it('maps form input back into the policy_documents mutation DTO (category=handbook_policy, tag=sub-category)', () => {
    const input: PolicyFormInput = {
      title: '  Communication Standards  ',
      category: 'Communication',
      content: '  Use plain language with families.  ',
    };

    expect(toPolicyDocumentMutationDto(input)).toEqual({
      title: 'Communication Standards',
      category: POLICY_DOCUMENT_PAGE_CATEGORY.handbookPolicies,
      tag: 'Communication',
      body: 'Use plain language with families.',
    });
  });
});
