<?php

namespace Modules\Client\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $request->validate([
            'phoneOrEmail' => 'required'
        ]);

        $user = User::query()
            ->when(filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL), function (Builder $query) use ($request) {
                $query->getQuery()->where('email', $request->phoneOrEmail);
            }, function (Builder $query) use ($request) {
                $query->getQuery()->where('phone', $request->phoneOrEmail);
            })
            ->first();

        if (!$user) {
            $message = filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL) ? "User dengan alamat email " . $request->phoneOrEmail . " tidak ditemukan" : "User dengan nomor telepon " . $request->phoneOrEmail . " tidak ditemukan";

            return Helper::responseJson(statusCode: Response::HTTP_NOT_FOUND, message: $message);
        }

        if (filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL)) {
            User::query()->find($user->id)->update([
                'email' => $request->phoneOrEmail,
                'otp' => 4444, //rand(1000, 9999),
                'otp_created_at' => now()
            ]);
        } else {
            User::query()->find($user->id)->update([
                'phone' => $request->phoneOrEmail,
                'otp' => 4444,//rand(1000, 9999),
                'otp_created_at' => now()
            ]);
        }

        // TODO Send the OTP
        if (filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL)) {
            // Send Email
        } else {
            // Send Whatsapp
        }

        return Helper::responseJson(message: 'OTP Terkirim');
    }

    public function validateOtp(Request $request)
    {
        $request->validate([
            'phoneOrEmail' => 'required',
            'otp' => 'required'
        ]);

        $user = User::query()
            ->when(filter_var($request->phoneOrEmail, FILTER_VALIDATE_EMAIL), function (Builder $query) use ($request) {
                $query->getQuery()->where('email', $request->phoneOrEmail);
            }, function (Builder $query) use ($request) {
                $query->getQuery()->where('phone', $request->phoneOrEmail);
            })
            ->first();

        if ($user->otp == $request->otp) {
            return Helper::responseJson(
                data: [
                    'token' => $user->createToken('app')->plainTextToken,
                    'user' => $user,
                ],
                message: 'Selamat Datang'
            );
        }

        return Helper::responseJson(status: 'error', message: 'OTP yang anda masukan salah!');
    }

    public function logout(Request $request)
    {
        $request->user()->currentAccessToken()->delete();

        return Helper::responseJson(message: 'Berhasil Logout.');
    }
}
