<?php
namespace flow\social\publisher;
if (!defined('WPINC'))
	die;

/**
 * LinkedIn Publisher.
 *
 * Publishes text, article, and image posts to a LinkedIn Organisation Page
 * via the Community Management API (REST).
 *
 * Required scopes: w_organization_social (org pages), w_member_social (personal)
 *
 * Endpoint:
 *   POST https://api.linkedin.com/rest/posts
 *
 * Uses the same API version header as the feed reader (FFLinkedIn).
 *
 * @package   FlowFlow
 * @author    Looks Awesome <email@looks-awesome.com>
 * @link      http://looks-awesome.com
 * @copyright Looks Awesome
 */
class FFLinkedInPublisher extends FFBasePublisher {

	/** LinkedIn REST API version header (YYYYMM). */
	const LI_API_VERSION = '202601';

	public function getNetwork() {
		return 'linkedin';
	}

	/**
	 * @inheritDoc
	 */
	public function publish(array $postData) {
		$this->token = isset($postData['token']) ? $postData['token'] : '';
		$accountId   = isset($postData['account_id']) ? $postData['account_id'] : '';

		if (empty($this->token) || empty($accountId)) {
			return $this->errorResult('Missing LinkedIn token or organisation ID.');
		}

		$this->log('Publishing to LinkedIn', [
			'account_id' => $accountId,
			'has_text'   => !empty($postData['text']),
			'has_media'  => !empty($postData['media']),
			'has_link'   => !empty($postData['link']),
		]);

		// TODO: Implement actual API call when ready
		// POST https://api.linkedin.com/rest/posts
		// Headers: Authorization, X-Restli-Protocol-Version, LinkedIn-Version
		// Body: { author, commentary, visibility, distribution, lifecycleState, ... }

		return $this->errorResult('LinkedIn publishing is not yet implemented. API integration pending.');
	}

	/**
	 * @inheritDoc
	 */
	protected function isRateLimitError($message) {
		$indicators = [
			'Resource level throttle',
			'APPLICATION DAY limit',
			'Too Many Requests',
			'Throttled',
			'Rate limit reached',
		];
		$lower = strtolower($message);
		foreach ($indicators as $phrase) {
			if (stripos($message, $phrase) !== false) {
				return true;
			}
		}
		return parent::isRateLimitError($message);
	}
}
