import { FranvaroResponse } from './api/franvaro-response.model';
import { FranvaroReason } from './franvaro-reason.model';

export interface Franvaro {
  reason: string;
  date: Date;
  wholeDay: boolean;
  startTime: string;
  endTime: string;
  expectedPresenceStartTime: string;
  expectedPresenceEndTime: string;
  otherKnownReason: string;
  knownReasonComment: string;
  motivation?: string;
  status?: string;
}

export interface FranvaroReport {
  genomforandeReferens: number;
  franvaro: Franvaro;
}

export function mapResponseToFranvaro(
  data: FranvaroResponse,
  reasons: FranvaroReason[],
  otherKnownReasons: FranvaroReason[],
  motivation?: string,
  status?: string
): Franvaro {
  const { avvikelseOrsaksKod, datum, heldag, startTid, slutTid, alternativForKandaOrsaker, forvantadNarvaro } = data;

  return {
    reason: reasons.find(reason => reason.value.toString() === avvikelseOrsaksKod).name,
    date: new Date(datum),
    wholeDay: heldag,
    startTime: startTid,
    endTime: slutTid,
    otherKnownReason: alternativForKandaOrsaker
      ? otherKnownReasons.find(reason => reason.value.toString() === alternativForKandaOrsaker.typ).name
      : null,
    knownReasonComment: alternativForKandaOrsaker?.motivering || null,
    expectedPresenceStartTime: forvantadNarvaro.startTid,
    expectedPresenceEndTime: forvantadNarvaro.slutTid,
    motivation,
    status,
  };
}
