<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Config;
use App\Models\User;

class EmailVerificationController extends BaseController
{
    /**
     * Send verification email
     */
    public function sendVerificationEmail(Request $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return $this->sendResponse([], 'Email sudah terverifikasi.');
        }

        // Generate verification URL
        $verificationUrl = $this->generateVerificationUrl($request->user());

        // Send custom verification email
        $request->user()->notify(new \App\Notifications\VerifyEmailNotification($verificationUrl));

        return $this->sendResponse([
            'verification_url' => $verificationUrl
        ], 'Link verifikasi email telah dikirim.');
    }

    /**
     * Verify email
     */
    public function verify(Request $request, $id)
    {
        if (!URL::hasValidSignature($request)) {
            return response()->view('emails.verification-error', [
                'message' => 'Link verifikasi tidak valid atau sudah kadaluarsa.'
            ], 400);
        }

        $user = User::find($id);

        if (!$user) {
            return response()->view('emails.verification-error', [
                'message' => 'Pengguna tidak ditemukan.'
            ], 404);
        }

        if ($user->hasVerifiedEmail()) {
            return response()->view('emails.verification-success', [
                'message' => 'Email sudah diverifikasi sebelumnya.',
                'user' => $user
            ]);
        }

        if ($user->markEmailAsVerified()) {
            event(new Verified($user));
        }

        return response()->view('emails.verification-success', [
            'message' => 'Email berhasil diverifikasi.',
            'user' => $user
        ]);
    }

    /**
     * Resend verification email
     */
    public function resend(Request $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return $this->sendResponse([], 'Email sudah terverifikasi.');
        }

        // Generate verification URL
        $verificationUrl = $this->generateVerificationUrl($request->user());

        // Send custom verification email
        $request->user()->notify(new \App\Notifications\VerifyEmailNotification($verificationUrl));

        return $this->sendResponse([
            'verification_url' => $verificationUrl
        ], 'Link verifikasi email telah dikirim ulang.');
    }

    /**
     * Generate verification URL
     */
    protected function generateVerificationUrl($user)
    {
        // Generate verification URL yang langsung mengarah ke API backend
        return URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60), // Link expires in 60 minutes
            ['id' => $user->id, 'hash' => sha1($user->email)]
        );
    }
}
