<?php

namespace App\Http\Controllers;

use App\Models\AnalyticsEvent;
use App\Services\AnalyticsReports\ActiveUserReport;
use App\Services\AnalyticsReports\FrequentActiveUserReport;
use App\Services\AnalyticsReports\FrequentLoyalUsersReport;
use App\Services\AnalyticsReports\LoyalUserReport;
use App\Services\AnalyticsReports\RegisteredUsersReport;
use App\Utils\AnalyticsReportTypes;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Yajny\Models\Country;

class AnalyticsSummaryReportController extends GenericController
{
    protected $layout = "layouts.main";
    protected $data = array();
    public $module = 'analytics_summary_report';
    static $per_page = '25';

    public function __construct()
    {
        $this->model = new AnalyticsEvent();
        parent::__construct();
    }

    public function index(Request $request)
    {
        if (!\Auth::check())
            return redirect('user/login')->with('status', 'error')->with('message', 'You are not login');

        $this->grab($request);
        if ($this->access['is_view'] == 0 && $this->access['is_search'] == 0)
            return redirect('dashboard')->with('message', __('core.note_restric'))->with('status', 'error');

        if ($this->access['is_view'] == 0 && $this->access['is_search'] == 1)
            $limit = 5;

        $extraData = [];
        $reportData = [];
        if ($request->has('range', 'user_type')) {
            $range = explode('-', $request->range);
            $startDate = Carbon::make(trim($range[0]));
            $endDate = Carbon::make(trim($range[1]));
            $selectedCountry = $request->country;
            $selectedUserType = $request->user_type;

            $reportNames = AnalyticsReportTypes::getTypesWithNames();
            foreach (AnalyticsReportTypes::getTypes() as $reportType) {
                switch ($reportType) {
                    case AnalyticsReportTypes::ACTIVE_USERS:
                        $reportService = new ActiveUserReport($startDate, $endDate, $selectedCountry, $selectedUserType);
                        break;
                    case AnalyticsReportTypes::LOYAL_USERS:
                        $reportService = new LoyalUserReport($startDate, $endDate, $selectedCountry, $selectedUserType);
                        break;
                    case AnalyticsReportTypes::REGISTERED_USERS:
                        $reportService = new RegisteredUsersReport($startDate, $endDate, $selectedCountry, $selectedUserType);
                        break;
                    case AnalyticsReportTypes::FREQUENT_ACTIVE_USERS:
                        $reportService = new FrequentActiveUserReport($startDate, $endDate, $selectedCountry, $selectedUserType);
                        break;
                    case AnalyticsReportTypes::FREQUENT_LOYAL_USERS:
                        $reportService = new FrequentLoyalUsersReport($startDate, $endDate, $selectedCountry, $selectedUserType);
                        break;
                    default:
                        $reportService = null;
                        break;
                }

                if ($reportService) {
                    $reportCount = $reportService->getQuery()->select($reportService->getCountColumn())->get()->count();
                    $reportData[$reportNames[$reportType]] = $reportCount;
                }
            }

            $extraData = [
                'selectedStartDate' => $startDate->toDateString(),
                'selectedEndDate' => $endDate->toDateString(),
                'selectedCountry' => $selectedCountry,
                'selectedUserType' => $selectedUserType
            ];
        }

        $countriesOptions = Country::all()->map(function ($country) {
            return ['id' => $country->code, 'text' => $country->name];
        })->toArray();

        return view('manage.analytics.summary_report', array_merge([
            'reportData' => $reportData,
            'countries' => $countriesOptions
        ], $extraData));
    }
}