<?php

declare(strict_types=1);

namespace ACP\Plugin\Update;

use AC\Plugin\Update;
use AC\Plugin\Version;
use ACP\Search\Middleware\Mapping;
use ACP\Search\Middleware\Mapping\Rule;

class V5201 extends Update
{
    public function __construct()
    {
        parent::__construct(new Version('5.2.1'));
    }

    public function apply_update(): void
    {
        global $wpdb;

        $sql = "
			SELECT *
			FROM {$wpdb->usermeta}
			WHERE `meta_key` LIKE '{$wpdb->prefix}ac_preferences_search_segments_%'
		";

        $preferences = $wpdb->get_results($sql);

        if (! is_array($preferences)) {
            return;
        }

        foreach ($preferences as $preference) {
            $segments = unserialize($preference->meta_value, ['allowed_classes' => false]);

            foreach ($segments['segments'] as $k => $segment) {
                $data = unserialize($segment['data'], ['allowed_classes' => false]);

                $data['url_parameters'] = [];

                if (isset($data['rules'])) {
                    if (is_array($data['rules']) && $data['rules']) {
                        $mapped_rules = [];

                        foreach ($data['rules'] as $rule) {
                            $mapped_rules[] = $this->map_rule((array)$rule);
                        }

                        $data['url_parameters']['ac-rules'] = json_encode([
                            'condition' => 'AND',
                            'rules'     => $mapped_rules,
                            'valid'     => true,
                        ]);
                    }

                    unset($data['rules']);
                }

                if (isset($data['order'])) {
                    $data['url_parameters']['order'] = $data['order'];
                    unset($data['order']);
                }

                if (isset($data['orderby'])) {
                    $data['url_parameters']['orderby'] = $data['orderby'];
                    unset($data['orderby']);
                }

                $segments['segments'][$k]['data'] = serialize($data);
            }

            $meta_key = str_replace('_search_segments_', '_segments_', $preference->meta_key);

            update_user_meta(
                $preference->user_id,
                $meta_key,
                $segments
            );
        }
    }

    private function map_rule(array $rule): array
    {
        $rule_mapping = new Rule(Mapping::RESPONSE);
        $operator_mapping = new Mapping\Operator(Mapping::RESPONSE);
        $value_type = new Mapping\ValueType(Mapping::RESPONSE);

        return [
            $rule_mapping->get('name')        => $rule['name'],
            $rule_mapping->get('operator')    => $operator_mapping->get($rule['operator']),
            $rule_mapping->get('value')       => $rule['value'],
            $rule_mapping->get('value_type')  => $value_type->get($rule['value_type']),
            $rule_mapping->get('value_label') => $rule['value_label'],
        ];
    }

}
