<?php

namespace App\Console\Commands\Test;

use App\Packages\Capsule\Models\Capsule;
use App\Packages\Capsule\Models\CapsuleBlock;
use App\Packages\Capsule\Models\CapsuleComment;
use App\Packages\Capsule\Models\CapsuleLike;
use App\Packages\Capsule\Models\CapsuleOpen;
use App\Packages\Capsule\Models\CapsulePreview;
use App\Packages\Capsule\Models\CapsuleRemoved;
use App\Packages\Capsule\Models\CapsuleReport;
use App\Packages\Capsule\Models\CapsuleSubscriber;
use App\Packages\Capsule\Models\CapsuleTaggedUser;
use App\Packages\Capsule\Models\CapsuleVideo;
use App\Packages\Friend\Models\FriendInvite;
use App\Packages\Friend\Models\FriendRelation;
use App\Packages\User\Models\User;
use App\Packages\User\Models\UserNotificationPush;
use App\Packages\User\Models\v3\UserNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Ramsey\Uuid\Uuid;

/**
 * Class DuplicationContentForUserCommand
 * @package App\Console\Commands
 */
class DuplicateContentForUserCommand extends Command
{
    protected $signature = 'user:duplicate_content';

    public function handle()
    {
        $owner_id = (int) $this->ask('Owner id') ? : 0;
        $for_id = (int) $this->ask('For id') ? : 0;

        if ($owner_id == 0 || $for_id == 0) {
            $this->info('No one correct ID specified.');
            return true;
        }


        // Capsules
        $capsules = Capsule::query()->where('user_id', $owner_id)
            ->get();

        $capsules_ids = [];
        $comments_ids = [];
        $likes_ids = [];

        if ($capsules->isNotEmpty()) {
            foreach ($capsules as $capsule) {
                /** @var Capsule $capsule */


                // Preview
                $preview_id = false;
                $previews = CapsulePreview::query()->where('id', $capsule->preview_id)->get();
                if ($previews->isNotEmpty()) {
                    foreach ($previews as $preview) {
                        /** @var CapsulePreview $preview */
                        $new_preview = $preview->replicate();
                        $new_preview->uuid = Uuid::uuid4();
                        $new_preview->save();

                        $preview_id = $new_preview->id;
                    }
                }

                // Croped
                $cropped_id = false;
                $croppedList = CapsulePreview::query()->where('id', $capsule->cropped_id)->get();
                if ($croppedList->isNotEmpty()) {
                    foreach ($croppedList as $cropped) {
                        /** @var CapsulePreview $cropped */
                        $new_cropped = $cropped->replicate();
                        $new_cropped->uuid = Uuid::uuid4();
                        $new_cropped->save();

                        $cropped_id = $new_cropped->id;
                    }
                }

                // Thumbnail
                $thumbnail_id = false;
                $thumbnailList = CapsulePreview::query()->where('id', $capsule->thumbnail_id)->get();
                if ($thumbnailList->isNotEmpty()) {
                    foreach ($thumbnailList as $thumbnail) {
                        /** @var CapsulePreview $thumbnail */
                        $new_thubmnail = $thumbnail->replicate();
                        $new_thubmnail->uuid = Uuid::uuid4();
                        $new_thubmnail->save();

                        $thumbnail_id = $new_thubmnail->id;
                    }
                }

                // Video
                $video_id = false;
                $videos = CapsuleVideo::query()->where('id', $capsule->video_id)->get();
                if ($videos->isNotEmpty()) {
                    foreach ($videos as $video) {
                        /** @var CapsuleVideo $video */
                        $new_video = $video->replicate();
                        $new_video->uuid = Uuid::uuid4();
                        $new_video->save();

                        $video_id = $new_video->id;
                    }
                }


                $new_capsule = $capsule->replicate();
                $new_capsule->uuid = Uuid::uuid4();
                $new_capsule->user_id = $for_id;
                if ($preview_id) $new_capsule->preview_id = $preview_id;
                if ($video_id) $new_capsule->video_id = $video_id;
                if ($thumbnail_id) $new_capsule->thumbnail_id = $thumbnail_id;
                if ($cropped_id) $new_capsule->cropped_id = $cropped_id;
                $new_capsule->save();

                $capsules_ids[$capsule->id] = $new_capsule->id;

                // Capsule block
                $blocks = CapsuleBlock::query()->where('capsule_id', $capsule->id)->get();
                if ($blocks->isNotEmpty()) {
                    foreach ($blocks as $block) {
                        /** @var CapsuleBlock $block */
                        $new_block = $block->replicate();
                        $new_block->capsule_id = $new_capsule->id;
                        $new_block->save();
                    }
                }

                // Capsule Open
                $openList = CapsuleOpen::query()->where('capsule_id', $capsule->id)->get();
                if ($openList->isNotEmpty()) {
                    foreach ($openList as $open) {
                        /** @var CapsuleOpen $open */
                        $new_open = $open->replicate();
                        $new_open->capsule_id = $new_capsule->id;
                        if ($open->user_id == $owner_id) {
                            $new_open->user_id = $for_id;
                        }
                        $new_open->save();
                    }
                }

                // Capsule Removed
                $removedList = CapsuleRemoved::query()->where('capsule_id', $capsule->id)->get();
                if ($openList->isNotEmpty()) {
                    foreach ($removedList as $removed) {
                        /** @var CapsuleRemoved $removed */
                        $new_removed = $removed->replicate();
                        $new_removed->capsule_id = $new_capsule->id;
                        if ($removed->user_id == $owner_id) $new_removed->user_id = $for_id;
                        $new_removed->save();
                    }
                }

                // Capsule Report
                $reportList = CapsuleReport::query()->where('capsule_id', $capsule->id)->get();
                if ($reportList->isNotEmpty()) {
                    foreach ($reportList as $report) {
                        /** @var CapsuleReport $report */
                        $new_report = $report->replicate();
                        $new_report->capsule_id = $new_capsule->id;
                        if ($report->by_user_id == $owner_id) $new_report->by_user_id = $for_id;
                        $new_report->save();
                    }
                }

                // Capsule Subscriber
                $subscribers = CapsuleSubscriber::query()->where('capsule_id', $capsule->id)->get();
                if ($subscribers->isNotEmpty()) {
                    foreach ($subscribers as $subscriber) {
                        /** @var CapsuleSubscriber $subscriber */
                        $new_subscriber = $subscriber->replicate();
                        if ($subscriber->user_id == $owner_id) $new_subscriber->user_id = $for_id;
                        $new_subscriber->capsule_id = $new_capsule->id;
                        $new_subscriber->save();
                    }
                }

                // Capsule Tagged Users
                $taggedList = CapsuleTaggedUser::query()->where('capsule_id', $capsule->id)->where('by_user_id', $owner_id)->get();
                if ($taggedList->isNotEmpty()) {
                    foreach ($taggedList as $tagged) {
                        /** @var CapsuleTaggedUser $tagged */
                        $new_tagged = $tagged->replicate();
                        $new_tagged->capsule_id = $new_capsule->id;
                        $new_tagged->by_user_id = $for_id;
                        $new_tagged->save();
                    }
                }

                // Capsule Likes
                $likes = CapsuleLike::query()->where('capsule_id', $capsule->id)->get();
                if ($likes->isNotEmpty()) {
                    foreach ($likes as $like) {
                        /** @var CapsuleLike $like */
                        $new_like = $like->replicate();
                        $new_like->capsule_id = $new_capsule->id;
                        if ($like->user_id == $owner_id) $new_like->user_id = $for_id;
                        $new_like->save();

                        $likes_ids[$like->id] = $new_like->id;
                    }
                }

                // Capsule comments
                $comments = CapsuleComment::query()->where('capsule_id', $capsule->id)->get();
                if ($comments->isNotEmpty()) {
                    foreach ($comments as $comment) {
                        /** @var CapsuleComment $comment */
                        $new_comment = $comment->replicate();
                        $new_comment->uuid = Uuid::uuid4();
                        $new_comment->capsule_id = $new_capsule->id;
                        if ($comment->user_id == $owner_id) $new_comment->user_id = $for_id;
                        $new_comment->save();

                        $comments_ids[$comment->id] = $new_comment->id;
                    }
                }
            }
        }

        // Friends
        $friends = FriendRelation::query()->where('user_id', $owner_id)->orWhere('friend_id', $owner_id)->get();
        if ($friends->isNotEmpty()) {
            foreach ($friends as $friend) {
                /** @var FriendRelation $friend */
                if ($friend->user_id == $for_id || $friend->friend_id == $for_id) continue;

                $new_friend = $friend->replicate();
                if ($friend->user_id == $owner_id) $new_friend->user_id = $for_id;
                if ($friend->friend_id == $owner_id) $new_friend->friend_id = $for_id;

                $check = FriendRelation::query()->where('user_id', $new_friend->user_id)->where('friend_id', $new_friend->friend_id)->exists();
                if ($check) continue;

                $new_friend->save();
            }
        }

        // Like
        $likes = CapsuleLike::query()->where('user_id', $owner_id)->get();
        if ($likes->isNotEmpty()) {
            foreach ($likes as $like) {
                /** @var CapsuleLike $like */
                if (in_array($like->id, $likes_ids)) continue;

                $new_like = $like->replicate();
                $new_like->user_id = $for_id;
                $new_like->save();

                $likes_ids[$like->id] = $new_like->id;
            }
        }

        // Comments
        $comments = CapsuleComment::query()->where('user_id', $owner_id)->get();
        if ($comments->isNotEmpty()) {
            foreach ($comments as $comment) {
                /** @var CapsuleComment $comment */

                if (in_array($comment->id, $comments_ids)) continue;

                $new_comment = $comment->replicate();
                $new_comment->uuid = Uuid::uuid4();
                $new_comment->user_id = $for_id;
                $new_comment->save();

                $comments_ids[$comment->id] = $new_comment->id;
            }
        }

        // Notifications
        $notifications = UserNotification::query()->where('owner_id', $owner_id)->orWhere('user_id', $owner_id)->get();
        if ($notifications->isNotEmpty()) {
            foreach($notifications as $notification) {
                /** @var UserNotification $notification */
                $new_notification = $notification->replicate();
                $new_notification->uuid = Uuid::uuid4();
                if ($notification->user_id == $owner_id) $new_notification->user_id = $for_id;
                if ($notification->owner_id == $owner_id) $new_notification->owner_id = $for_id;

                if ($notification->comment_id && isset($comments_ids[$notification->comment_id])) {
                    $new_notification->comment_id = $comments_ids[$notification->comment_id];
                }

                if ($notification->capsule_id && isset($capsules_ids[$notification->capsule_id])) {
                    $new_notification->capsule_id = $capsules_ids[$notification->capsule_id];
                }

                $new_notification->save();
            }
        }

        // Push Notifications
        $push_notifications = UserNotificationPush::query()->where('owner_id', $owner_id)->orWhere('user_id', $owner_id)->get();
        if ($push_notifications->isNotEmpty()) {
            foreach ($push_notifications as $push_notification) {
                /** @var UserNotificationPush $push_notification */
                $new_push_notificaiton = $push_notification->replicate();
                $new_push_notificaiton->uuid = Uuid::uuid4();
                if ($push_notification->user_id == $owner_id) $new_push_notificaiton->user_id = $for_id;
                if ($push_notification->owner_id == $owner_id) $new_push_notificaiton->owner_id = $for_id;

                if ($push_notification->comment_id && isset($comments_ids[$push_notification->comment_id])) {
                    $new_push_notificaiton->comment_id = $comments_ids[$push_notification->comment_id];
                }

                if ($push_notification->capsule_id && isset($capsules_ids[$push_notification->capsule_id])) {
                    $new_push_notificaiton->capsule_id = $capsules_ids[$push_notification->capsule_id];
                }

                $new_push_notificaiton->save();
            }
        }


        $this->info('Script END');
        return true;

    }
}













