import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { format, Interval } from 'date-fns';
import { ConfigService } from 'src/core/config/config.service';
import {
  FahrzeugItemDto,
  FahrzeugplanungItemDto,
  TourSummaryDto,
} from './source.interface';

@Injectable()
export class DispoLiveApiExtractorsService {
  constructor(
    private readonly http: HttpService,
    private readonly config: ConfigService,
  ) {}

  toursFindByRange(interval: Interval) {
    const endpoint = 'custom/open-api/fahrberichte/findByRange';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}/${format(new Date(interval.start), 'yyyy-MM-dd')}_${format(new Date(interval.end), 'yyyy-MM-dd')}`;

    return this.http.get<TourSummaryDto[]>(url, {
      headers: {
        Authorization: `Bearer ${this.config.dispoliveApiKey()}`,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
      },
    });
  }

  fahrzeuge() {
    const endpoint = 'custom/open-api/fahrzeuge/all';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}`;

    return this.http.get<FahrzeugItemDto[]>(url, {
      headers: {
        Authorization: `Bearer ${this.config.dispoliveApiKey()}`,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
      },
    });
  }

  fahrzeugplanungFindByRange(interval: Interval) {
    const endpoint = 'custom/open-api/fahrzeugplanung/findByRange';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}/${format(new Date(interval.start), 'yyyy-MM-dd')}_${format(new Date(interval.end), 'yyyy-MM-dd')}`;

    return this.http.get<FahrzeugplanungItemDto[]>(url, {
      headers: {
        Authorization: `Bearer ${this.config.dispoliveApiKey()}`,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
      },
    });
  }

  fahrzeugplanungAdd(item: FahrzeugplanungItemDto) {
    const endpoint = 'custom/open-api/fahrzeugplanung/add';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}`;
    const authToken = `Bearer ${this.config.dispoliveApiKey()}`;

    return this.http.post<FahrzeugplanungItemDto[]>(url, item, {
      headers: {
        Authorization: authToken,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
        'Content-Type': 'application/json',
      },
    });
  }

  fahrzeugplanungUpdate(item: FahrzeugplanungItemDto) {
    const endpoint = 'custom/open-api/fahrzeugplanung/update';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}`;

    return this.http.post<FahrzeugplanungItemDto[]>(url, item, {
      headers: {
        Authorization: `Bearer ${this.config.dispoliveApiKey()}`,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
        'Content-Type': 'application/json',
      },
    });
  }

  fahrzeugplanungDelete(id: string) {
    const endpoint = 'custom/open-api/fahrzeugplanung/delete';
    const url = `${this.config.dispoliveBaseUrl()}/${endpoint}/${id}`;

    return this.http.get<FahrzeugplanungItemDto[]>(url, {
      headers: {
        Authorization: `Bearer ${this.config.dispoliveApiKey()}`,
        'dispolive-api-version': this.config.dispoliveApiVersion(),
      },
    });
  }
}
