<?php

declare(strict_types=1);

namespace KupShop\BonusProgramBundle\View\Exchange;

use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Views\View;
use Query\Operator;

class BonusProgramCouponView extends View
{
    protected string $smartyFallback = 'account';
    protected string $entrypoint = 'account';
    protected $template = 'bonus-program/bonus-program.coupon-list.tpl';

    public function getTitle()
    {
        return translate('couponTitle', 'bonus_program');
    }

    public function getBodyVariables()
    {
        $vars = parent::getBodyVariables();

        $vars['couponList'] = $this->getCouponList();

        return $vars;
    }

    private function getCouponList(): array
    {
        return sqlQueryBuilder()
            ->select('d.descr as name, dc.*')
            ->from('discounts_coupons', 'dc')
            ->join('dc', 'discounts', 'd', 'd.id = dc.id_discount')
            ->where(Operator::equals(['dc.id_user' => Contexts::get(UserContext::class)->getActiveId()]))
            ->orderBy('dc.date_activated', 'DESC')
            ->execute()->fetchAllAssociative();
    }
}
