import axios from 'axios';
import { Injectable } from '@nestjs/common';
import { ConfigurationService } from '../../config/configuration.service';
import { ErrorService } from '../../error/error.service';

@Injectable()
export class AlopeykService {
  private axios;

  constructor(
    private config: ConfigurationService,
    private error: ErrorService,
  ) {
    this.axios = axios.create({
      baseURL:
        config.thirdParty.alopeykMode === 'sandbox'
          ? 'https://sandbox-api.alopeyk.com/api'
          : 'https://api.alopeyk.com/api',
      headers: {
        Authorization: `Bearer ${this.config.thirdParty.alopeykToken}`,
      },
    });
  }

  /////////////////////////////////////////////////////////////////
  async getPrice(
    latOrigin: string,
    lngOrigin: string,
    latDestination: string,
    lngDestination: string,
    transportType = 'motorbike',
  ) {
    try {
      const { data } = await this.axios.post('/v2/orders/price/calc', {
        addresses: [
          {
            type: 'origin',
            lat: latOrigin,
            lng: lngOrigin,
          },
          {
            type: 'destination',
            lat: latDestination,
            lng: lngDestination,
          },
        ],
        transport_type: 'motorbike',
        has_return: false,
        cashed: false,
      });

      return data;
    } catch (e) {
      console.log('Alopeyk catch', e?.response?.data);
      this.error.internalServerError([
        'خطایی در محاسبه هزینه پستی رخ داده است',
      ]);
    }
  }

  /////////////////////////////////////////////////////////////////
  async createOrder(
    latOrigin: string,
    lngOrigin: string,
    latDestination: string,
    lngDestination: string,
    orderId = '',
    personFullnameDestination = '',
    personPhoneDestination = '',
    transportType = 'motorbike',
  ) {
    try {
      const { data } = await this.axios.post('/v2/orders', {
        addresses: [
          {
            type: 'origin',
            lat: latOrigin,
            lng: lngOrigin,
            person_fullname: 'اتوتیک',
            person_phone: '09391857423',
          },
          {
            type: 'destination',
            lat: latDestination,
            lng: lngDestination,
            person_fullname: personFullnameDestination,
            person_phone: personPhoneDestination,
          },
        ],
        transport_type: 'motorbike',
        has_return: false,
        cashed: false,
        extra_params: JSON.stringify({ order_id: orderId }),
      });

      return data;
    } catch (e) {
      console.log('Alopeyk catch', e?.response?.data);
      this.error.internalServerError(['خطایی در سمت الوپیک رخ داده است']);
    }
  }

  /////////////////////////////////////////////////////////////////
  async getDetail(orderId) {
    try {
      const { data } = await this.axios.get(`/v2/orders/${orderId}`);
      return data;
    } catch (e) {
      console.log('Alopeyk catch', e?.response?.data);
      this.error.internalServerError(['خطایی در سمت الوپیک رخ داده است']);
    }
  }

  /////////////////////////////////////////////////////////////////
  async getAll(page = 1, perpage = 15) {
    try {
      const { data } = await this.axios.get(`/v2/orders`, {
        params: { page, perpage },
      });
      return data;
    } catch (e) {
      console.log('Alopeyk catch', e?.response?.data);
      this.error.internalServerError(['خطایی در سمت الوپیک رخ داده است']);
    }
  }

  ////////////////////////////////////////////////////////////////
  async cancelOrder(orderId) {
    try {
      const { data } = await this.axios.get(`/v2/orders/${orderId}/cancel`);
      return data;
    } catch (e) {
      console.log('Alopeyk catch', e?.response?.data);
      this.error.internalServerError(['خطایی در سمت الوپیک رخ داده است']);
    }
  }
}
