<?php

namespace Modules\PrintSuppliers\Controllers;

use Illuminate\Http\Request;
use Modules\FinancialStatement\Services\PaypalService;
use Modules\PrintSuppliers\Services\ChrisXService;
use Modules\PrintSuppliers\Services\CustomCatService;
use Modules\PrintSuppliers\Services\GelatoService;
use Modules\PrintSuppliers\Services\LuxuryProService;
use Modules\PrintSuppliers\Services\MerchizeService;
use Modules\PrintSuppliers\Services\PrintifyService;
use Modules\PrintSuppliers\Services\TezzilyService;
use Modules\PrintSuppliers\Services\ToadditService;
use Modules\PrintSuppliers\Services\YiwangService;
use Modules\PrintSuppliers\Services\YoycolService;


class ReportCostController extends Controller
{

    public function report(Request $request) {
        $filter = $this->buildFilterReport($request);
        $result['jet'] = 0;
        $result['printLogictics'] = 0;
        //1k + 9367,85 printblur
        $result['printify'] = $this->reportPrintify($filter);
        //lech 8$
        $result['merchize'] = $this->reportMerchize($filter);
        //125 eur
        $result['gelato'] = $this->reportGelato($filter);
        $result['gelato'] = PaypalService::convertToUsd($result['gelato'], 'EUR');
        //+3
        $result['yiwang'] = $this->reportYiwang($filter);
        //0
        $result['luxuryPro'] = $this->reportLuxuryPro($filter);
        //0 (lệch 2105 do tháng 9 + vào)
        $result['chrisX'] = $this->reportChrisX($filter);
        //0
        $result['tezzily'] = $this->reportTezzily($filter);
        //0
        $result['yoycol'] = $this->reportYoycol($filter);
        //4$
        $result['toaddit'] = $this->reportToaddit($filter);
        //40.68
        $result['customcat'] = $this->reportCustomCat($filter);
        //$result['printLogictics'] = $this->reportPrintLogictics($filter);
        $result['total_usd'] = array_sum($result);
        $result['total_vnd'] = PaypalService::convertVND($result['total_usd'], 'VND');
        $result['total'] = $result['total_vnd'];
        $result['additional_cost'] = $this->reportAdditionalCost($filter);
        $result['additional_ads_cost'] = $this->reportAdsCost($filter);
        return response()->json([
            'status' => 'sucessful',
            'result' => $result,
        ]);
    }

    protected function reportAdsCost($filter) {
        $fromDate =  date('Y-m', strtotime($filter['transaction_from']));
        $day =  date('j', strtotime($filter['transaction_to']));
        $result = ceil(1500000000/30) * $day;
        if ($fromDate == '2023-10' || $fromDate == '2023-11') {
            $result = 580000000;
        } else if ($fromDate == '2024-01') {
            $result = 1600000000;
        }
        return $result;

    }

    protected function reportAdditionalCost($filter) {
        $fromDate =  date('Y-m', strtotime($filter['transaction_from']));
        $result = 0;
        if ($fromDate == '2023-10') {
            $result += 444500 + 357;
            $result = PaypalService::convertVND($result, 'VND');
        }  else if ($fromDate == '2023-11') {
            $result = 0;
        }
        return $result;

    }



    protected function reportPrintify($filter) {
        $charge = PrintifyService::getCharge($filter);
        $refund = PrintifyService::getRefund($filter);
        $bonus = PrintifyService::getBonus($filter);

        return $charge - $refund - $bonus;

    }

    protected function reportMerchize($filter) {
        $charge = MerchizeService::getCharge($filter);
        $refund = MerchizeService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportGelato($filter) {
        $charge = GelatoService::getCharge($filter);
        $refund = GelatoService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportPrintLogictics($filter) {
        return 445000;
    }

    protected function reportCustomCat($filter) {
        $charge = CustomCatService::getCharge($filter);
        $refund = CustomCatService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportToaddit($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = ToadditService::getCharge($filter);
        $refund = ToadditService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportYoycol($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = YoycolService::getCharge($filter);
        $refund = YoycolService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportTezzily($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = TezzilyService::getCharge($filter);
        $refund = TezzilyService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportChrisX($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = ChrisXService::getCharge($filter);
        $refund = ChrisXService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportLuxuryPro($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = LuxuryProService::getCharge($filter);
        $refund = LuxuryProService::getRefund($filter);
        return $charge - $refund;
    }

    protected function reportYiwang($filter) {
        $filter = $this->filterTransactionDate($filter);
        $charge = YiwangService::getCharge($filter);
        $refund = YiwangService::getRefund($filter);
        return $charge - 0;
    }


    protected function filterTransactionDate($filter) {
        $filter['transaction_from'] = date('Y-m-d', strtotime( $filter['transaction_from']));
        $filter['transaction_to'] = date('Y-m-d', strtotime( $filter['transaction_to']));
        return $filter;
    }


    protected function buildFilterReport($request)
    {
        $dateFrom = $request->input('date_from', date('Y-m-d', time() - 30 * 86400));
        $dateTo = $request->input('date_to', date('Y-m-d', time()));
        return [
            'transaction_from' => date('Y-m-d 00:00:00', strtotime($dateFrom)),
            'transaction_to' => date('Y-m-d 23:59:59', strtotime($dateTo)),
        ];
    }

}