<?php

namespace KupShop\UserBundle\Util;

use Query\Operator;

class UserExporter
{
    public function exportUsers($IDs)
    {
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?> <USERS/>');

        // TODO: TMP: #15311
        $emails = [];

        foreach ($this->getUsers($IDs) as $user) {
            if (!filter_var($user['email'], FILTER_VALIDATE_EMAIL)) {
                continue;
            }
            $emails[$user['email']] = $user['pohoda_id'] ?? '-';
            $this->addUserToXML($xml, $user);
        }

        getLogger()->notice('Pohoda:Users export: '.print_r($emails, true));

        return $xml->asXML();
    }

    protected function addUserToXML(\SimpleXMLElement &$xml, $user)
    {
        $baseElement = $xml->addChild('USER');
        $baseElement->ID = $user['id'];
        $baseElement->EMAIL = $user['email'];
        $baseElement->CURRENCY = $user['currency'];

        $groupsElement = $baseElement->addChild('GROUPS');
        $groups = $this->getGroups($user['id']);
        foreach ($groups as $group) {
            $groupElement = $groupsElement->addChild('GROUP');
            $groupElement->ID = $group['id_group'];
        }

        $invoiceElement = $baseElement->addChild('INVOICE');
        $invoiceElement->NAME = $user['name'];
        $invoiceElement->SURNAME = $user['surname'];
        $invoiceElement->COMPANY = $user['firm'];
        $invoiceElement->ADDRESS = $user['street'];
        $invoiceElement->CITY = $user['city'];
        $invoiceElement->ZIP = $user['zip'];
        $invoiceElement->COUNTRY = $user['country'];
        $invoiceElement->ICO = $user['ico'];
        $invoiceElement->DIC = $user['dic'];
        $invoiceElement->PHONE = $user['phone'];
        $invoiceElement->ADDRESS2 = $user['custom_address'];
        $invoiceElement->STATE = $user['state'];

        $deliveryElement = $baseElement->addChild('DELIVERY');
        $deliveryElement->NAME = $user['delivery_name'];
        $deliveryElement->SURNAME = $user['delivery_surname'];
        $deliveryElement->COMPANY = $user['delivery_firm'];
        $deliveryElement->ADDRESS = $user['delivery_street'];
        $deliveryElement->CITY = $user['delivery_city'];
        $deliveryElement->ZIP = $user['delivery_zip'];
        $deliveryElement->COUNTRY = $user['delivery_country'];
        $deliveryElement->PHONE = $user['delivery_phone'];
        $deliveryElement->ADDRESS2 = $user['delivery_custom_address'];
        $deliveryElement->STATE = $user['delivery_state'];

        $dateElement = $baseElement->addChild('DATE');
        $dateElement->REG = $user['date_reg'];
        $dateElement->SUBSCRIBE = $user['date_subscribe'];
        $dateElement->UNSUBSCRIBE = $user['date_unsubscribe'];

        $this->addAdditionalFields($baseElement, $user);
    }

    private function getGroups($userId)
    {
        return sqlQueryBuilder()
            ->select('*')
            ->from('users_groups_relations')
            ->andWhere(Operator::equals(['id_user' => $userId]))
            ->execute()
            ->fetchAll();
    }

    protected function getUsers($IDs)
    {
        return sqlQueryBuilder()
            ->select('*')
            ->from('users')
            ->andWhere(Operator::inStringArray($IDs, 'id'))
            ->sendToMaster()
            ->execute()
            ->fetchAll();
    }

    protected function addAdditionalFields(&$baseElement, $user)
    {
    }
}
