<?php
if (!defined('ABSPATH')) {
    exit;
}

abstract class VMO_Promotion_Base_Connector implements VMO_Promotion_Provider_Connector {
    protected $platform_key = '';
    protected $platform_label = '';
    protected $capabilities = array();

    public function __construct($platform_key, $platform_label, array $capabilities = array()) {
        $this->platform_key = sanitize_key($platform_key);
        $this->platform_label = sanitize_text_field($platform_label);
        $this->capabilities = wp_parse_args($capabilities, $this->default_capabilities());
    }

    public function get_platform_key() {
        return $this->platform_key;
    }

    public function get_platform_label() {
        return $this->platform_label;
    }

    public function get_capabilities() {
        return $this->capabilities;
    }

    public function connect_account(array $input) {
        return $this->unsupported('connect_account');
    }

    public function handle_callback(array $request) {
        return $this->unsupported('handle_callback');
    }

    public function refresh_auth(array $input) {
        return $this->unsupported('refresh_auth');
    }

    public function validate_scopes(array $input) {
        return array(
            'valid' => false,
            'platform_key' => $this->platform_key,
            'missing_scopes' => array(),
            'granted_scopes' => array(),
        );
    }

    public function list_accounts(array $input) {
        return array(
            'accounts' => array(),
            'platform_key' => $this->platform_key,
        );
    }

    public function publish_text_post(array $input) {
        return $this->unsupported('publish_text_post');
    }

    public function publish_media_post(array $input) {
        return $this->unsupported('publish_media_post');
    }

    public function publish_carousel_post(array $input) {
        return $this->unsupported('publish_carousel_post');
    }

    public function fetch_metrics(array $input) {
        return $this->unsupported('fetch_metrics');
    }

    public function list_replies(array $input) {
        return $this->unsupported('list_replies');
    }

    public function reply_to_comment(array $input) {
        return $this->unsupported('reply_to_comment');
    }

    public function list_ad_accounts(array $input) {
        return $this->unsupported('list_ad_accounts');
    }

    public function create_ad_draft(array $input) {
        return $this->unsupported('create_ad_draft');
    }

    public function launch_ad_campaign(array $input) {
        return $this->unsupported('launch_ad_campaign');
    }

    public function fetch_ad_metrics(array $input) {
        return $this->unsupported('fetch_ad_metrics');
    }

    public function register_webhook(array $input) {
        return $this->unsupported('register_webhook');
    }

    public function receive_webhook($payload) {
        return $this->unsupported('receive_webhook');
    }

    public function normalize_error($error) {
        if (is_wp_error($error)) {
            return array(
                'code' => $error->get_error_code(),
                'message' => $error->get_error_message(),
                'platform_key' => $this->platform_key,
            );
        }

        if ($error instanceof Throwable) {
            return array(
                'code' => 'connector_exception',
                'message' => $error->getMessage(),
                'platform_key' => $this->platform_key,
            );
        }

        return array(
            'code' => 'connector_error',
            'message' => is_scalar($error) ? (string) $error : 'Unknown connector error.',
            'platform_key' => $this->platform_key,
        );
    }

    protected function unsupported($operation) {
        return new WP_Error(
            'connector_not_implemented',
            sprintf('%s is not implemented for %s.', $operation, $this->platform_label),
            array(
                'platform_key' => $this->platform_key,
                'platform_label' => $this->platform_label,
                'operation' => $operation,
            )
        );
    }

    protected function default_capabilities() {
        return array(
            'connect_supported' => false,
            'publish_text' => false,
            'publish_image' => false,
            'publish_video' => false,
            'publish_carousel' => false,
            'reply_read' => false,
            'reply_write' => false,
            'metrics_read' => false,
            'webhooks' => false,
            'ad_management' => false,
            'app_review_required' => false,
            'domain_verification_required' => false,
        );
    }
}
