<?php

namespace App\Http\Controllers\web\home;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserVerify;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rules\Password;

class registerController extends Controller
{
    public function user_register(Request $request)
    {

        $request->validate([
            'name' => 'required|unique:users,name',
            'email' => 'required',
            'password' => 'required',
            // Corrected validation rule for confirming password
        ]);
        DB::beginTransaction();
        if ($request->password) {

            try {
                $data = User::create([
                    'name' => $request->name,
                    'email' => $request->email,
                    'status' => 1,
                    'is_corporate' => $request->has('is_corporate') ? 1 : 0,
                    'password' => Hash::make($request->password)
                ]);
                if ($data) {
                    $token = Str::random(64);

                    UserVerify::create([
                        'user_id' => $data->id,
                        'token' => $token
                    ]);

                    Mail::send('emails.verify_email', ['token' => $token], function ($message) use ($request) {
                        $message->to($request->email);
                        $message->subject('Email Verification Mail');
                    });
                    DB::commit();

                    return redirect()->route('american.plastic.web')->with('verify-email', true);

                }
            } catch (\Throwable $th) {
                DB::rollBack();
                DB::table('error_logs')->insert([
                    'message' => $th->getMessage(),
                    'file' => $th->getFile(),
                    'line' => $th->getLine(),
                    'trace' => $th->getTraceAsString(),
                    'created_at' => now(),
                ]);

                return redirect()->route('american.plastic.web');
            }

        } else {
            return redirect()->back()->withErrors('Password & confirm password incorrect')->withInput();
        }
    }
    public function verifyAccount($token)
    {
        $verifyUser = UserVerify::where('token', $token)->first();

        try {
            if (!is_null($verifyUser)) {
                $user = $verifyUser->user;

                if (!$user->email_verified_at) {
                    $verifyUser->user->email_verified_at = now();
                    $verifyUser->user->status = 1;
                    $verifyUser->user->save();
                    $message = "Your e-mail is verified. You can now login.";
                    return redirect()->route('american.plastic.web')->with('message', $message);
                } else {
                    $message = "Your e-mail is already verified. You can now login.";
                    return redirect()->route('american.plastic.web')->with('message', $message);
                }
            } else {
                $errormessage = 'Sorry your email cannot be identified.';
                return redirect()->route('american.plastic.web')->with('errormsg', $errormessage);

            }
        } catch (\Throwable $th) {
            DB::table('error_logs')->insert([
                'message' => $th->getMessage(),
                'file' => $th->getFile(),
                'line' => $th->getLine(),
                'trace' => $th->getTraceAsString(),
                'created_at' => now(),
            ]);

            $errormessage = 'Sorry your email cannot be identified.';
            return redirect()->route('american.plastic.web')->with('errormsg', $errormessage);
        }




    }
}
