<?php

namespace App\PackagesAdmin\Core\Controllers;

use App\Http\Controllers\Controller;
use App\Packages\Capsule\Models\Capsule;
use App\Packages\Capsule\Models\CapsuleComment;
use App\Packages\Friend\Models\FriendInvite;
use App\Packages\User\Models\User;
use App\Packages\v4\Capsule\Repository\CapsuleRepository;
use App\Packages\v4\User\Repository\UserRepository;
use App\PackagesAdmin\Manager\Models\Manager;
use Auth;
use Carbon\Carbon;
use Illuminate\Database\Query\JoinClause;
use Illuminate\View\View;

/**
 * Class IndexController
 * @package App\PackagesAdmin\Core\Controllers
 */
class IndexController extends Controller
{

	/**
	 * @TODO: Refactore to service
     * @return View
	 */
	public function index()
	{
	    $total_users = User::where('is_registered', true)
            ->count();

	    $total_users_without_capsules = User::where('table_user.is_registered', true)
            ->leftJoin('table_capsule', 'table_capsule.user_id', '=', 'table_user.id')
            ->whereNull('table_capsule.id')
            ->count();

        $total_capsules = Capsule::where('is_complete', true)
            ->count();

        $total_comments = CapsuleComment::count();
        $total_invites = FriendInvite::count();

        $total_capsule_per_user = \DB::table(\DB::raw("(
            SELECT COUNT(user_id) as total_per_user 
            FROM table_capsule 
            WHERE is_complete = true 
            GROUP BY user_id
        ) as grouped_capsules"))
            ->selectRaw("AVG(grouped_capsules.total_per_user) as result")
            ->first();

        if ($total_capsules <= 0 || $total_users <= 0) $total_capsules_users = 0;
        else $total_capsules_users = $total_capsules / $total_users;

        $total_per_month = [];
        $total_per_week = [];

        Carbon::macro('range', function ($from, $to, $interval = 'P1D') {
            $interval = $interval instanceof \DateInterval ?: new \DateInterval($interval);
            return collect(
                new \DatePeriod($from, $interval, $to)
            );
        });

        $from = Carbon::now()->subYear(1);
        $to = Carbon::now();

        $months = Carbon::range($from, $to, 'P1M');

        foreach ($months->reverse()->take(7) as $month) {
            $total_per_month[] = [
                'date' => $month->format('m.Y'),
                'users' => UserRepository::init()->countByMonth($month),
                'capsules' => CapsuleRepository::init()->countByMonth($month)
            ];
        }

        $weeks = Carbon::range($from, $to, 'P1W');

        foreach ($weeks->reverse()->take(7) as $week) {
            $start_week = clone $week->startOfWeek();
            $end_week = clone $week->endOfWeek();

            $total_per_week[] = [
                'date' => $start_week->format('d.m.Y') . ' - ' . $end_week->format('d.m.Y'),
                'users' => UserRepository::init()->countByDateRange($start_week, $end_week),
                'capsules' => CapsuleRepository::init()->countByDateRange($start_week, $end_week)
            ];
        }

		return view('admin.index.index', [
		    'total_users' => $total_users,
            'total_capsules' => $total_capsules,
            'total_comments' => $total_comments,
            'total_invites' => $total_invites,
            'total_capsule_per_user' => (float) number_format($total_capsule_per_user->result, 2, '.', ''),
            'total_per_month' => $total_per_month,
            'total_per_week' => $total_per_week,
            'total_users_without_capsules' => $total_users_without_capsules,
            'total_capsules_users' => (float) number_format($total_capsules_users, 2, '.', '')
        ]);
	}

    /**
     *
     */
	public function phpInfo()
    {
        phpinfo();
    }
}
