<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Api\Facebook;

// https://developers.facebook.com/docs/graph-api/reference
class FacebookApi extends FacebookBaseApi implements FacebookApiInterface
{
    const TYPE_PROFILE = 'profile';
    const TYPE_UPLOADED = 'uploaded';
    const DEFAULT_LIMIT = 20;

    // prettier-ignore
    const PAGE_FIELDS = ['id', 'name', 'username', 'link', 'website', 'about', 'category', 'whatsapp_number', 'description', 'description_html', 'general_info', 'overall_star_rating', 'rating_count', 'followers_count', 'talking_about_count', 'birthday', 'personal_interests', 'affiliation'];

    // prettier-ignore
    const POST_FIELDS = ['id', 'parent_id', 'created_time', 'updated_time', 'from{name}', 'actions', 'is_expired', 'is_hidden', 'is_popular', 'is_published', 'full_picture', 'reactions.summary(true)', 'comments.summary(true)', 'message', 'message_tags{name}', 'permalink_url', 'likes.summary(true)', 'shares'];

    // prettier-ignore
    const REVIEW_FIELDS = ['created_time', 'has_rating', 'has_review', 'rating', 'recommendation_type', 'review_text', 'reviewer{id,first_name,last_name,middle_name,name,name_format,picture,short_name}'];

    // prettier-ignore
    const PHOTO_FIELDS = ['id', 'name', 'album', 'width', 'from', 'height', 'link', 'images', 'event', 'name_tags{name}', 'place', 'alt_text', 'alt_text_custom', 'backdated_time', 'created_time', 'target', 'updated_time', 'comments.summary(true)', 'likes.summary(true)', 'shares'];

    public function page(string $pageId): array
    {
        return $this->get($pageId, [
            'fields' => implode(',', self::PAGE_FIELDS),
        ]);
    }

    public function posts(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/feed", [
            'fields' => implode(',', self::POST_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
        ]);

        return $result['data'] ?? [];
    }

    // https://developers.facebook.com/docs/graph-api/reference/page/ratings
    public function reviews(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/ratings", [
            'fields' => implode(',', self::REVIEW_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
        ]);

        return $result['data'] ?? [];
    }

    public function photos(string $userOrPageId, array $filters = []): array
    {
        $this->withAccessToken($this->getPageAccessToken($userOrPageId));

        $result = $this->get("{$userOrPageId}/photos", [
            'fields' => implode(',', self::PHOTO_FIELDS),
            'limit' => $filters['limit'] ?? self::DEFAULT_LIMIT,
            'type' => $filters['type'] ?? self::TYPE_UPLOADED,
        ]);

        return $result['data'] ?? [];
    }

    public function getPageAccessToken(string $pageId): ?string
    {
        return $this->get($pageId, ['fields' => 'access_token'])['access_token'] ?? null;
    }
}
