<?php

namespace App\Http\Controllers\User;

use App\Constants\Status;
use App\Http\Controllers\Controller;
use App\Lib\FormProcessor;
use App\Lib\GoogleAuthenticator;
use App\Models\AdminNotification;
use App\Models\Chat;
use App\Models\Currency;
use App\Models\DeviceToken;
use App\Models\Form;
use App\Models\Investment;
use App\Models\Payment;
use App\Models\ReferralCommissionWallet;
use App\Models\ReferralWithdrawLog;
use App\Models\RefLog;
use App\Models\Transaction;
use App\Models\User;
use App\Models\Withdrawal;
use App\Rules\FileTypeValidate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class UserController extends Controller
{
    public function home()
    {
        $pageTitle = 'Dashboard';

        $user                   = auth()->user();
        $widget['investments']  = Investment::where('user_id', $user->id)->count();
        $widget['withdrawals']  = Withdrawal::where('user_id', $user->id)->count();
        $widget['referred']     = User::where('ref_by', $user->id)->count();
        $widget['transactions'] = Transaction::orderBy('id', 'desc')->where('user_id', $user->id)->count();
        $transactions           = Transaction::orderBy('id', 'desc')->where('user_id', $user->id)->with('currency')->take(10)->get();
        return view('Template::user.dashboard', compact('pageTitle', 'widget', 'transactions'));
    }

    public function show2faForm()
    {
        $ga = new GoogleAuthenticator();
        $user = auth()->user();
        $secret = $ga->createSecret();
        $qrCodeUrl = $ga->getQRCodeGoogleUrl($user->username . '@' . gs('site_name'), $secret);
        $pageTitle = '2FA Security';
        return view('Template::user.twofactor', compact('pageTitle', 'secret', 'qrCodeUrl'));
    }

    public function create2fa(Request $request)
    {
        $user = auth()->user();
        $request->validate([
            'key' => 'required',
            'code' => 'required',
        ]);
        $response = verifyG2fa($user, $request->code, $request->key);
        if ($response) {
            $user->tsc = $request->key;
            $user->ts = Status::ENABLE;
            $user->save();
            $notify[] = ['success', 'Two factor authenticator activated successfully'];
            return back()->withNotify($notify);
        } else {
            $notify[] = ['error', 'Wrong verification code'];
            return back()->withNotify($notify);
        }
    }

    public function disable2fa(Request $request)
    {
        $request->validate([
            'code' => 'required',
        ]);

        $user = auth()->user();
        $response = verifyG2fa($user, $request->code);
        if ($response) {
            $user->tsc = null;
            $user->ts = Status::DISABLE;
            $user->save();
            $notify[] = ['success', 'Two factor authenticator deactivated successfully'];
        } else {
            $notify[] = ['error', 'Wrong verification code'];
        }
        return back()->withNotify($notify);
    }

    public function transactions(Request $request)
    {
        $request->validate([
            'currency' => 'nullable|exists:currencies,id'
        ]);
        $pageTitle  = 'Transaction History';
        $currencies = Currency::where('status', Status::ENABLE)->select('id', 'code')->get();
        $transactions = auth()->user()->transactions();
        
        if ($request->currency) $transactions = $transactions->where('currency_id', $request->currency);

        $transactions = $transactions->orderBy('id', 'desc')->with('currency', 'user')->paginate(getPaginate(1));
        return view('Template::user.transactions', compact('pageTitle', 'transactions', 'currencies'));
    }

    public function referral()
    {
        $pageTitle     = "My Referred Users";
        $referralUsers = User::where('ref_by', auth()->id())->where('status', Status::USER_ACTIVE)->orderBy('id', 'DESC')->paginate(getPaginate());
        return view('Template::user.referral_user', compact('pageTitle', 'referralUsers'));
    }

    public function referralUserInvest($id)
    {
        $pageTitle        = 'Investment History';
        $user             = auth()->user();
        $referralUser     = User::where('id', $id)->where('ref_by', $user->id)->firstOrFail();
        $referInvestments = Investment::where('status', Status::INVESTMENT_COMPLETE)
            ->where('user_id', $referralUser->id)
            ->groupBy('currency_id')
            ->selectRaw('sum(amount) as total, user_id, currency_id, status, created_at')
            ->with('user', 'currency')
            ->paginate(getPaginate());
        return view('Template::user.referral_user_invest', compact('pageTitle', 'referInvestments'));
    }

    public function referralCommission()
    {
        $pageTitle      = "Referral Commission";
        $user           = auth()->user();
        $commissionLogs = RefLog::where('user_id', $user->id)->orderBy('id', 'desc')->with('currency', 'byWho', 'user')->paginate(getPaginate());
        return view('Template::user.referral_commission', compact('pageTitle', 'commissionLogs', 'user'));
    }

    public function referralWithdraw()
    {
        $pageTitle       = "Referral Bonus Withdraw log";
        $refWithdrawLogs = ReferralWithdrawLog::where('user_id', auth()->id())->orderBy('id', 'desc')->paginate(getPaginate());
        return view('Template::user.referral_withdraw_logs', compact('pageTitle', 'refWithdrawLogs'));
    }


    public function activateAccount()
    {
        if (Investment::where("user_id", auth()->id())->exists()) {
            return redirect()->route('user.invest.history');
        }

        $currencies = Currency::active()->get();
        $pageTitle  = 'Activate Account';
        return view('Template::user.activate_account', compact('pageTitle', 'currencies'));
    }
    public function withdrawCommission(Request $request)
    {
        $request->validate([
            'id'     => 'required|exists:referral_commission_wallets,id',
            'amount' => 'required|numeric|gt:0',
        ]);
        $user   = auth()->user();
        $wallet = ReferralCommissionWallet::where('id', $request->id)->where('user_id', $user->id)->firstOrFail();
        if ($request->amount > $wallet->amount) {
            $notify[] = ['error', 'Insufficient Balance!'];
            return back()->withNotify($notify);
        }
        $wallet->amount -= $request->amount;
        $wallet->save();

        $withdrawal                = new Withdrawal();
        $withdrawal->user_id       = $user->id;
        $withdrawal->currency_id   = $wallet->currency_id;
        $withdrawal->amount        = $request->amount;
        $withdrawal->remain_amount = $request->amount;
        $withdrawal->date_priority = now();
        $withdrawal->save();

        $log              = new ReferralWithdrawLog();
        $log->user_id     = $user->id;
        $log->currency_id = $wallet->currency_id;
        $log->amount      = getAmount($request->amount);
        $log->save();

        notify($user, 'WITHDRAW_REQUEST', [
            'amount'   => getAmount($request->amount),
            'currency' => $wallet->currency->code,
        ]);
        $notify[] = ['success', 'Your Withdraw Requested Sent Successfully'];
        return back()->withNotify($notify);
    }

    public function kycForm()
    {
        if (auth()->user()->kv == Status::KYC_PENDING) {
            $notify[] = ['error', 'Your KYC is under review'];
            return to_route('user.home')->withNotify($notify);
        }
        if (auth()->user()->kv == Status::KYC_VERIFIED) {
            $notify[] = ['error', 'You are already KYC verified'];
            return to_route('user.home')->withNotify($notify);
        }
        $pageTitle = 'KYC Form';
        $form = Form::where('act', 'kyc')->first();
        return view('Template::user.kyc.form', compact('pageTitle', 'form'));
    }

    public function kycData()
    {
        $user = auth()->user();
        $pageTitle = 'KYC Data';
        return view('Template::user.kyc.info', compact('pageTitle', 'user'));
    }

    public function kycSubmit(Request $request)
    {
        $form = Form::where('act', 'kyc')->firstOrFail();
        $formData = $form->form_data;
        $formProcessor = new FormProcessor();
        $validationRule = $formProcessor->valueValidation($formData);
        $request->validate($validationRule);
        $user = auth()->user();
        foreach (@$user->kyc_data ?? [] as $kycData) {
            if ($kycData->type == 'file') {
                fileManager()->removeFile(getFilePath('verify') . '/' . $kycData->value);
            }
        }
        $userData = $formProcessor->processFormData($request, $formData);
        $user->kyc_data = $userData;
        $user->kyc_rejection_reason = null;
        $user->kv = Status::KYC_PENDING;
        $user->save();

        $notify[] = ['success', 'KYC data submitted successfully'];
        return to_route('user.home')->withNotify($notify);
    }

    public function downloadAttachment($fileHash)
    {
        $filePath = decrypt($fileHash);
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
        $title = slug(gs('site_name')) . '- attachments.' . $extension;
        try {
            $mimetype = mime_content_type($filePath);
        } catch (\Exception $e) {
            $notify[] = ['error', 'File does not exists'];
            return back()->withNotify($notify);
        }
        header('Content-Disposition: attachment; filename="' . $title);
        header("Content-Type: " . $mimetype);
        return readfile($filePath);
    }

    public function userData()
    {
        $user = auth()->user();

        if ($user->profile_complete == Status::YES) {
            return to_route('user.home');
        }

        $pageTitle  = 'User Data';
        $info       = json_decode(json_encode(getIpInfo()), true);
        $mobileCode = @implode(',', $info['code']);
        $countries  = json_decode(file_get_contents(resource_path('views/partials/country.json')));

        return view('Template::user.user_data', compact('pageTitle', 'user', 'countries', 'mobileCode'));
    }

    public function userDataSubmit(Request $request)
    {

        $user = auth()->user();

        if ($user->profile_complete == Status::YES) {
            return to_route('user.home');
        }

        $countryData  = (array)json_decode(file_get_contents(resource_path('views/partials/country.json')));
        $countryCodes = implode(',', array_keys($countryData));
        $mobileCodes  = implode(',', array_column($countryData, 'dial_code'));
        $countries    = implode(',', array_column($countryData, 'country'));

        $request->validate([
            'country_code' => 'required|in:' . $countryCodes,
            'country'      => 'required|in:' . $countries,
            'mobile_code'  => 'required|in:' . $mobileCodes,
            'username'     => 'required|unique:users|min:6',
            'mobile'       => ['required', 'regex:/^([0-9]*)$/', Rule::unique('users')->where('dial_code', $request->mobile_code)],
        ]);


        if (preg_match("/[^a-z0-9_]/", trim($request->username))) {
            $notify[] = ['info', 'Username can contain only small letters, numbers and underscore.'];
            $notify[] = ['error', 'No special character, space or capital letters in username.'];
            return back()->withNotify($notify)->withInput($request->all());
        }

        $user->country_code = $request->country_code;
        $user->mobile       = $request->mobile;
        $user->username     = $request->username;


        $user->address = $request->address;
        $user->city = $request->city;
        $user->state = $request->state;
        $user->zip = $request->zip;
        $user->country_name = @$request->country;
        $user->dial_code = $request->mobile_code;

        $user->profile_complete = Status::YES;
        $user->save();

        return to_route('user.home');
    }

    public function sendActivationRequest(Request $request)
    {
        $request->validate([
            'currency_id' => 'required',
        ]);

        $user = auth()->user();

        if ($user->activation == Status::INVESTMENT_ACTIVATION) {
            $notify[] = ['error', 'You account is active'];
            return redirect()->route('user.home')->withNotify($notify);
        }
        if (Investment::where("user_id", $user->id)->exists()) {
            $notify[] = ['error', 'Please wait till merging your activating investment'];
            return redirect()->route('user.home')->withNotify($notify);
        }

        $currency = Currency::active()->findOrFail($request->currency_id);

        $investment                = new Investment();
        $investment->currency_id   = $currency->id;
        $investment->user_id       = $user->id;
        $investment->plan_id       = 0;
        $investment->amount        = $currency->activation_fees;
        $investment->remain_amount = $currency->activation_fees;
        $investment->is_activation = Status::INVESTMENT_ACTIVATION;
        $investment->save();

        $user->activation = Status::PENDING;
        $user->save();

        $adminNotification            = new AdminNotification();
        $adminNotification->user_id   = $user->id;
        $adminNotification->title     = 'New activation request for the amount of ' . getAmount($investment->amount) . ' ' . $investment->currency->code;
        $adminNotification->click_url = urlPath('admin.investment.detail', @$investment->id);;
        $adminNotification->save();

        notify($user, 'ACTIVATION_REQUEST');
        $notify[] = ['success', 'Activating request sent successfully'];

        return redirect()->route('user.home')->withNotify($notify);
    }

    public function sendMessage(Request $request, $id)
    {

        $validator = Validator::make($request->all(), [
            'message'    => 'required|max:250',
            'file' => ['nullable', new FileTypeValidate(['jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'txt'])],
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()]);
        }


        $payment = Payment::where('id', $id)->first();
        if (!$payment) {
            return response()->json(['error' => 'Invalid this payment']);
        }


        if ($payment->status != Status::PAYMENT_COMPLETED && $payment->status != Status::PAYMENT_REPORT_RECEIVER && $payment->status != Status::PAYMENT_CANCELLED) {
            $user             = auth()->user();
            $chat             = new Chat();
            $chat->payment_id = $payment->id;
            $chat->user_id    = auth()->id();
            $chat->message    = $request->message;

            if ($request->hasFile('file')) {
                try {
                    $chat->file = fileUploader($request->file('file'), getFilePath('conversation'));
                } catch (\Exception $exp) {
                    return response()->json(['error' => 'Couldn\'t upload your image']);
                }
            }

            $chat->save();
            return view('Template::user.chat.last_message', compact('chat'));
        } else {
            return response()->json(['error' => 'You are not eligible to send a message']);
        }
    }

    public function chatMessage(Request $request)
    {
        $totalMessage = Chat::where('payment_id', $request->payment_id)->count();
        $messages     = Chat::where('payment_id', $request->payment_id)->take($request->messageCount)->with('user')->latest()->get();
        $messageCount = 1;
        if ($messages->count() == $totalMessage) {
            $messageCount = 0;
        }
        return view('Template::user.chat.message', compact('messages', 'messageCount'));
    }
    public function addDeviceToken(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'token' => 'required',
        ]);

        if ($validator->fails()) {
            return ['success' => false, 'errors' => $validator->errors()->all()];
        }

        $deviceToken = DeviceToken::where('token', $request->token)->first();

        if ($deviceToken) {
            return ['success' => true, 'message' => 'Already exists'];
        }

        $deviceToken          = new DeviceToken();
        $deviceToken->user_id = auth()->user()->id;
        $deviceToken->token   = $request->token;
        $deviceToken->is_app  = Status::NO;
        $deviceToken->save();

        return ['success' => true, 'message' => 'Token saved successfully'];
    }
}
