<?php

declare(strict_types=1);

namespace KupShop\IncomakerBundle\Util\XMLElements;

use Query\Operator;
use Query\QueryBuilder;
use Symfony\Component\HttpFoundation\Request;

class CouponTemplateXMLElement extends AbstractIncomakerXML
{
    protected static string $rootElement = 'couponTemplates';
    protected static string $feedVersion = '3.0';

    protected function createXmlElement(array $item): void
    {
        $this->xmlWriter->startElement('couponTemplate');

        foreach ($item as $key => $value) {
            $this->xmlWriter->writeElement($key, (string) $value);
        }

        $this->xmlWriter->endElement();
    }

    protected function getFilterSpecsByRequest(Request $request): array
    {
        if ($id = $request->query->get('id')) {
            return [Operator::equals(['d.id' => $id])];
        }

        return [];
    }

    protected function getQueryBuilder(): QueryBuilder
    {
        return sqlQueryBuilder()
            ->select('d.id, d.descr AS title, 0 AS reusable')
            ->from('discounts', 'd')
            ->where('d.condition_type = "generate_coupon"')
            ->orderBy('id', 'ASC');
    }
}
