<?php

namespace App\Classes\Modules\Companies\Services;


use App\Classes\Constants;
use App\Classes\Modules\Documents\DeleteAllDocuments;
use App\Models\Company;
use App\Http\Resources\Company as CompanyResource;

class DeletesCompany
{

    /** @var  Company */
    private $repository;

    /**
     * DeletesCompany constructor.
     * @param Company $repository
     */
    public function __construct(Company $repository)
    {
        $this->repository = $repository;
    }


    public function execute(string $id)
    {

        $company = $this->repository->findOrFail($id);


        foreach (Constants::COMPANY_DOCUMENTS as $documentType) {
            (new DeleteAllDocuments(Constants::MODULE_TYPES['company'], $id, $documentType))->execute();
        }

        if ($company->delete()) {

            // Return single compnay as a resource
            return new CompanyResource($company);

        }
    }

}