<?php

namespace App\Classes\Modules\Documents\Processors;

use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\DocumentType;

use App\Models\Document;
use Illuminate\Http\Request;
use App\Models\Company;

class UploadIdentityDocumentProcessor
{

    /** @var CreatesDocument */
    private $createsDocument;

    /** @var UpdatesCompanyStatus */
    private $updatesCompanyStatus;

    /** @var CreatesFiles */
    private $createsFiles;

    /**
     * UploadIdentityDocumentProcessor constructor.
     * @param CreatesDocument $createsDocument
     * @param UpdatesCompanyStatus $updatesCompanyStatus
     * @param CreatesFiles $createsFiles
     */
    public function __construct(CreatesDocument $createsDocument, UpdatesCompanyStatus $updatesCompanyStatus, CreatesFiles $createsFiles)
    {
        $this->createsDocument = $createsDocument;
        $this->updatesCompanyStatus = $updatesCompanyStatus;
        $this->createsFiles = $createsFiles;
    }


    /**
     * @param Request $request
     * @param Company $company
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Request $request, Company $company) {

        $object = new DocumentObject($company->type === CompanyType::COMPANY_BUSINESS ?
            DocumentType::SSM_REGISTRATION : DocumentType::IDENTITY_CARD, $request->input('files'),
            $request->input('identification_no'), ApprovalStatus::PENDING_VERIFICATION, 'identifications');

        /** @var Document $document */
        $document = $this->createsDocument->execute($company, $object);

        $this->createsFiles->execute($document, $object);

        $this->updatesCompanyStatus->execute($company, ApprovalStatus::PENDING_VERIFICATION);
    }

}