<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\SegmentConstants\Services\FetchesSegmentConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;

use App\Models\District;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\General\Helper;

class ShippingEstimationCalculatorLogic extends AbstractControllerLogic
{

    /** @var FetchesSegmentConstant */
    private $fetchesSegmentConstant;

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Shipping estimation',
            'message' => 'You have successfully calculate the Shipping Estimation'
        ];
    }

    public function __construct(
        FetchesSegmentConstant $fetchesSegmentConstant
    ){
        $this->fetchesSegmentConstant = $fetchesSegmentConstant;
    }

    public function logic(Request $request) : JsonResponse
    {
        $base_price =  $warehouse_rate =  $state_rate = 0;

        $width = $request->input('width') / 100;    // metre
        $length = $request->input('length') / 100;  // metre
        $height = $request->input('height') / 100;  // metre

        $cbm = (float) ($width * $length * $height);

        // calculate base price
        $base_prices = $this->getSegmentConstantData(SegmentConstants::BASE_PRICE);
        $base_price  = $this->getConstantDataUsingKey($base_prices, date('Y-m-d'));  

        // calculate warehouse rate
        $warehouse_prices = $this->getSegmentConstantData(SegmentConstants::WAREHOUSE_RATE);
        $warehouse_rate  = $this->getConstantDataUsingKey($warehouse_prices, $request->input('warehouse_id'), 'amount'); 

        // calculate applied state rate
        $state_prices = $this->getSegmentConstantData(SegmentConstants::STATE_RATE);
        $outstation_postcodes = $this->getSegmentConstantData(SegmentConstants::OUTSTATION_POSTCODE);

        $district = District::where('postcode','LIKE','%'.$request->input('postcode').'%')->first();
        if(array_search($request->input('postcode'), $outstation_postcodes)!==false){
            $state_rate = $this->getConstantDataUsingKey($state_prices, $district->state_id, 'outstation'); 
        }

        $price_cbm = $base_price + $warehouse_rate + $state_rate;
        $total_price_cbm = $price_cbm * $cbm;

        return $this->response([
            'cbm' => $cbm,  'base_price' => $base_price, 'warehouse_rate' => $warehouse_rate,
            'state_rate' => $state_rate, 'total_price_cbm' => $total_price_cbm
        ]);
    }

    private function getConstantDataUsingKey($data, $search_key, $field='')
    {
        if(array_key_exists($search_key, $data) !== true) {
            return 0;
        } else {
            if($field!=''){
                return $data[$search_key]->$field;
            } else {
                return $data[$search_key];
            }
        }
    }

    private function getSegmentConstantData($reference)
    {
        $data = $this->fetchesSegmentConstant->execute(['segment_id' => 1, 'reference' => $reference]);
        return (array) $data->value;
    }

}
