<?php
namespace App\Domain\Subscriptions\Service;

use Carbon\Carbon;
use App\Models\User;
use App\Domain\Points\Models\Point;
use App\Domain\Subscriptions\Models\Subscription;

class SubscriptionService
{
    public static function amount()
    {
        $subscriptions = Subscription::where('ends_at', '>', Carbon::now())
        ->where('status', 'complete')->get();
        return $subscriptions->sum('price');
    }


    public static function hasSubscription()
    {
        return auth()->user()->subscription()->count();
    }

    public static function activeSubscription()
    {
        $last = auth()->user()->subscription()->first();
        if (empty($last)) {
            return false;
        }

        if ($last->ends_at > Carbon::now()) {
            return true;
        }
        return false;
    }

    public static function freeLeaders()
    {
        $leader_count = nova_get_setting('vote_leader_count');

        $users = \DB::table('users_subscribers')->get();

        $users = $users->groupBy('subscriber_id');

        $leaders = collect();

        $users = $users->map(function ($item) {
            return $item->sum('leader');
        })->sortDesc()->take($leader_count);

        foreach ($users as $user_id => $leader) {
            $lUser = User::find($user_id);
            $lUser->name = $lUser->name;
            $lUser->countVote = $leader;
            $leaders[] = $lUser;
        }

        return $leaders;
    }

    public static function leaders()
    {
        $leaders = \DB::select("SELECT vote.subscriber_id as user_id, SUM(vote.leader) as vote_count FROM
        (
            SELECT friends.subscriber_id, friends.leader
            FROM users_subscribers friends,
            (
                  SELECT user_id,ends_at FROM subscriptions
                  WHERE ends_at > CURRENT_TIMESTAMP
            )user_active,
            (
                  SELECT user_id,ends_at FROM subscriptions
                  WHERE ends_at > CURRENT_TIMESTAMP
            )user_active2

            where
            friends.user_id = user_active.user_id
            and friends.subscriber_id  = user_active2.user_id
        ) vote
        GROUP BY vote.subscriber_id ORDER BY vote_count DESC");


        return $leaders;
    }

    public static function calculate($user_id)
    {
        $balance = \DB::select("SELECT T_IN.user_id, T_IN.in-T_OUT.out as balance FROM
        (SELECT user_id, sum(point) as out FROM points WHERE user_id = {$user_id} AND direction = 1 GROUP BY user_id) T_OUT,
        (SELECT user_id, sum(point) as in FROM points WHERE user_id = {$user_id} AND direction = 0 GROUP BY user_id) T_IN
        where T_IN.user_id = T_OUT.user_id");
        $balance = @$balance[0]->balance;

        if (empty($balance)) {
            $points = Point::where('user_id', $user_id)->get();
            $in = $points->where('direction', 0)->sum('point');
            $out = $points->where('direction', 1)->sum('point');
            $sum = $in-$out;
            if ($sum <= 0) {
                return 0;
            }
            return $sum;
        }
        return $balance;
    }
}
