<?php

namespace KupShop\GraphQLBundle\ApiAdmin\Controller;

use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\OrderCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Input\OrderCreateInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Input\OrderFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Input\OrderStornoInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Input\OrderUpdateInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Order;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\Response\OrderMutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiAdmin\Util\OrderUtil;
use Symfony\Contracts\Service\Attribute\Required;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Annotations\UseInputType;

class OrderController
{
    /**
     * @var OrderUtil
     */
    private $orderUtil;

    /**
     * @required
     */
    public function setOrderUtil(OrderUtil $orderUtil): void
    {
        $this->orderUtil = $orderUtil;
    }

    /**
     * Načte objednávku podle zadaného ID.
     *
     * @Query()
     */
    public function order(int $id): ?Order
    {
        return $this->orderUtil->getOrder($id);
    }

    /**
     * Seznam objednávek.
     *
     * @Query()
     *
     * @UseInputType(for="$sort", inputType="OrderSortInput")
     * @UseInputType(for="$filter", inputType="OrderFilterInput")
     */
    public function orders(int $offset = 0, int $limit = 100, ?Parameters $sort = null, ?OrderFilterInput $filter = null): OrderCollection
    {
        return $this->orderUtil->getOrders($offset, $limit, $sort, $filter?->getParameters());
    }

    /**
     * Aktualizuje objednávku.
     *
     * @Mutation()
     */
    public function orderUpdate(OrderUpdateInput $input): OrderMutateResponse
    {
        return $this->orderUtil->updateOrder($input);
    }

    /**
     * Stornuje objednávku.
     *
     * @Mutation()
     */
    public function orderStorno(OrderStornoInput $input): OrderMutateResponse
    {
        return $this->orderUtil->stornoOrder($input);
    }

    /** Vytvoří objednávku */
    #[Mutation]
    #[Module(\Modules::DROPSHIP)]
    public function orderCreate(OrderCreateInput $orderCreateInput): OrderMutateResponse
    {
        return $this->orderUtil->createOrder(
            $orderCreateInput,
        );
    }
}
