<?php

namespace SugarCalendarBookings\Integrations;

use SugarCalendarBookings\Options;
use SugarCalendarBookings\Plugin;

/**
 * Abstract base integration.
 *
 * Provides default implementations for logic shared by all integrations:
 * enabled state via options, base status, and processing rules for the enable toggle.
 *
 * @since 1.4.0
 */
abstract class AbstractIntegration implements IntegrationInterface {

	/**
	 * Check if the integration is enabled.
	 *
	 * Reads the `integrations.{slug}.enabled` option.
	 *
	 * @since 1.4.0
	 *
	 * @return bool
	 */
	public function is_enabled(): bool {

		return (bool) Plugin::get( Options::class )->get( 'integrations.' . $this->get_slug() . '.enabled', true );
	}

	/**
	 * Get the integration connection status.
	 *
	 * Base implementation returns connected/not_connected based on enabled state.
	 * OAuth integrations override with connection-aware logic.
	 *
	 * @since 1.4.0
	 *
	 * @return string
	 */
	public function get_status(): string {

		return $this->is_enabled() ? 'connected' : 'not_connected';
	}

	/**
	 * Get the post data processing rules.
	 *
	 * Base rules include the enable toggle. Subclasses should merge with parent.
	 *
	 * @since 1.4.0
	 *
	 * @return array
	 */
	public function get_post_data_processing_rules() {

		return [
			'integrations.' . $this->get_slug() . '.enabled' => 'form_boolean',
		];
	}
}
