<?php

namespace App\Http\Controllers\Imports;

use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Imports\Services\ImportsDebtor;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Excel as ExcelFileTypes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;

class ImportUpdateDebtorController
{
    /**
     * @param Request $request
     * @return array
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function import(Request $request) {
        $object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports');
        $file = json_decode($object->getFiles()[0])->file_info->original->file;

        Log::info('ImportUpdateDebtorController file: ' . $file);

        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            Excel::import(new ImportsDebtor(), $file, 's3');
        }
        else{
            $file = 'documents/' . $file;
            Excel::import(new ImportsDebtor(), $file);
        }

        return [];
    }
}
