<?php

namespace App\Packages\v4\User\Transformer;

use App\Packages\v4\Capsule\Transformer\CapsuleArrayTransformer;
use App\Packages\v4\Capsule\Transformer\CapsuleCommentTransformer;
use App\Packages\User\Models\UserNotification;
use App\Packages\User\Models\UserNotificationPush;
use App\Packages\v4\User\Notifications\TypesNotification;

/**
 * Class NotificationTransformer
 * @package App\Packages\User\Transformer\v4
 */
class NotificationTransformer
{
	/**
	 * @param UserNotification $notification
	 *
	 * @return array
	 */
	public static function smallObject(UserNotification $notification)
	{
	    $owner = $notification->owner;

		$result = [
			'id' => $notification->id,
			'type' => $notification->getTypeTitle(),
            'is_seen' => (bool)$notification->is_seen,
			'created_at' => $notification->getFormattedCreated(),
			'created_at_micro' => $notification->created_at_micro,
		];

		if ($notification->user) {
            $result['user'] = UserTransformer::smallObject($notification->user);
        }

		if ($notification->capsule) {
			$result['capsule'] = CapsuleArrayTransformer::smallObject($notification->capsule);
		}

		if ($notification->comment) {
			$result['comment'] = CapsuleCommentTransformer::commentSmall($notification->comment, $owner);
		}

		return $result;
	}

    /**
     * @param UserNotificationPush $notification
     *
     * @return array
     */
    public static function smallObjectPush(UserNotificationPush $notification)
    {
        $owner = $notification->owner;

        $result = [
            'id' => $notification->id,
            'type' => $notification->getTypeTitle(),
            'created_at' => $notification->getFormattedCreated(),
            'created_at_micro' => $notification->created_at_micro,
        ];

        if ($notification->user) {
            $result['user'] = UserTransformer::smallObject($notification->user);
        }

        if ($notification->capsule) {
            $result['capsule'] = CapsuleArrayTransformer::smallObject($notification->capsule);
        }

        if ($notification->comment) {
            $result['comment'] = CapsuleCommentTransformer::commentSmall($notification->comment, $owner);
        }

        // If capsule is blocked, hack to bind app notification with push notification
        if ($notification->type == TypesNotification::BLOCKED) {
            $appNotification = $notification->findInAppBlockedCapsuleNotification();
            if ($appNotification) {
                $result['in_app'] = [
                    'id' => $appNotification->id,
                    'is_seen' => (bool)$appNotification->is_seen,
                ];
            }
        }

        if ($notification->type == TypesNotification::CAPSULE_UPLOAD_COMPLETE) {
            $result['should_force_display'] = (bool)true;
        }

        return $result;
    }
}