<?php

namespace Modules\PrintSuppliers\Controllers;

use Illuminate\Http\Request;
use Modules\PrintSuppliers\Services\BaseService;
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 TransactionController extends Controller
{


    public function transactionCustomCat(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'customcatexport.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        CustomCatService::storeItems($items);
        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionToaddit(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'Printerval-Toaddit  daily order sheet - daily invoice .csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        ToadditService::storeItems($items);
        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionYoycol(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'Yoycol-Printerval - Daily invoice.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        YoycolService::storeItems($items);

        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionTezzily(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'teezzi2023-08-09-2023-11-09-sZnfyZAWHcWemWfV.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        TezzilyService::storeItems($items);
        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    //@todo
    public function transactionPrintLogictics(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'print_logictics_654ccbf81b827.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        ChrisXService::storeItems($items);

        return [
            'status' => 'successful'
        ];
    }

    public function transactionChrisX(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'Chris x Printerval Orders 3108 - Daily invoice.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        ChrisXService::storeItems($items);

        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionLuxuryPro(Request $request) {
        set_time_limit(2 * 3600);
        $fileName = $request->input('fileName', 'MEG-Fullfill-Linda - Daily invoice.csv');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseCSV($filePath);
        LuxuryProService::storeItems($items);

        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionYiwang(Request $request) {
        set_time_limit(2 * 3600);
        //$file = '/Users/tuanpa/Applications/shopot/resources/printing/Order List.xlsx';
        $fileName = $request->input('fileName', 'yiwangOrder List (8).xlsx');
        $filePath = resource_path('/printing/' . $fileName);
        $items = BaseService::parseExcel($filePath);
        YiwangService::storeItems($items);
        return [
            'status' => 'successful',
            'items' => count($items),
        ];
    }

    public function transactionPrintify(Request $request) {
        set_time_limit(2 * 3600);
        $token = $request->input('token');
        $limit = $request->input('limit', 20000);
        if (!$token) {
            return;
        }
        PrintifyService::handleTransaction($token, $limit);
        return [
            'status' => 'successful',
        ];
    }

    public function transactionGelato(Request $request) {
        set_time_limit(2 * 3600);
        //a1737f53-db23-4986-91cc-0e74474bf732
        //c20bf927-0a91-44a2-8809-b6397e9978c5
        $wallet = $request->input('wallet', 'a1737f53-db23-4986-91cc-0e74474bf732');
        $wallet = $request->input('wallet', 'c20bf927-0a91-44a2-8809-b6397e9978c5');
        $token = $request->input('token');
        if (!$token) {
            return;
        }
        $from = $request->input('from', 4);
        $to = $request->input('to', 0);
        for($day = $from; $day > $to; $day--) {
            $filter = $this->gelatoFilter([
                'from' => $day,
                'to' => $day - 1,
            ]);

            $items = GelatoService::getTransaction($token, $wallet, $filter);
            GelatoService::storeItems($items);
        }
        return [
            'status' => 'successful'
        ];
    }

    protected function gelatoFilter($filter) {
        return [
            'createdAtAfter' => date("Y-m-d\T00:00:00.000\Z", time() - $filter['from'] * 86400),
            'createdAtBefore' => date("Y-m-d\T23:59:59.000\Z", time() - $filter['to'] * 86400),
        ];
    }

    public function merchizeTransaction(Request $request) {
        $token = $request->input('token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MTQ5YWM5ZTdkMjYyNDU2ODZlMzJmNjYiLCJlbWFpbCI6ImZ1bGxmaWxsLnByaW50ZXJ2YWxAZ21haWwuY29tIiwiaWF0IjoxNjk5MDY3MzE3LCJleHAiOjE3MDE2NTkzMTd9.sbUyATN4PTVvD5C-OHX4JTytImvVVz1yheZt40dMS4U');
        if (!$token) {
            return;
        }
        $filter = $this->merchizeFilter($request);
        $items = MerchizeService::getTransaction($token, $filter);
        MerchizeService::storeItems($items);
        return [
            'status' => 'successful'
        ];
    }

    protected function merchizeFilter($request) {
        $from = $request->input('from', 3);
        $to = $request->input('to', 0);
        return [
            'from' => date("d/m/Y", time() - $from * 86400),
            'to' => date("d/m/Y", time() - $to * 86400),
        ];
    }
}