<?php

namespace App\Http\Controllers;

use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Requests\Auth\ResendVerificationCodeRequest;
use App\Http\Requests\Auth\VerifyRegistrationRequest;
use App\Models\User;
use App\Services\Auth\AuthService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function __construct(
        protected AuthService $authService
    ) {}

    public function register(RegisterRequest $request)
    {
        $result = $this->authService->register(
            $request->validated()
        );

        return response()->json([
            'success' => true,
            'message' => 'Register berhasil, silakan verifikasi kode.',
            'data' => [
                'user' => $result['user'],
                'verification' => $result['verification'],
            ],
        ], 201);
    }

    public function verifyRegistration(
        VerifyRegistrationRequest $request
    ) {
        $user = $this->authService->verifyRegistration(
            $request->validated()
        );

        return response()->json([
            'success' => true,
            'message' => 'Verifikasi berhasil, user sudah aktif.',
            'data' => $user,
        ]);
    }

    public function resendVerificationCode(
        ResendVerificationCodeRequest $request
    ) {
        $verification = $this->authService->resendVerificationCode(
            $request->validated()
        );

        return response()->json([
            'success' => true,
            'message' => 'Kode verifikasi baru berhasil dibuat.',
            'data' => [
                'verification' => $verification,
            ],
        ]);
    }

    public function login(Request $request)
    {
        $request->validate([
            'login' => 'required|string',
            'password' => 'required|string',
        ]);

        $user = User::where('email', $request->login)
            ->orWhere('username', $request->login)
            ->first();

        if (!$user || !Hash::check($request->password, $user->password)) {
            return response()->json([
                'success' => false,
                'message' => 'Username/email atau password salah.'
            ], 401);
        }

        if ($user->status !== 'active') {
            return response()->json([
                'success' => false,
                'message' => 'Akun belum aktif. Silakan verifikasi terlebih dahulu.'
            ], 403);
        }

        $token = $user->createToken('services-core')->plainTextToken;

        return response()->json([
            'success' => true,
            'token' => $token,
            'user' => $user,
        ]);
    }

    // public function login(Request $request)
    // {
    //     $request->validate([
    //         'email'    => 'required|email',
    //         'password' => 'required'
    //     ]);

    //     $user = User::where('email', $request->email)->first();

    //     if (!$user || !Hash::check($request->password, $user->password)) {

    //         return response()->json([
    //             'success' => false,
    //             'message' => 'Email atau password salah'
    //         ], 401);

    //     }

    //     if ($user->status !== 'active') {
    //         return response()->json([
    //             'success' => false,
    //             'message' => 'Akun belum aktif. Silakan verifikasi terlebih dahulu.'
    //         ], 403);
    //     }

    //     $token = $user->createToken('services-core')->plainTextToken;

    //     return response()->json([
    //         'success' => true,
    //         'token'   => $token,
    //         'user'    => $user
    //     ]);
    // }
}