<?php

namespace App\Classes\Modules\Vouchers\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Vouchers\Services\ListsVoucherCampaigns;
use App\Classes\Modules\Vouchers\Standards\Rules\CanListVoucherCampaigns;
use App\Http\Resources\VoucherCampaignResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListVoucherCampaignsLogic extends AbstractControllerLogic
{
    /** @var ListsVoucherCampaigns */
    private $listsVoucherCampaigns;

    /** @var CanListVoucherCampaigns */
    private $canListVoucherCampaigns;

    /**
     * ListVoucherCampaignsLogic constructor.
     * @param ListsVoucherCampaigns $listsVoucherCampaigns
     * @param CanListVoucherCampaigns $canListVoucherCampaigns
     */
    public function __construct(ListsVoucherCampaigns $listsVoucherCampaigns, CanListVoucherCampaigns $canListVoucherCampaigns)
    {
        $this->listsVoucherCampaigns = $listsVoucherCampaigns;
        $this->canListVoucherCampaigns = $canListVoucherCampaigns;
    }

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved Voucher Campaigns',
            'message' => 'You have successfully retrieved a list of voucher campaigns'
        ];
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $this->canListVoucherCampaigns->passes();

        $query = $this->listsVoucherCampaigns->execute($this->listsVoucherCampaigns->deserializeFilters($request->input('filters')));

        $filters = json_decode($request->input('filters'), true);
        if(isset($filters['include_metadata'])){
            $request->merge(['include_metadata' => true]);
        }

        return $this->collectionResponse(VoucherCampaignResource::collection($query));
    }

}
