import { AvvikelseResponse } from './api/avvikelse-response.model';
import { AvvikelseQuestion } from './avvikelse-question.model';
import { AvvikelseReason } from './avvikelse-reason.model';

export interface Avvikelse {
  reason: string;
  questions: {
    question: string;
    answer: string;
  }[];
  date: Date;
}

export interface AvvikelseReport {
  genomforandeReferens: number;
  avvikelse: Avvikelse;
}

export function mapResponseToAvvikelse(
  data: AvvikelseResponse,
  reasons: AvvikelseReason[],
  questions: AvvikelseQuestion[]
): Avvikelse {
  const { avvikelseOrsakskod, rapporteringsdatum, frageformular } = data;

  return {
    reason: reasons.find(reason => reason.id.toString() === avvikelseOrsakskod).name,
    questions: frageformular.map(fraga => ({
      question: questions.find(question => question.id === fraga.fraga).name,
      answer: fraga.svar,
    })),
    date: new Date(rapporteringsdatum),
  };
}
