<?php
namespace WPDRMS\ASP\Index\ORM;

use WPDRMS\AdminUI\Data\AbstractOptionDataSiteOption;
use WPDRMS\AdminUI\Options\BoolOption;
use WPDRMS\AdminUI\Options\CustomFieldRuleArrayOption;
use WPDRMS\AdminUI\Options\TaxonomyExclusionRuleArrayOption;
use WPDRMS\AdminUI\Options\DirectoryListOption;
use WPDRMS\AdminUI\Options\IntArrayOption;
use WPDRMS\AdminUI\Options\IntOption;
use WPDRMS\AdminUI\Options\SelectOption;
use WPDRMS\AdminUI\Options\StringArrayOption;
use WPDRMS\AdminUI\Options\StringOption;

final class IndexTableOptions extends AbstractOptionDataSiteOption {
	public BoolOption $index_title;
	public BoolOption $index_content;
	public BoolOption $index_excerpt;
	public BoolOption $index_permalinks;
	public BoolOption $index_author_name;
	public BoolOption $index_author_bio;
	public BoolOption $index_tags;
	public BoolOption $index_categories;
	public StringArrayOption $index_taxonomies;
	public StringArrayOption $post_types;
	public StringArrayOption $post_statuses;
	public BoolOption $post_password_protected;
	public StringArrayOption $blog_ids;
	public StringArrayOption $custom_fields;
	public BoolOption $index_on_update_post_meta;
	public BoolOption $index_media_files;
	public StringOption $attachment_mime_types;
	public DirectoryListOption $attachment_exclude_directories;
	public DirectoryListOption $attachment_include_directories;
	public BoolOption $index_pdf_content;
	public SelectOption $pdf_method;
	public BoolOption $index_text_content;
	public BoolOption $index_richtext_content;
	public BoolOption $index_msword_content;
	public BoolOption $index_msexcel_content;
	public BoolOption $index_msppt_content;
	public BoolOption $index_epub_content;
	public BoolOption $media_service_send_file;
	public IntOption $limit;
	public BoolOption $use_stopwords;
	public StringArrayOption $stopwords;
	public IntOption $min_word_length;
	public BoolOption $extract_iframes;
	public BoolOption $extract_gutenberg_blocks;
	public BoolOption $extract_shortcodes;
	public StringArrayOption $exclude_shortcodes;
	public BoolOption $inflections;
	public BoolOption $extract_numeric;
	public SelectOption $language;
	public BoolOption $index_on_save;
	public BoolOption $cron_enable;
	public SelectOption $cron_period;
	public BoolOption $enable_compression;
	public BoolOption $pool_size_auto;
	public IntOption $pool_size_one;
	public IntOption $pool_size_two;
	public IntOption $pool_size_three;
	public IntOption $pool_size_rest;
	public CustomFieldRuleArrayOption $cf_exclusion_rules;
	public TaxonomyExclusionRuleArrayOption $taxonomy_exclusion_rules;
	public StringOption $date_exclusion_field;
	public StringOption $date_exclusion_global;
	public StringArrayOption $date_exclusion_overrides;
	public IntArrayOption $author_exclusion;
	public IntArrayOption $post_exclusion;

	protected const OPTION_NAME = 'asp_it_options_v2';

