<?php
namespace App\Domain\Feeds\DataTransferObjects;

use App\Domain\Feeds\Enums\CollectionMediaEnum;
use App\Domain\Feeds\Service\FeedMediaTransform;
use Spatie\DataTransferObject\DataTransferObject;

class FeedData extends DataTransferObject
{

    public static function fromAds($feed)
    {
        $user = new \stdClass();
        $user->id = 999999;
        $user->name = 'Реклама';
        $user->username = 'Реклама';
        $user->color = '#795541';
        $user->user_char = 'AD';

        return [
            'id'                => $feed->id,
            'type'              => $feed->type,
            'user'              => (array)$user,
            'entity'            => self::fromDataAds($feed, CollectionMediaEnum::COMMON()),
        ];
    }


    public static function fromDataAds($feed, $collection_media_name)
    {
        $entity = (object) [];
        $entity->title = $feed->title;
        $entity->body = nl2br($feed->body);
        $entity->views_count = $feed->views_count;
        $entity->slug = $feed->slug;
        $entity->type = $feed->type;
        $entity->created_at_humans = $feed->created_at->diffForHumans();
        $mediaTransform = (new FeedMediaTransform($feed))
            ->addCollectionMediaName($collection_media_name)
            ->spot();
        $entity->preview = $mediaTransform['preview'];
        $entity->collection_medias = $mediaTransform['medias'];

        $entity->is_ads = true;
        $entity->is_repost = false;
        $entity->price = null;
        $entity->is_paid = 0;
        $entity->likes = 0;
        $entity->liked = false;
        $entity->tags = [];
        $entity->status = 1;
        $entity->comments = 0;

        return $entity;
    }


    public static function fromDataBaseWithUser($feed, $purchased_subscriptions_by_users = [])
    {
        $feed_is_restrict = false;
        $feed_is_restrict_name = '';

        $feed_user_id = $feed->user_id;

        $is_my_feed = false;
        if(auth()->user()){
            $is_my_feed = auth()->user()->id === $feed_user_id;
        }


        $feed_is_semi_paid = $feed->is_paid === 2;

        // $is_adult_feed = false;
        // $is_my_settings_adult_restrict = auth()->user()->allow_adult_content;
        $restrict_feed_adult = false;

        // if(($is_my_settings_adult_restrict === false && $is_adult_feed) && $is_my_feed === false){
        //     $feed_is_restrict_name = 'adults';
        //     $restrict_feed_adult = true;
        // }

        $private_user_feed = $feed->user->private;
        $restrict_feed_private_account = false;
        if(($feed_is_semi_paid && $private_user_feed && !in_array($feed_user_id, $purchased_subscriptions_by_users)) && $is_my_feed === false){
            $feed_is_restrict_name = 'prohibited';
            $restrict_feed_private_account = true;
        }


        if($restrict_feed_adult || $restrict_feed_private_account){
            $feed_is_restrict = true;
        }

        $user = $feed->user->only('id', 'name', 'username', 'photo_path', 'color', 'user_char', 'private');
        return [
            'id'                => $feed->id,
            'type'              => $feed_is_restrict ? $feed_is_restrict_name : $feed->type,
            'user'              => $user,
            'entity'            => $feed_is_restrict ? null : self::fromData($feed, CollectionMediaEnum::COMMON()),
        ];
    }



    public static function fromData($feed, $collection_media_name)
    {
        $entity = (object) [];
        $entity->title = $feed->title;
        $entity->body = $feed->body;
        $entity->views_count = $feed->views_count;
        $entity->price = $feed->price;
        $entity->is_paid = $feed->is_paid;
        $entity->is_ads = false;
        // $entity->is_adult = $feed->is_adult;
        $entity->slug = $feed->slug;
        $entity->type = $feed->type;
        $entity->is_repost = $feed->is_repost;

        $entity->likes = $feed->likes_count;
        $entity->liked = $feed->liked ?? false;
        $entity->tags = $feed->tags;
        $entity->status = $feed->status;
        $entity->comments = $feed->comments_count;
        $entity->created_at_humans = $feed->created_at->diffForHumans();

        $mediaTransform = (new FeedMediaTransform($feed))
            ->addCollectionMediaName($collection_media_name)
            ->spot();


        $entity->preview = $mediaTransform['preview'];
        $entity->collection_medias = $mediaTransform['medias'];

        return $entity;
    }
}
