<?php

declare(strict_types=1);

namespace KupShop\CoworkersBundle\Admin\Tabs;

use KupShop\AdminBundle\Admin\WindowTab;
use KupShop\KupShopBundle\Util\Functional\Mapping;

class CoworkersWindowTab extends WindowTab
{
    protected $title = 'coworkers';
    protected $template = 'window/settings.coworkers.tpl';

    public static function getTypes()
    {
        return [
            'settings' => 1,
        ];
    }

    public function getLabel()
    {
        return 'Coworkers';
    }

    public function getVars($smarty_tpl_vars)
    {
        $vars = [];

        $vars['order_statuses'] = Mapping::mapKeys(getOrderStatuses(), function ($k, $v) {
            return [$k, $v['name']];
        });

        return $vars;
    }

    public function handleUpdate()
    {
        $voicebot = getVal('voicebot');
        $apiKey = $voicebot['api_key'] ?? '';
        $apiUrl = $voicebot['api_url'] ?? '';
        $instanceId = $voicebot['instance_id'] ?? '';
        $campaignId = $voicebot['campaign_id'] ?? '';
        $days = $voicebot['days'] ?? '';
        $orderStatuses = $voicebot['order_statuses'] ?? [];

        $voicebot = $settings->voicebot ?? [];

        $voicebot['api_key'] = $apiKey;
        $voicebot['api_url'] = $apiUrl;
        $voicebot['instance_id'] = $instanceId;
        $voicebot['campaign_id'] = $campaignId;
        $voicebot['days'] = $days;
        $voicebot['order_statuses'] = $orderStatuses;

        $settings = \Settings::getDefault();
        $settings->saveValue('voicebot', $voicebot);
    }
}
