<?php

declare(strict_types=1);

namespace ACA\WC\ColumnFactory\Product;

use AC\FormatterCollection;
use AC\Setting\ComponentCollection;
use AC\Setting\Config;
use AC\Setting\DefaultSettingsBuilder;
use ACA\WC\ColumnFactory\WooCommerceGroupTrait;
use ACA\WC\ConditionalFormat\Formatter\Product\OrderTotal;
use ACA\WC\Search;
use ACA\WC\Setting\ComponentFactory\Order\OrderStatusesFactory;
use ACA\WC\Setting\ComponentFactory\PeriodWithAllTime;
use ACA\WC\Sorting;
use ACA\WC\Value\Formatter;
use ACP;
use ACP\Column\FeatureSettingBuilderFactory;
use ACP\ConditionalFormat\FormattableConfig;
use ACP\Search\Comparison;

class OrderTotalFactory extends ACP\Column\AdvancedColumnFactory
{

    use ACP\ConditionalFormat\IntegerFormattableTrait;
    use WooCommerceGroupTrait;

    private OrderStatusesFactory $order_statuses_factory;

    private PeriodWithAllTime $period;

    public function __construct(
        FeatureSettingBuilderFactory $feature_settings_builder_factory,
        DefaultSettingsBuilder $default_settings_builder,
        OrderStatusesFactory $order_statuses_factory,
        PeriodWithAllTime $period
    ) {
        parent::__construct($feature_settings_builder_factory, $default_settings_builder);
        $this->order_statuses_factory = $order_statuses_factory;
        $this->period = $period;
    }

    protected function get_settings(Config $config): ComponentCollection
    {
        return parent::get_settings($config)
                     ->add($this->order_statuses_factory->create(['wc-completed'])->create($config))
                     ->add($this->period->create($config));
    }

    public function get_column_type(): string
    {
        return 'column-wc-total_order_amount';
    }

    public function get_label(): string
    {
        return __('Total Revenue', 'codepress-admin-columns');
    }

    public function get_description(): ?string
    {
        return __('Total revenue generated by this product across all matching orders.', 'codepress-admin-columns');
    }

    protected function get_formatters(Config $config): FormatterCollection
    {
        return parent::get_formatters($config)
                     ->add($this->create_formatter($config))
                     ->add(new Formatter\WcPrice());
    }

    protected function get_sorting(Config $config): ?ACP\Sorting\Model\QueryBindings
    {
        return new Sorting\Product\Order\OrderTotal(
            (array)$config->get('order_status', ['wc-completed']),
            $this->get_period_in_days($config)
        );
    }

    protected function get_search(Config $config): ?Comparison
    {
        return new Search\Product\Order\OrderTotal(
            (array)$config->get('order_status', ['wc-completed']),
            $this->get_period_in_days($config)
        );
    }

    protected function get_export(Config $config): ?FormatterCollection
    {
        return FormatterCollection::from_formatter($this->create_formatter($config));
    }

    protected function get_conditional_format(Config $config): ?FormattableConfig
    {
        return new FormattableConfig(
            new OrderTotal($this->create_formatter($config))
        );
    }

    private function create_formatter(Config $config): Formatter\Product\OrderTotal
    {
        return new Formatter\Product\OrderTotal(
            (array)$config->get('order_status', ['wc-completed']),
            $this->get_period_in_days($config)
        );
    }

    private function get_period_in_days(Config $config): ?int
    {
        $period = $config->get('period', '');

        if ('' === $period) {
            return null;
        }

        if ('custom' === $period) {
            $custom = (int)$config->get('period_custom', 0);

            return $custom > 0 ? $custom : null;
        }

        return (int)$period;
    }

}
