<?php

namespace App\Console\Commands\Test;

use App\Packages\Capsule\Models\Capsule;
use App\Packages\Capsule\Models\CapsuleComment;
use App\Packages\User\Models\User;
use App\Packages\User\Models\UserNotification;
use App\Packages\User\Notifications\NotificationComment;
use App\Packages\User\Notifications\TypesNotification;
use Illuminate\Support\Carbon;
use Illuminate\Console\Command;

/**
 * Class ForUserNotificationCommand
 * @package App\Console\Commands\Test
 */
class ForUserNotificationCommand extends Command
{
    protected $signature = 'notif:user';

    public function handle()
    {

        $user_id = (int) $this->ask('User id') ? : 0;

        /** @var User $user_from */
        $user_from = User::query()->find(0);

        if ($user_id != 0) {

            /** @var Capsule $capsule */
            $capsule = Capsule::query()
                ->where('user_id', $user_id)
                ->first();

            if ( ! $capsule ) {
                $this->info('User dont have capsule');
                return false;
            }

            $owner = $capsule->user;

            /** Create comments 29 month old */
            $comment_time = Carbon::now()->subDays(29);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 29 days old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 30 days old */
            $comment_time = Carbon::now()->subDays(30);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 30 days old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 31 days old */
            $comment_time = Carbon::now()->subDays(31);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 31 month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 1 month old */
            $comment_time = Carbon::now()->subMonth();

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 1 month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();


            /** Create comments 2 month old */
            $comment_time = Carbon::now()->subMonths(2);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 2 month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 5 month old */
            Carbon::now()->subMonths(5);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 5 month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 11 month old */
            $comment_time = Carbon::now()->subMonths(11);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 11 months old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments year old */
            $comment_time = Carbon::now()->subYear();

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 1 year old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments year and month old */
            $comment_time = Carbon::now()->subYear()->subMonth();

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 1 year and month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 2 years old */
            $comment_time = Carbon::now()->subYears(2);

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 2 years old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();

            /** Create comments 2 years and month old */
            $comment_time = Carbon::now()->subYears(2)->subMonth();

            $comment = new CapsuleComment();
            $comment->capsule_id = $capsule->id;
            $comment->user_id = $user_from->id;
            $comment->content = 'comment 2 years and month old';
            $comment->created_at = $comment_time->format('Y-m-d H:i:s');
            $comment->save();

            $notification = UserNotification::storeNotification($owner, TypesNotification::COMMENT);
            $notification
                ->setCapsule($capsule)
                ->setComment($comment)
                ->setUser($user_from)
                ->setCustomTime($comment_time)
                ->persist();


            $user_from->refreshCommentCount();
            $capsule->refreshCommentCount();

        } else {
            $this->info('No user id inputed');
            return false;
        }

        $this->info('Completed');
    }
}













