import { TourSource } from '@prisma/client';
import { isAfter } from 'date-fns';
import { TourPreStaging } from '../staging.interface';
import {
  parseBoolean,
  parseCETDateTime,
  parseDateTime,
  parseFloat,
  parseInteger,
  parseIsoDateTime,
  pick,
  subTimeZone,
  transformProperties,
} from '../transformer.functions';

export class TourTransformer {
  public static transform(tourSource: TourSource): TourPreStaging {
    const tourParsed = {
      ...tourSource,
      // operationId: faker.number.int({ min: 10000, max: 19999 }),
      // driverName: faker.person.fullName(),
      // codriverName: faker.person.fullName(),
      // patientName: faker.person.firstName(),
      // patientSurname: faker.person.lastName(),
      // patientStreet: faker.location.streetAddress(),
      // startInstitution: faker.company.name(),
      // startStreet: faker.location.streetAddress(),
      // targetInstitution: faker.company.name(),
      // targetStreet: faker.location.streetAddress(),
      ...transformProperties(parseInteger)(pick(['check'])(tourSource)),
      ...transformProperties(parseFloat)(
        pick(['besetztKm', 'gesamtPreis'])(tourSource),
      ),
      //! DispoLive changed date_iso from beeing UTC to local time, which in our case is CET
      ...(isAfter(
        new Date(tourSource.date_iso),
        new Date('2024-10-28T00:00:00.000Z'),
      )
        ? transformProperties(subTimeZone)(pick(['date_iso'])(tourSource))
        : transformProperties(parseIsoDateTime)(
            pick(['date_iso'])(tourSource),
          )),
      ...(tourSource.abrDate
        ? transformProperties(parseDateTime('yyyy-MM-dd'))(
            pick(['abrDate'])(tourSource),
          )
        : undefined),
      ...(tourSource.endgen
        ? transformProperties(parseDateTime('yyyy-MM-dd'))(
            pick(['endgen'])(tourSource),
          )
        : undefined),
      ...(tourSource.createTime.length > 10
        ? transformProperties(parseCETDateTime('yyyy-MM-dd HH:mm:ss'))(
            pick(['createTime'])(tourSource),
          )
        : transformProperties(parseCETDateTime('yyyy-MM-dd'))(
            pick(['createTime'])(tourSource),
          )),
      ...(tourSource.lastUpdate
        ? transformProperties(parseCETDateTime('yyyy-MM-dd HH:mm:ss'))(
            pick(['lastUpdate'])(tourSource),
          )
        : undefined),
      ...transformProperties(parseBoolean)(
        pick(['infektion', 'gefahren'])(tourSource),
      ),
    };

    const {
      id,
      gefahren,
      direction,
      carName,
      einsatzNummer,
      fahrer,
      fahrerName,
      beifahrer,
      beifahrerName,
      patId,
      patName,
      patSurname,
      patStreet,
      patZip,
      patCity,
      krankenkasse,
      kkId,
      type,
      serienKategorName,
      transportKrz,
      verordnungsName,
      endgen,
      startInstitution,
      startStreet,
      startZip,
      startCity,
      targetInstitution,
      targetStreet,
      targetZip,
      targetCity,
      date_iso,
      check,
      besetztKm,
      startBegin,
      startEnd,
      ziel,
      zielBegin,
      frei,
      infektion,
      itKrz,
      gesamtPreis,
      abrDate,
      abrNum,
      createTime,
      lastUpdate,
    } = tourParsed;

    return {
      id,
      direction,
      carName,
      startInstitution,
      startStreet,
      startZip,
      startCity,
      targetInstitution,
      targetStreet,
      targetZip,
      targetCity,
      check,
      type,
      done: gefahren,
      operationId: einsatzNummer,
      driverId: fahrer,
      driverName: fahrerName,
      codriverId: beifahrer,
      codriverName: beifahrerName,
      patientId: patId,
      patientName: patName,
      patientSurname: patSurname,
      patientStreet: patStreet,
      patientZip: patZip,
      patientCity: patCity,
      healthInsurance: krankenkasse,
      healthInsuranceNumber: kkId,
      category: serienKategorName,
      transportType: transportKrz,
      ordinanceType: verordnungsName,
      rangeEndDate: endgen,
      startDate: date_iso,
      occupiedKm: besetztKm,
      totalKm: besetztKm,
      startBegin,
      startEnd,
      target: ziel,
      targetBegin: zielBegin,
      empty: frei,
      hasInfection: infektion,
      infectionName: itKrz,
      revenueDispoLive: gesamtPreis,
      billDate: abrDate,
      billNumber: abrNum,
      createdAt: createTime,
      updatedAt: lastUpdate,
      revenue: 0,
      consumptionCosts: 0,
    } as unknown as TourPreStaging;
  }
}
