<?php

namespace App\Classes\Modules\Companies\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Processors\CreateCompanyProcessor;
use App\Classes\Modules\Companies\Processors\CreateCompanyModuleProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Http\Resources\CompanyResource;
use App\Models\Company;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateCompanyLogic extends AbstractControllerLogic
{

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


    /** @var CreateCompanyProcessor  */
    private $createCompanyProcessor;

    /** @var CreateCompanyModuleProcessor  */
    private $createCompanyModuleProcessor;

    /**
     * CreateCompanyLogic constructor.
     * @param CreateCompanyProcessor $createCompanyProcessor
     * @param CreateCompanyModuleProcessor $createCompanyModuleProcessor
     */
    public function __construct(CreateCompanyProcessor $createCompanyProcessor, CreateCompanyModuleProcessor $createCompanyModuleProcessor)
    {
        $this->createCompanyProcessor = $createCompanyProcessor;
        $this->createCompanyModuleProcessor = $createCompanyModuleProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        /** @var Company $company */
        $company = $this->createCompanyProcessor->execute($request->input('type') === CompanyType::COMPANY_BUSINESS ? $request->input('company_name') : $request->input('name'), $request->input('type'), ApprovalStatus::PENDING_SUBMISSION);

        $this->createCompanyModuleProcessor->execute($company , $request->input('business_type'));

        return $this->resourceResponse(new CompanyResource($company));
    }

}