	protected const OPTIONS = array(
		'index_title'                    => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'index_content'                  => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'index_excerpt'                  => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'index_permalinks'               => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_author_name'              => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_author_bio'               => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_tags'                     => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_categories'               => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_taxonomies'               => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array() ),
		),
		'post_types'                     => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array( 'post', 'page' ) ),
		),
		'post_statuses'                  => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array( 'publish' ) ),
		),
		'post_password_protected'        => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'blog_ids'                       => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array() ),
		),
		'custom_fields'                  => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array() ),
		),
		'index_on_update_post_meta'      => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_media_files'              => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'attachment_mime_types'          => array(
			'type'         => 'string',
			'default_args' => array( 'value' => 'image/jpeg, image/gif, image/png' ),
		),
		'attachment_exclude_directories' => array(
			'type'         => 'directory_list',
			'default_args' => array( 'directories' => array() ),
		),
		'attachment_include_directories' => array(
			'type'         => 'directory_list',
			'default_args' => array( 'directories' => array() ),
		),
		'index_pdf_content'              => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'pdf_method'                     => array(
			'type'         => 'select',
			'default_args' => array(
				'value'         => 'auto',
				'default_value' => 'auto',
				'options'       => array( 'auto', 'pdftotext', 'pdf2text' ),
			),
		),
		'index_text_content'             => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_richtext_content'         => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_msword_content'           => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_msexcel_content'          => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_msppt_content'            => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'index_epub_content'             => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'media_service_send_file'        => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'limit'                          => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 25 ),
		),
		'use_stopwords'                  => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'stopwords'                      => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array( 'a', 'about', 'an', 'are', 'as', 'at', 'be', 'by', 'com', 'de', 'en', 'for', 'from', 'how', 'i', 'in', 'is', 'it', 'la', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'what', 'when', 'where', 'who', 'will', 'with', 'und', 'www' ) ),
		),
		'min_word_length'                => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 1 ),
		),
		'extract_iframes'                => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'extract_gutenberg_blocks'       => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'extract_shortcodes'             => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'exclude_shortcodes'             => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array( 'wpdreams_rpl', 'wpdreams_rpp' ) ),
		),
		'inflections'                    => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'extract_numeric'                => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'language'                       => array(
			'type'         => 'select',
			'default_args' => array(
				'value'         => 'english',
				'default_value' => 'english',
				'options'       => array(
					'english',
					'spanish',
					'french',
					'german',
					'italian',
					'portuguese',
					'dutch',
					'swedish',
					'norwegian',
					'danish',
					'finnish',
					'hungarian',
					'romanian',
					'russian',
					'turkish',
				),
			),
		),
		'index_on_save'                  => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'cron_enable'                    => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'cron_period'                    => array(
			'type'         => 'select',
			'default_args' => array(
				'value'         => 'asp_cr_five_minutes',
				'default_value' => 'asp_cr_five_minutes',
				'options'       => array(
					'asp_cr_two_minutes',
					'asp_cr_three_minutes',
					'asp_cr_five_minutes',
					'asp_cr_fifteen_minutes',
					'asp_cr_thirty_minutes',
					'asp_cr_sixty_minutes',
					'twicedaily',
					'daily',
				),
			),
		),
		'enable_compression'             => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => false ),
		),
		'pool_size_auto'                 => array(
			'type'         => 'bool',
			'default_args' => array( 'value' => true ),
		),
		'pool_size_one'                  => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 5000 ),
		),
		'pool_size_two'                  => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 8000 ),
		),
		'pool_size_three'                => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 10000 ),
		),
		'pool_size_rest'                 => array(
			'type'         => 'int',
			'default_args' => array( 'value' => 10000 ),
		),
		'cf_exclusion_rules'             => array(
			'type'         => 'custom_field_rule_array',
			'default_args' => array(
				'logic' => 'AND',
				'rules' => array(),
			),
		),
		'taxonomy_exclusion_rules'       => array(
			'type'         => 'taxonomy_exclusion_rule_array',
			'default_args' => array(
				'logic' => 'AND',
				'rules' => array(),
			),
		),
		// Date exclusion: which date column to test, a global cutoff applied to all indexed
		// post types, and optional per-post-type overrides stored as "post_type|YYYY-MM-DD".
		'date_exclusion_field'           => array(
			'type'         => 'string',
			'default_args' => array( 'value' => 'post_date' ),
		),
		'date_exclusion_global'          => array(
			'type'         => 'string',
			'default_args' => array( 'value' => '' ),
		),
		'date_exclusion_overrides'       => array(
			'type'         => 'string_array',
			'default_args' => array( 'value' => array() ),
		),
		// Author exclusion (user IDs) and manual post exclusion (post IDs).
		'author_exclusion'               => array(
			'type'         => 'int_array',
			'default_args' => array( 'value' => array() ),
		),
		'post_exclusion'                 => array(
			'type'         => 'int_array',
			'default_args' => array( 'value' => array() ),
		),
	);

	/**
	 * Returns a flat array suitable for Manager::__construct() $args.
	 *
	 * @return array<string, mixed>
	 */
	public function toManagerArgs(): array {
		return array(
			'index_title'                    => $this->index_title->value,
			'index_content'                  => $this->index_content->value,
			'index_excerpt'                  => $this->index_excerpt->value,
			'index_permalinks'               => $this->index_permalinks->value,
			'index_author_name'              => $this->index_author_name->value,
			'index_author_bio'               => $this->index_author_bio->value,
			'index_tags'                     => $this->index_tags->value,
			'index_categories'               => $this->index_categories->value,
			'index_taxonomies'               => implode( '|', $this->index_taxonomies->value ),
			'post_types'                     => $this->index_media_files->value
				? array_unique( array_merge( $this->post_types->value, array( 'attachment' ) ) )
				: $this->post_types->value,
			'post_statuses'                  => implode( ',', $this->post_statuses->value ),
			'post_password_protected'        => $this->post_password_protected->value,
			'blog_ids'                       => $this->blog_ids->value,
			'index_customfields'             => implode( '|', $this->custom_fields->value ),
			'index_on_update_post_meta'      => $this->index_on_update_post_meta->value,
			'attachment_mime_types'          => $this->attachment_mime_types->value,
			'attachment_exclude_directories' => $this->attachment_exclude_directories->directories,
			'attachment_include_directories' => $this->attachment_include_directories->directories,
			'index_pdf_content'              => $this->index_pdf_content->value,
			'index_pdf_method'               => $this->pdf_method->value,
			'index_text_content'             => $this->index_text_content->value,
			'index_richtext_content'         => $this->index_richtext_content->value,
			'index_msword_content'           => $this->index_msword_content->value,
			'index_msexcel_content'          => $this->index_msexcel_content->value,
			'index_msppt_content'            => $this->index_msppt_content->value,
			'index_epub_content'             => $this->index_epub_content->value,
			'media_service_send_file'        => $this->media_service_send_file->value,
			'limit'                          => $this->limit->value,
			'use_stopwords'                  => $this->use_stopwords->value,
			'stopwords'                      => implode( ',', $this->stopwords->value ),
			'min_word_length'                => $this->min_word_length->value,
			'extract_iframes'                => $this->extract_iframes->value,
			'extract_gutenberg_blocks'       => $this->extract_gutenberg_blocks->value,
			'extract_shortcodes'             => $this->extract_shortcodes->value,
			'exclude_shortcodes'             => implode( ',', $this->exclude_shortcodes->value ),
			'inflections'                    => $this->inflections->value,
			'extract_numeric'                => $this->extract_numeric->value,
			'language'                       => $this->language->value,
			'index_on_save'                  => $this->index_on_save->value,
			'cron_enable'                    => $this->cron_enable->value,
			'cron_period'                    => $this->cron_period->value,
			'enable_compression'             => $this->enable_compression->value,
			'pool_size_auto'                 => $this->pool_size_auto->value,
			'pool_size_one'                  => $this->pool_size_one->value,
			'pool_size_two'                  => $this->pool_size_two->value,
			'pool_size_three'                => $this->pool_size_three->value,
			'pool_size_rest'                 => $this->pool_size_rest->value,
			'cf_exclusion_rules'             => array_map(
				function ( $r ) {
					return $r->jsonSerialize();
				},
				$this->cf_exclusion_rules->rules
			),
			'cf_exclusion_rules_logic'       => $this->cf_exclusion_rules->logic,
			'taxonomy_exclusion_rules'       => array_map(
				function ( $r ) {
					return $r->jsonSerialize();
				},
				$this->taxonomy_exclusion_rules->rules
			),
			'taxonomy_exclusion_rules_logic' => $this->taxonomy_exclusion_rules->logic,
			'date_exclusion_field'           => $this->date_exclusion_field->value,
			'date_exclusion_global'          => $this->date_exclusion_global->value,
			'date_exclusion_overrides'       => $this->date_exclusion_overrides->value,
			'author_exclusion'               => $this->author_exclusion->value,
			'post_exclusion'                 => $this->post_exclusion->value,
		);
	}
}
