<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
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\Company;
use App\Models\Document;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateIdentificationDocumentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Identification Document',
            'message' => 'You have successfully created a new identification document'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

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

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

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


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

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {

        /** @var Company $company */
        $company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
        $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->createsFile->execute($document, $object);

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

        return $this->response([]);
    }

}