<?php

use Illuminate\Support\Facades\Route;
use twa\translationssync\translationsSyncController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/ecom/users/login/{newtwork}/redirect', [\twa\ecomauth\controllers\v1\SocialAuthenticateController::class , 'redirectToSocial']);
Route::get('/ecom/users/login/{newtwork}/callback', [\twa\ecomauth\controllers\v1\SocialAuthenticateController::class , 'render']);

Route::get('/users/login/data/callback',[\twa\ecomauth\controllers\v1\SocialAuthenticateController::class,"handleSocialLogin"]);

Route::get('{store_prefix}/{language_prefix}/logout',function($store,$lang){
    session(['login' => false]);
    \Session::flush();
    twaSetCookie('rmt', '' , time()-3600);
    return redirect('/'.$store.'/'.$lang.'/');
})->name('logout');

Route::group([
    'prefix'     => '/{store_prefix}/{language_prefix}/',
    'middleware' => [
       \twa\ecomgeneral\middleware\storeRoutesMiddleware::class,
        \twa\ecomgeneral\middleware\EcomLanguageMiddleware::class,
//        twa\ecomgeneral\middleware\storeDefaultDataMiddleware::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \twa\ecomauth\middleware\AppendUserMiddleware::class,
    ],
], function () {

    Route::get('/complete-profile', [\twa\ecomauth\controllers\v1\ProfileController::class , 'redirectCompleteProfile'])->name('redirect-complete-profile');

    Route::group(['prefix' => 'account'], function(){
        Route::get('/auth/guest', [\twa\ecomauth\controllers\AccountLoginController::class, 'renderGuestCheckout'])->name('account-auth-guest');
        Route::get('/auth/{type?}', [\twa\ecomauth\controllers\v1\AuthenticateController::class, 'render'])->name('account-auth');
        Route::post('/auth/{type?}', [\twa\ecomauth\controllers\v1\AuthenticateController::class, 'authenticate'])->name('account-auth');
        Route::get('get/login/{type_slug}/{login_type}/{token}', [\twa\ecomauth\controllers\v1\LoginController::class, 'render'])->name('account-login');
        Route::post('get/login/{type_slug}/{login_type}/{token}', [\twa\ecomauth\controllers\v1\LoginController::class, 'login'])->name('account-login');
        Route::get('get/redirect/{type_slug}/otp/{token}', [\twa\ecomauth\controllers\v1\LoginController::class, 'redirectToOTP'])->name('redirect-otp');
        Route::get('/complete-profile', [\twa\ecomauth\controllers\v1\ProfileController::class, 'renderCompleteProfile'])->name('complete-profile');
        Route::post('/complete-profile', [\twa\ecomauth\controllers\v1\ProfileController::class, 'completeProfile'])->name('complete-profile');
        Route::get('/request-reset-password/{type_slug?}' , [\twa\ecomauth\controllers\v1\ResetPasswordController::class, 'requestResetPassword'])->name('request-reset-password');
        Route::post('/verify-reset-password/{type_slug?}' , [\twa\ecomauth\controllers\v1\ResetPasswordController::class, 'verifyRequestResetPassword'])->name('verify-reset-password');
        Route::get('/reset-password/{type_slug}', [\twa\ecomauth\controllers\v1\ResetPasswordController::class, 'renderResetPassword'])->name('account-reset-password');
        Route::post('/reset-password/{type_slug}', [\twa\ecomauth\controllers\v1\ResetPasswordController::class, 'resetPassword'])->name('account-reset-password');
        Route::get('/redirect/login' , [\twa\ecomauth\controllers\v1\LoginController::class , 'redirectAsLoggedIn'])->name('redirect-login')->middleware([\twa\ecomauth\middleware\AuthCheckCompleteProfile::class]);
    });

    // Dont Know the status of this API
    Route::any('/verification/response/fallback/{type}',function ($store,$lang,$type){
        if($type=="phone"){ return view('pages.auth.login',["route"=>route('account-login',['type'=>'phone','verify'=>true])]);
        }else{ view('pages.auth.login',["route"=>route('account-login',['type'=>'email','verify'=>true])]); }
    })->name('verification-link-fallback');

});


//Route::get('/twa/lang/sync', function () {
//    return translationsSyncController::FetchLanguagesFromAPI();
//});

//Route::get('/ecom/users/forgot/form/{id}/{token}', function ($id, $token) {
//    $Challenge = \twa\ecomauth\models\EcomUsersPasswordResetChallengesModel::where('id', $id)->first();
//    if (!$Challenge) {
//        abort(404);
//    }
//    if ($token != md5($Challenge->challenge)) {
//        abort(404);
//    }
//    return view('EcomAuthViews::changepass.default', [ "challenge" => $Challenge ]);
//});

//Route::get('/ecom/users/login/{newtwork}/callback', function ($network) {
//    return (new \twa\ecomauth\facades\EcomUsersRegistrationFacade())->socialLogin($network);
//});


//Route::post('/ecom/users/forgot/form/{id}/{token}', function ($id, $token) {
//    $Challenge = \twa\ecomauth\models\EcomUsersPasswordResetChallengesModel::where('id', $id)->first();
//    if (!$Challenge) {
//        abort(404);
//    }
//    if ($token != md5($Challenge->challenge)) {
//        abort(404);
//    }
//    $User = \twa\ecomauth\models\EcomUsersModel::where('id', $Challenge->ecom_users_id)->where('cancelled', 0)->first();
//    if (!$User) {
//        abort(404);
//    }
//    if (request()->input('confirm_password') != request()->input('password')) {
//        echo "passwords does not match";
//        exit();
//    }
//    $User->password = md5(request()->input('password'));
//    $User->save();
//    echo "password successfully changed. <a href='".env('APP_URL')."'>proceed to the website</a>";
//});






