import { EmployeeDispoLiveSource } from '@prisma/client';

import { isValidDate } from 'src/utils';
import { EmployeeDispoLivePreStaging } from '../staging.interface';
import {
  parseDateTime,
  pick,
  transformProperties,
} from '../transformer.functions';

export class DispoliveEmployeeTransformer {
  public static transform(
    dispoLiveEmployeeSource: EmployeeDispoLiveSource,
  ): EmployeeDispoLivePreStaging {
    const dispoLiveEmployeeParsed = {
      ...dispoLiveEmployeeSource,
      ...transformProperties(parseDateTime('yyyy-MM-dd'))(
        pick(['pScheinAblaufDatum'])(dispoLiveEmployeeSource),
      ),
    };

    const {
      id,
      name,
      surname,
      importPersId,
      pScheinAblaufDatum,
      qualifikation,
    } = dispoLiveEmployeeParsed;

    return {
      id,
      name,
      surname,
      personnelNumber: importPersId,
      personTransportCertificate: isValidDate(new Date(pScheinAblaufDatum))
        ? pScheinAblaufDatum
        : undefined,
      qualification: qualifikation,
    } as unknown as EmployeeDispoLivePreStaging;
  }
}
