<?php

namespace App\Http\Controllers\Admin\User\Log\Login;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\LoginLog;

class StatController extends Controller
{
    private LoginLog $userLoginLog;

    public function __construct(LoginLog $userLoginLog)
    {
        $this->userLoginLog = $userLoginLog;
    }

    /**
     * 로그인 그래프
     * @method GET
     * @see /admin/user/log/login/stat
     * https://developers.google.com/chart/interactive/docs/basic_load_libs
     */
    public function index(Request $request)
    {
        /*
         * d: 일별, m: 월별, y: 년도별
         */
        $dateType = $request->get('date_type', 'd');
        if ($dateType !== 'm' && $dateType !== 'y') {
            $dateType = 'd';
        }

        $startDate = $request->get('start_date', date('Y-m-01'));
        $endDate = $request->get('end_date', date('Y-m-d'));
        $success = $request->get('success', 1);

        $startYear = '';
        $endYear = '';
        $startYearMonth = '';
        $endYearMonth = '';
        if ($dateType === 'y' || $dateType === 'm') {
            $startYear = substr($startDate, 0, 4);
            $endYear = substr($endDate, 0, 4);
        }
        if ($dateType === 'm') {
            $startMonth = substr($startDate, 5, 2);
            $endMonth = substr($endDate, 5, 2);
            $startYearMonth = ($startYear * 12 + $startMonth);
            $endYearMonth = ($endYear * 12 + $endMonth);
        }

        $userLoginLogData = $this->userLoginLog->getLoginSuccessCount($dateType, $startDate, $endDate, $success);
        $sumCount = 0;
        $arr = [];
        $max = 0;

        if ($userLoginLogData->total > 0) {
            foreach ($userLoginLogData->list as $key => $value) {
                $s = $value->day;
                if (!isset($arr[$s])) {
                    $arr[$s] = 0;
                }
                $arr[$s] += $value->cnt;

                if ($arr[$s] > $max) {
                    $max = $arr[$s];
                }
                $sumCount += $value->cnt;
            }
        }

        $result = [];
        $i = 0;
        $no = 0;
        $saveCount = -1;

        /*
         * 통계 데이터 생성
         */
        if (count($arr) > 0) {
            foreach ($arr as $date => $value) {
                $count = (int)$arr[$date];
                $result[$date]['count'] = $count;
                $i++;
                if ($saveCount !== $count) {
                    $no = $i;
                    $saveCount = $count;
                }
                $result[$date]['no'] = $no;
                $result[$date]['key'] = $date;
                $rate = ($count / $sumCount * 100);
                $result[$date]['rate'] = $rate;
                $sRate = number_format($rate, 1);
                $result[$date]['sRate'] = $sRate;

                $bar = (int)($count / $max * 100);
                $result[$date]['bar'] = $bar;
            }
            $data['maxValue'] = $max;
            $data['sumCount'] = $sumCount;
        }

        /*
         * 빈 배열 초기화
         */
        if ($dateType === 'y') {
            for ($i = $startYear; $i <= $endYear; $i++) {
                if (!isset($result[$i])) {
                    $result[$i] = [
                        'count' => 0,
                        'no' => 0,
                        'key' => 0,
                        'rate' => 0,
                        'sRate' => 0,
                        'bar' => 0,
                    ];
                }
            }
        } elseif ($dateType === 'm') {
            for ($i = $startYearMonth; $i <= $endYearMonth; $i++) {
                $year = floor($i / 12);
                if ($year * 12 == $i) $year--;
                $month = sprintf("%02d", ($i - ($year * 12)));
                $date = $year . '-' . $month;
                if (!isset($result[$date])) {
                    $result[$date] = [
                        'count' => 0,
                        'no' => 0,
                        'key' => 0,
                        'rate' => 0,
                        'sRate' => 0,
                        'bar' => 0,
                    ];
                }
            }
        } elseif ($dateType === 'd') {
            $date = $startDate;
            while ($date <= $endDate) {
                if (!isset($result[$date])) {
                    $result[$date] = [
                        'count' => 0,
                        'no' => 0,
                        'key' => 0,
                        'rate' => 0,
                        'sRate' => 0,
                        'bar' => 0,
                    ];
                }
                $date = date('Y-m-d', strtotime($date) + 86400);
            }
        }

        ksort($result);

        $data['result'] = $result;
        $data['dateType'] = $dateType;
        $data['startDate'] = $startDate;
        $data['endDate'] = $endDate;
        $data['success'] = $success;

        return view('admin.user.log.login.stat', $data);
    }
}
