<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Facades\Socialite;

class UserController extends Controller
{
    // GET
    
    public function create() {
        return view('user.create');
    }
    
    public function loginForm() {
        return view('user.login');
    }
    
    public function loginAuth() {
        $is_auth = true;
        return view('user.auth', compact('is_auth'));
    }
    
    public function logout(Request $request) {
        Auth::logout();
        //return redirect()->route('login.create');
        return redirect()->route('home');
    }
    
    public function settings() {
        return view('user.settings');
    }
    
    // POST
    
    public function store(Request $request) {
        //dd($request->all());
        
        $request->validate([
            'email' => 'required|email|unique:users',
            'password' => 'required|confirmed',
        ],
        [
            'email.unique' => 'Email вже використовується.',
        ]);
        
        $user = User::create([
            'email' => $request->email, // 'email' => $request->input('email'),
            'password' => Hash::make($request->password),
            'email_verified_at' => NOW(),
        ]);
        
        session()->flash('success', 'Реєстрація успішна');
        Auth::login($user);
        //return redirect()->route('home'); // || redirect()->home(); || redirect('/');
        return response()->json([
            'status' => 'success',
            'redirect' => route('home')
        ]);
    }
    
    public function edit(Request $request) {
        //dd($request->all());
        
        $request->validate([
            'email' => 'required|email',
            'password' => 'required|confirmed',
        ]);
        
        $user = Auth()->user();
        
        $user->email = $request->email;
        $user->password = Hash::make($request->password);
        $user->email_verified_at = NOW();
        
        if ($user->save()) {
            session()->flash('success', 'Налаштування збережено успішно');
            //return redirect()->back();
            return response()->json([
                'status' => 'success',
                'redirect' => route('settings')
            ]);
        }
        //return redirect()->back()->with('error', 'Виникла помилка при збереженні налаштувань');
        return response()->json([
            'status' => 'error',
            'message' => 'Виникла помилка при збереженні налаштувань'
        ]);

    }
    
    public function login(Request $request) {
        
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);
        
        if (Auth::attempt([
            'email' => $request->email,
            'password' => $request->password,
        ])) {
            //return redirect()->route('home');
            return response()->json([
                'status' => 'success',
                'redirect' => route('home')
            ]);
        }
        //return redirect()->back()->with('error', 'Введено невiрний Email або пароль');
        return response()->json([
            'status' => 'error',
            'message' => 'Введено невiрний Email або пароль'
        ]);

    }
    
    public function loginGoogle() {
        $googleUser = Socialite::driver('google')->user();

        $user = User::where('email','=', $googleUser->email)->first();
        if (!$user) {
            $user = User::create([
                'email' => $googleUser->email,
                'password' => Hash::make(config('project.empty_password')),
            ]);
        }

        Auth::login($user);
        return redirect()->route('loading');
    }
    
    public function loginApple() {
        $appleUser = Socialite::driver('apple')->user();
        //$appleUser = Socialite::driver("sign-in-with-apple")->user();
        
        $user = User::where('email','=', $appleUser->email)->first();
        if (!$user) {
            $user = User::create([
                'email' => $appleUser->email,
                'password' => Hash::make(config('project.empty_password')),
            ]);
        }

        Auth::login($user);
        return redirect()->route('loading');
    }
    
}
