<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Traits\CommonTrait;
use App\Models\Visit;
use App\Models\User;
use DateTime;

class AdminController extends Controller
{
    use CommonTrait;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {
        // 관리자만 접속 가능하다.
        if(!$request->user()->is_admin) {
            abort(401);
        }

        $visitStats = $this->_visitStats();
        $userStats = $this->_userStats();

        return view('admin.index', [
            'visitStats' => $visitStats,
            'userStats' => $userStats
        ]);
    }

    /**
     * 방문자 통계
     */
    private function _visitStats()
    {
        $visitModel = new Visit();

        $monthlyData = $visitModel->getMonthlyData();
        $arrDt = array_column($monthlyData, 'dt');
        $arrCntNew = array_column($monthlyData, 'cntNew');
        $arrCntRe = array_column($monthlyData, 'cntRe');
        $arrCntTot = array_column($monthlyData, 'cntTot');

        $dates = $this->setArraySingQuote($arrDt);
        $cntNews = $this->setArraySingQuote($arrCntNew);
        $cntRes = $this->setArraySingQuote($arrCntRe);
        $cntTots = $this->setArraySingQuote($arrCntTot);

        return [
            'monthlyData' => $visitModel->getMonthlyData(),
            'visitorTodayCount' => $visitModel->todayCount(),
            'visitorYesterdayCount' => $visitModel->yesterdayCount(),
            'visitorTotalCount' => $visitModel->totalCount(),
            'dates' => $dates,
            'cntNews' => $cntNews,
            'cntRes' => $cntRes,
            'cntTots' => $cntTots,
            'totalCntNew' => array_sum($arrCntNew),
            'totalCntRe' => array_sum($arrCntRe),
            'totalCntTo' => array_sum($arrCntTot)
        ];
    }

    /**
     * 회원가입 통계
     */
    private function _userStats()
    {
        $userModel = new User();

        $dailyData = $userModel->dailyStats();
        $mapDailyData = array_column($dailyData, 'value', 'date');

        $startDt = new DateTime(now()->subDays(14)->toDateString());
        $endDt = new DateTime(now()->toDateString());

        $dates = [];
        for ($i = $startDt; $i <= $endDt; $i->modify('+1 day')) {
            $dt = $i->format("Y-m-d");

            $dates[$dt] = (array_key_exists($dt, $mapDailyData) ? $mapDailyData[$dt] : 0);
        }

        $todayCount = $dates[date('Y-m-d')];
        $cntNews = $this->setArraySingQuote(array_values($dates));
        $dates = $this->setArraySingQuote(array_keys($dates));

        return [
            'dailyData' => $dailyData, // 일별 통계
            'dates' => $dates,
            'cntNews' => $cntNews,
            'todayCount' => $todayCount, // 오늘 가입자
            'weeklyUserCount' => $userModel->totalWeeklyCount(), // 주간 회원가입자 수
            'monthlyUserCount' => $userModel->totalMonthlyCount(), // 월별 회원가입자 수
            'totalUserCount' => $userModel->totalUserCount(), // 전체 회원가입자 수
        ];
    }
}
