<?php

/**
 * @copyright © Melograno Ventures. All rights reserved.
 * @licence   See LICENCE.md for license details.
 */

namespace AmeliaBooking\Application\Commands\Report;

use AmeliaBooking\Application\Commands\CommandHandler;
use AmeliaBooking\Application\Commands\CommandResult;
use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
use AmeliaBooking\Domain\Collection\Collection;
use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
use AmeliaBooking\Domain\Entity\Entities;
use AmeliaBooking\Domain\Entity\User\AbstractUser;
use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
use AmeliaBooking\Domain\Services\Report\ReportServiceInterface;
use AmeliaBooking\Domain\Services\Settings\SettingsService;
use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository;
use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings;
use Exception;
use Slim\Exception\ContainerValueNotFoundException;

/**
 * Class GetCustomersCommandHandler
 *
 * @package AmeliaBooking\Application\Commands\Report
 */
class GetCustomersCommandHandler extends CommandHandler
{
    /**
     * @param GetCustomersCommand $command
     *
     * @return CommandResult
     * @throws ContainerValueNotFoundException
     * @throws AccessDeniedException
     * @throws InvalidArgumentException
     * @throws QueryExecutionException
     * @throws Exception
     */
    public function handle(GetCustomersCommand $command)
    {
        if (!$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS)) {
            throw new AccessDeniedException('You are not allowed to read customers.');
        }

        /** @var UserRepository $userRepository */
        $userRepository = $this->container->get('domain.users.repository');
        /** @var CustomerRepository $customerRepository */
        $customerRepository = $this->container->get('domain.users.customers.repository');
        /** @var ReportServiceInterface $reportService */
        $reportService = $this->container->get('infrastructure.report.csv.service');
        /** @var SettingsService $settingsDS */
        $settingsDS = $this->container->get('domain.settings.service');

        $rolesSettings = $settingsDS->getCategorySettings('roles');

        $result = new CommandResult();

        $this->checkMandatoryFields($command);

        $params = $command->getField('params');

        /** @var AbstractUser $currentUser */
        $currentUser = $this->container->get('logged.in.user');

        if (
            $currentUser !== null &&
            $currentUser->getType() === Entities::PROVIDER &&
            empty($rolesSettings['allowReadAllCustomers'])
        ) {
            /** @var Collection $providerCustomers */
            $providerCustomers = $userRepository->getProviderAllowedCustomers(
                $currentUser->getId()->getValue()
            );

            $params['customers'] = empty($params['customers'])
                ? $providerCustomers->keys()
                : array_intersect(
                    array_map('intval', $params['customers']),
                    $providerCustomers->keys()
                );
        }

        $customers = $customerRepository->getFiltered($params, null);

        $rows = [];

        $fields    = $command->getField('params')['fields'];
        $delimiter = $command->getField('params')['delimiter'];

        $dateFormat = $settingsDS->getSetting('wordpress', 'dateFormat');
        $timeFormat = $settingsDS->getSetting('wordpress', 'timeFormat');

        foreach ($customers as $customer) {
            $row = [];

            if (in_array('firstName', $fields, true)) {
                $row[BackendStrings::get('first_name')] = $customer['firstName'];
            }

            if (in_array('lastName', $fields, true)) {
                $row[BackendStrings::get('last_name')] = $customer['lastName'];
            }

            if (in_array('email', $fields, true)) {
                $row[BackendStrings::get('email')] = $customer['email'];
            }

            if (in_array('phone', $fields, true)) {
                $row[BackendStrings::get('phone')] = $customer['phone'];
            }

            if (in_array('gender', $fields, true)) {
                $row[BackendStrings::get('gender')] = $customer['gender'];
            }

            if (in_array('birthday', $fields, true)) {
                $row[BackendStrings::get('date_of_birth')] = $customer['birthday'] ?
                    DateTimeService::getCustomDateTimeObject($customer['birthday'])
                        ->format($dateFormat) : null;
            }

            if (in_array('note', $fields, true)) {
                $row[BackendStrings::get('customer_note')] = $customer['note'];
            }

            if (in_array('lastBooking', $fields, true)) {
                $row[BackendStrings::get('last_booking')] =
                    !empty($customer['lastBooking']) ?
                        DateTimeService::getCustomDateTimeObject(
                            $customer['lastBooking']
                        )->format($dateFormat . ' ' . $timeFormat) :
                        '';
            }

            if (in_array('totalBookings', $fields, true)) {
                $row[BackendStrings::get('total_bookings')] = $customer['totalBookings'];
            }

            if (in_array('pendingAppointments', $fields, true)) {
                $row[BackendStrings::get('pending_appointments')] =
                    $customer['countPendingAppointments'];
            }

            $row = apply_filters('amelia_before_csv_export_customers', $row, $customer);

            $rows[] = $row;
        }

        $reportService->generateReport($rows, Entities::CUSTOMERS, $delimiter);

        $result->setAttachment(true);

        return $result;
    }
}
