<?php

namespace VendorTasksDokanClickUp\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * PluginSettings class.
 *
 * Provides typed accessor methods for all plugin settings stored in WordPress options.
 *
 * @since 1.0.0
 */
class PluginSettings {

	public const OPTION_NAME = 'vendor_tasks_dokan_clickup_settings';

	/**
	 * Settings data loaded from WordPress options.
	 *
	 * @var array<string, mixed>
	 */
	private array $data;

	/**
	 * Constructor.
	 *
	 * Loads plugin settings from WordPress options into a local array.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$raw        = get_option( self::OPTION_NAME, array() );
		$this->data = is_array( $raw ) ? $raw : array();
	}

	/**
	 * Get the ClickUp API token.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_api_token(): string {
		return (string) ( $this->data['api_token'] ?? '' );
	}

	/**
	 * Get the ClickUp list ID.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_list_id(): string {
		return (string) ( $this->data['list_id'] ?? '' );
	}

	/**
	 * Get the ClickUp workspace ID.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_workspace_id(): string {
		return (string) ( $this->data['workspace_id'] ?? '' );
	}

	/**
	 * Get the task priority (1=Urgent, 2=High, 3=Normal, 4=Low).
	 *
	 * @since 1.0.0
	 * @return int
	 */
	public function get_task_priority(): int {
		return (int) ( $this->data['task_priority'] ?? 3 );
	}

	/**
	 * Get the task name format string.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_task_name_format(): string {
		return (string) ( $this->data['task_name_format'] ?? 'New Vendor: {vendor_name}' );
	}

	/**
	 * Get the task description type ('normal' or 'markdown').
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_task_description_type(): string {
		return (string) ( $this->data['task_description_type'] ?? 'normal' );
	}

	/**
	 * Get the description template string.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_description_template(): string {
		return (string) ( $this->data['description_template'] ?? '' );
	}

	/**
	 * Get the task status.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_task_status(): string {
		return (string) ( $this->data['task_status'] ?? '' );
	}

	/**
	 * Get the task due date offset in days.
	 *
	 * @since 1.0.0
	 * @return int Days until due date; 0 = no due date.
	 */
	public function get_task_due_date_offset(): int {
		return (int) ( $this->data['task_due_date_offset'] ?? 0 );
	}

	/**
	 * Get the task assignee IDs.
	 *
	 * @since 1.0.0
	 * @return int[]
	 */
	public function get_task_assignees(): array {
		return array_values(
			array_filter(
				array_map( 'absint', (array) ( $this->data['task_assignees'] ?? array() ) )
			)
		);
	}

	/**
	 * Check if logging is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_logging_enabled(): bool {
		return (bool) ( $this->data['enable_logging'] ?? true );
	}

	/**
	 * Check if profile update sync is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_sync_on_profile_update(): bool {
		return ! empty( $this->data['sync_on_profile_update'] );
	}

	/**
	 * Check if failed task retry is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_retry_failed_tasks(): bool {
		return ! empty( $this->data['retry_failed_tasks'] );
	}

	/**
	 * Get the maximum retry attempts.
	 *
	 * @since 1.0.0
	 * @return int Minimum 1.
	 */
	public function get_max_retry_attempts(): int {
		return max( 1, (int) ( $this->data['max_retry_attempts'] ?? 3 ) );
	}

