import { TrackName } from '@msfa-enums/track-name.enum';
import { mapStringToSsn } from '@msfa-shared/utils/map-string-to-ssn.util';
import { AvropResponse, DeltagareAvropResponse } from './api/avrop.response.model';
import { PaginationMeta } from './pagination-meta.model';

export interface DeltagareAvrop {
  id: string;
  fullName: string;
  tjanst: string;
  startDate: Date;
  endDate: Date;
  originalEndDate: Date;
  sprakstod: string;
  tolkbehov: string;
  utforandeVerksamhet: string;
  utforandeAdress: string;
  trackCode: string;
  trackName: TrackName;
  genomforandeReferens: number;
  participationFrequency: number;
  handledareCiamUserId: string;
  handledare: string;
  receivedTimestamp: Date;
  hasGemensamPlanering: boolean;
  hasAvbrott: boolean;
  kommun: string;
  ssn: string;
}

export interface Avrop {
  id: string;
  fullName: string;
  tjanst: string;
  startDate: Date;
  endDate: Date;
  sprakstod: string;
  tolkbehov: string;
  utforandeAdress: string;
  kommun: string;
  trackName: TrackName;
  genomforandeReferens: number;
}

export interface AvropAndMeta {
  data: Avrop[];
  meta: PaginationMeta;
}

export function mapResponseToAvrop(data: AvropResponse): Avrop {
  const {
    id,
    deltagare,
    tjansteNamn,
    startdatumAvrop,
    slutdatumAvrop,
    sprakstod,
    tolkbehov,
    adress,
    kommun,
    sparkod,
    genomforandeReferens,
  } = data;

  return {
    id,
    fullName: deltagare,
    tjanst: tjansteNamn,
    startDate: new Date(startdatumAvrop),
    endDate: new Date(slutdatumAvrop),
    sprakstod,
    tolkbehov,
    utforandeAdress: adress,
    kommun,
    trackName: (TrackName[sparkod] || TrackName.UNKNOWN) as TrackName,
    genomforandeReferens,
  };
}

export function mapResponseToDeltagareAvrop(data: DeltagareAvropResponse): DeltagareAvrop {
  const {
    id,
    fullName,
    tjanst,
    startDate,
    endDate,
    originalEndDate,
    sprakstod,
    tolkbehov,
    utforandeAdress,
    utforandeVerksamhet,
    sparkod,
    genomforandeReferens,
    participationFrequency,
    receivedTimestamp,
    handledareCiamUserId,
    handledare,
    hasAvbrott,
    hasGemensamPlanering,
    kommun,
    personnummer,
  } = data;

  return {
    id,
    fullName,
    tjanst,
    startDate: new Date(startDate),
    endDate: new Date(endDate),
    originalEndDate: new Date(originalEndDate),
    tolkbehov,
    sprakstod,
    utforandeVerksamhet,
    utforandeAdress,
    kommun: kommun || '',
    trackCode: sparkod,
    trackName: (TrackName[sparkod] || TrackName.UNKNOWN) as TrackName,
    ssn: mapStringToSsn(personnummer),
    genomforandeReferens,
    participationFrequency,
    handledareCiamUserId,
    handledare,
    receivedTimestamp: new Date(receivedTimestamp),
    hasAvbrott,
    hasGemensamPlanering,
  };
}
