<?php

namespace App\Http\Controllers;

use App\Models\CSRNow\Country;
use App\Models\SiteConfiguration;
use App\Services\Database\Facade\TouchDB;
use Illuminate\Encryption\Encrypter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Bus ;
use Illuminate\Support\Facades\Session;

class AjaxCallApiController extends Controller
{
    /**
     * @return JsonResponse
     */
    public function mainAjax()
    {
        $data = [
            'active_role' => Auth::check() ? laravelEncrypt(Auth::user()->roles[0]->name) : laravelEncrypt('guest'),
            'maintenance_status' => !app()->isDownForMaintenance() ? 'up' : 'down',
            'maintenance_key_live' => SiteConfiguration::LIVE_DOWN,
            'maintenance_key_whole' => SiteConfiguration::WHOLE_DOWN,

            'total_order' => 0,
            'import_2fa_status' => Session::get('ImportGoogleFA') == 2 ? 'inactive' : 'active',
            'export_2fa_status' => Session::get('ExportGoogleFA') == 2 ? 'inactive' : 'active',
            'clear_2fa_status' => Session::get('ClearGoogleFA') == 2 ? 'inactive' : 'active',
             'download_without_email_limit' => getSiteSettings('download_without_email_limit'),
        ];
        $encrypted = laravelEncrypt(json_encode($data));
        return response()->json($encrypted);
    }


    /**
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function checkCustomerStatus(Request $request)
    {
        $data = [];
        $guestMode = false;
        $guestCookie = $request->cookie('guest_mode');
        if (!empty($guestCookie) && $guestCookie == 'guest') {
            $guestMode = true;
        }
        $data['guestMode'] = $guestMode;
        $data['returning_customer'] = Auth::check();

        return response()->json($data);
    }



    /**
     * @return JsonResponse
     */
    public function getBatchInfo()
    {
        $id = Session::get('batchId');
        $progress = false;
        $hasFailure = false;
        $cancelled = false;
        $hasSession = !($id == null);
        if ($id) {
            $batch = Bus::findBatch($id);
            if ($batch) {
                $progress = $batch->progress() == 0.00;
                $hasFailure = !$batch->hasFailures() == true;
                $cancelled = !$batch->canceled() == true;
            }
        }

        return response()->json([
            'progress' => $progress,
            'hasFailure' => $hasFailure,
            'cancelled' => $cancelled,
            'hasSession' => $hasSession,
        ]);
    }

    /**
     * Get all column type date from database
     *
     * @return JsonResponse
     */
    public function getDateColumns()
    {
        TouchDB::setExtraDateColumns([
            'last_update',
            'retire_date',
            'retire_confirm_date',
        ]);
        $data = TouchDB::getAllDateColumns()->toArray();
        return response()->json($data);
    }

    /**
     * @return JsonResponse
     */
    public function getCountries()
    {
        $countries = Country::get()->pluck('country_name', 'id');
        return response()->json($countries);
    }

    /**
     * @return string
     */
    public function generateEncryptionKey()
    {
        $key = Encrypter::generateKey(config('app.cipher'));
        $newEncrypter = new Encrypter($key, config('app.cipher'));
        return base64_encode($newEncrypter->getKey());
    }
}