	/**
	 * Get the failure notification email address.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_failure_notify_email(): string {
		return (string) ( $this->data['failure_notify_email'] ?? '' );
	}

	/**
	 * Get the Slack webhook URL.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_slack_webhook_url(): string {
		return (string) ( $this->data['slack_webhook_url'] ?? '' );
	}

	/**
	 * Check if Slack task created notifications are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_slack_notify_task_created(): bool {
		return ! empty( $this->data['slack_notify_task_created'] );
	}

	/**
	 * Check if Slack task failed notifications are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_slack_notify_task_failed(): bool {
		return ! empty( $this->data['slack_notify_task_failed'] );
	}

	/**
	 * Check if vendor approved trigger is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_trigger_vendor_approved(): bool {
		return ! empty( $this->data['trigger_vendor_approved'] );
	}

	/**
	 * Get the vendor approved status.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_trigger_vendor_approved_status(): string {
		return (string) ( $this->data['trigger_vendor_approved_status'] ?? '' );
	}

	/**
	 * Check if vendor disabled trigger is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_trigger_vendor_disabled(): bool {
		return ! empty( $this->data['trigger_vendor_disabled'] );
	}

	/**
	 * Get the vendor disabled status.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_trigger_vendor_disabled_status(): string {
		return (string) ( $this->data['trigger_vendor_disabled_status'] ?? '' );
	}

	/**
	 * Check if commission change comments are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_commission_change_comment(): bool {
		return ! empty( $this->data['commission_change_comment'] );
	}

	/**
	 * Check if trust status tagging is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_trust_status_tag(): bool {
		return ! empty( $this->data['trust_status_tag'] );
	}

	/**
	 * Get the withdraw request action ('none' or 'comment').
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_withdraw_request_action(): string {
		return (string) ( $this->data['withdraw_request_action'] ?? 'none' );
	}

	/**
	 * Check if reverse withdrawal alerts are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_reverse_withdrawal_alert(): bool {
		return ! empty( $this->data['reverse_withdrawal_alert'] );
	}

	/**
	 * Get the product review action ('none' or 'comment').
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_product_review_action(): string {
		return (string) ( $this->data['product_review_action'] ?? 'none' );
	}

	/**
	 * Get the first product published status.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_first_product_published_status(): string {
		return (string) ( $this->data['first_product_published_status'] ?? '' );
	}

	/**
	 * Check if first sale comments are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_first_sale_comment(): bool {
		return ! empty( $this->data['first_sale_comment'] );
	}

	/**
	 * Check if order comments are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_order_comment_enabled(): bool {
		return ! empty( $this->data['order_comment_enabled'] );
	}

	/**
	 * Check if payment method checks are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_payment_method_check(): bool {
		return ! empty( $this->data['payment_method_check'] );
	}

	/**
	 * Check if webhooks are enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_enable_webhook(): bool {
		return ! empty( $this->data['enable_webhook'] );
	}

	/**
	 * Get the webhook secret.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_webhook_secret(): string {
		return (string) ( $this->data['webhook_secret'] ?? '' );
	}

	/**
	 * Get webhook status mappings.
	 *
	 * @since 1.0.0
	 * @return array<int, array<string, mixed>>
	 */
	public function get_webhook_status_mappings(): array {
		return (array) ( $this->data['webhook_status_mappings'] ?? array() );
	}

	/**
	 * Check if list routing is enabled.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	public function is_enable_list_routing(): bool {
		return ! empty( $this->data['enable_list_routing'] );
	}

	/**
	 * Get routing rules.
	 *
	 * @since 1.0.0
	 * @return array<int, array<string, mixed>>
	 */
	public function get_routing_rules(): array {
		return (array) ( $this->data['routing_rules'] ?? array() );
	}

	/**
	 * Get all event templates.
	 *
	 * @since 1.0.0
	 * @return array<string, mixed>
	 */
	public function get_event_templates(): array {
		return (array) ( $this->data['event_templates'] ?? array() );
	}

	/**
	 * Get a specific event template.
	 *
	 * @since 1.0.0
	 * @param string $event_key Event template key.
	 * @return array<string, mixed> Empty array when key not found.
	 */
	public function get_event_template( string $event_key ): array {
		return (array) ( ( $this->data['event_templates'] ?? array() )[ $event_key ] ?? array() );
	}

	/**
	 * Get all settings as an array.
	 *
	 * @since 1.0.0
	 * @return array<string, mixed>
	 */
	public function to_array(): array {
		return $this->data;
	}
}
