<?php

namespace App\Classes\Modules\ControllersLogic\Companies;


use App\Classes\Modules\Companies\Exceptions\CannotListCompanyOrderCategoryException;
use App\Classes\Modules\Companies\Services\ListsCompanyCancelledOrders;
use App\Classes\Modules\Companies\Services\ListsCompanyCompleteOrders;
use App\Classes\Modules\Companies\Services\FetchCompanyOrderCategorySteps;
use App\Classes\Modules\Companies\Services\ListsCompanyOrdersCategory;
use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\ValueObjects\Constants\OrderSteps;

class ListCompanyOrderLogic
{

    /** @var FetchCompanyOrderCategorySteps */
    private $fetchCompanyOrderCategorySteps;

    /** @var ListsCompanyCompleteOrders */
    private $listsCompanyCompleteOrders;

    /** @var ListsCompanyCancelledOrders */
    private $listCompanyCancelledOrders;

    /** @var ListsCompanyOrdersCategory */
    private $listCompanyOrdersCategory;

    /**
     * ListCompanyOrderLogic constructor.
     * @param FetchCompanyOrderCategorySteps $fetchCompanyOrderCategorySteps
     * @param ListsCompanyCompleteOrders $listsCompanyCompleteOrders
     * @param ListsCompanyCancelledOrders $listCompanyCancelledOrders
     * @param ListsCompanyOrdersCategory $listCompanyOrdersCategory
     */
    public function __construct(FetchCompanyOrderCategorySteps $fetchCompanyOrderCategorySteps, ListsCompanyCompleteOrders $listsCompanyCompleteOrders, ListsCompanyCancelledOrders $listCompanyCancelledOrders, ListsCompanyOrdersCategory $listCompanyOrdersCategory)
    {
        $this->fetchCompanyOrderCategorySteps = $fetchCompanyOrderCategorySteps;
        $this->listsCompanyCompleteOrders = $listsCompanyCompleteOrders;
        $this->listCompanyCancelledOrders = $listCompanyCancelledOrders;
        $this->listCompanyOrdersCategory = $listCompanyOrdersCategory;
    }


    /**
     * @param int $id
     * @param string $category
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * @throws InternalServerErrorException
     */
    public function execute(int $id, string $category){
        try {
            switch ($category){
                case OrderSteps::COMPANY_COMPLETE_CATEGORY:
                    return $this->listsCompanyCompleteOrders->execute($id);
                case OrderSteps::COMPANY_CANCEL_CATEGORY:
                    return $this->listCompanyCancelledOrders->execute($id);
                default:
                    return $this->listCompanyOrdersCategory->execute($id, $this->fetchCompanyOrderCategorySteps->execute($category));

            }
        } catch (CannotListCompanyOrderCategoryException $exception){
            throw new InternalServerErrorException('Cannot list company\'s order in category because'.$exception);
        }

    }

}