<?php

namespace App\Http\Controllers\Admin;

use App\CampaignsJobProvider;
use App\Http\Controllers\Controller;
use App\Imports\BulkPaymentFile;
use App\Imports\HypothecationBulkUpload;
use App\Slack;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

class HypothecationController extends Controller
{
    public function index(){
        return view('admin.hypothecation.index');
    }

    public function import(Request $request){
        try {

            if ($request->has('file')) {
        
                if($request->files->get('file') != null) {
                    if(filesize($request->files->get('file')) > 18874368){
                    Session::flash('error', 'Uploaded file is too large');
                    return redirect()->back();
                    }
                }
                
            }
            $date = Carbon::now()->toDateTimeString();
            $name = str_replace(' ','_', $date).'_'.time().'_hypothecation_processed.'.$request->file('file')->getClientOriginalExtension();
            $file_path = $this->saveReceiptAck($request->file('file'), $name);
            $file_path_local = storage_path('cron_hypothecation');
            $file = $request->file('file');
            $file->move($file_path_local, $name);
            $data = $request->all();
            $data['file_path'] = $file_path;
            $data['file_name'] = $name;
            $jobs = new CampaignsJobProvider();
            $jobs->from = 'hypothecation_data_upload';
            $jobs->payload = json_encode($data);
            $jobs->available_at = $date;
            $jobs->application_id = 181;
            $jobs->created_at = $date;
            $jobs->is_available = 0;
            $jobs->save();

            if (!file_exists(storage_path().'/cron_hypothecation/'.$name)) {
                mkdir(storage_path().'/cron_hypothecation/'.$name, 0777, true);
            }

            Session::flash('success', 'You we\'ll notify you once all the data are processed');
            return redirect()->back();
        }catch (\Exception $e){
            Session::flash('error', 'Something went wrong, '.$e->getFile().$e->getLine());
            return redirect()->back();
        }
    }

    public function startFileProcess(Request $request){
        try {
            $request->attributes->add(['file_path' => $request->get('file_path')]);

            Excel::import(new HypothecationBulkUpload($request), $request->get('file_path'), 's3');

        }catch (\Exception $e){
            (new Slack(new Request(['title' => 'Hypothecation file can\'t be processed', 'fields' => ['message' => $e->getMessage(), 'line' => $e->getLine(),'file' => $e->getFile()], 'group' => Slack::$STAGE_TESTING])))->send();
        }

    }

    private function saveReceiptAck($receipt_ack, $fileName){
        $s3 = Storage::disk('s3');
        $file_path = '/hypothecation/'.$fileName;
        $s3->put($file_path, $receipt_ack);
        $files = $s3->listContents($file_path);
        return $files[0]['path'];
    }
}