<?php

namespace Modules\Auth\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Auth\Services\PhoneVerificationOtpService;
use Modules\Auth\Transformers\UserResource;

class PhoneVerificationController extends Controller
{
    public function __construct(
        protected PhoneVerificationOtpService $phoneVerificationOtpService,
    ) {}

    public function send(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
        ], [
            'phone_number.required' => 'Nombor telefon diperlukan.',
            'phone_number.regex' => 'Format nombor telefon tidak sah.',
        ]);

        $this->phoneVerificationOtpService->send($validated['phone_number']);

        return response()->json([
            'success' => true,
            'message' => 'Kod OTP telah dihantar ke nombor telefon anda.',
        ]);
    }

    public function verify(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
            'otp' => ['required', 'string', 'digits:6'],
        ], [
            'phone_number.required' => 'Nombor telefon diperlukan.',
            'phone_number.regex' => 'Format nombor telefon tidak sah.',
            'otp.required' => 'Kod OTP diperlukan.',
            'otp.digits' => 'Kod OTP mestilah 6 digit.',
        ]);

        $result = $this->phoneVerificationOtpService->verify(
            $validated['phone_number'],
            $validated['otp'],
        );

        return response()->json([
            'success' => true,
            'message' => 'Nombor telefon berjaya disahkan.',
            'data' => [
                'phone_number' => $result['phone_number'],
                'verification_token' => $result['verification_token'],
            ],
        ]);
    }

    public function sendForAuthenticatedUser(Request $request): JsonResponse
    {
        $user = $request->user();

        $validated = $request->validate([
            'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
        ], [
            'phone_number.required' => 'Nombor telefon diperlukan.',
            'phone_number.regex' => 'Format nombor telefon tidak sah.',
        ]);

        $this->phoneVerificationOtpService->sendForUser($user, $validated['phone_number']);

        return response()->json([
            'success' => true,
            'message' => 'Kod OTP telah dihantar ke nombor telefon anda.',
        ]);
    }

    public function verifyForAuthenticatedUser(Request $request): JsonResponse
    {
        $user = $request->user();
        $wasVerified = (bool) $user->phone_verified_at;

        $validated = $request->validate([
            'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
            'otp' => ['required', 'string', 'digits:6'],
        ], [
            'phone_number.required' => 'Nombor telefon diperlukan.',
            'phone_number.regex' => 'Format nombor telefon tidak sah.',
            'otp.required' => 'Kod OTP diperlukan.',
            'otp.digits' => 'Kod OTP mestilah 6 digit.',
        ]);

        $verifiedUser = $this->phoneVerificationOtpService->verifyForUser(
            $user,
            $validated['phone_number'],
            $validated['otp'],
        );

        return response()->json([
            'success' => true,
            'message' => $wasVerified
                ? 'Nombor telefon berjaya dikemas kini.'
                : 'Nombor telefon berjaya disahkan.',
            'data' => new UserResource($verifiedUser),
        ]);
    }
}
