<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\SettingController;
use App\Models\Saldo;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\BannerPromosi;
use App\Http\Api\fiver;
use Illuminate\Support\Str;

use App\Models\Setting;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
//use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;

class RegisterController extends Controller
{

  use RegistersUsers;

  public function index() {
    $banners = BannerPromosi::all(); // Mengambil semua data banner promosi
    return view('auth.register')->with('banners', $banners);
  }

  protected $redirectTo = RouteServiceProvider::HOME;

  public function __construct() {
    $this->middleware('guest');
  }

  public function checkExistingEmail(Request $request) {
    $email = $request->input('email');

    $existingEmail = User::where('email', $email)->exists();

    return response()->json(['exists' => $existingEmail]);
  }

  public function checkExistingMobile(Request $request) {
    $mobile = $request->input('mobile_no');

    $existingMobile = User::where('telp', $mobile)->exists();

    return response()->json(['exists' => $existingMobile]);
  }



  public function showCaptcha() {
    $captchaUrl = captcha_src();
    return view('auth.register', compact('captchaUrl'));
  }


  public function validateCaptcha() {
    $captchaValue = \Illuminate\Support\Facades\Request::input('captcha');
    $isCaptchaValid = captcha_check($captchaValue);
    if ($isCaptchaValid) {
      return response()->json(true);
    } else {
      return response()->json('refreshCaptcha');
    }
  }

  public function refreshCaptcha() {
    return captcha_src();
  }

  protected function validator(array $data) {
    return Validator::make($data, [
      'name' => ['required', 'string', 'max:255', 'unique:users'],
      'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
      'password' => ['required', 'string', 'min:8', 'confirmed'],

    ]);
  }

  protected function create(array $data)
{
    /* ===============================
     * CREATE USER
     * =============================== */
    $user = User::create([
        'name'      => $data['name'],
        'email'     => $data['email'],
        'password'  => Hash::make($data['password']),
        'telp'      => $data['mobile_no'],
        'ref_code'  => $data['ref_code'] ?? null,
        'method'    => $data['method'] ?? null,
        'nama_rek'  => $data['acc_name'] ?? null,
        'bank'      => $data['bank_name'] ?? null,
        'no_rek'    => $data['acc_no'] ?? null,
    ]);

    /* ===============================
     * CREATE SALDO
     * =============================== */
    Saldo::create([
        'user_id'  => $user->id,
        'saldo'    => 0,
        'bonus'    => 0,
        'referral' => 0,
    ]);

    /* ===============================
     * GENERATE EXTERNAL PLAYER ID
     * =============================== */
    $extplayer = 'U' . $user->id . strtoupper(Str::random(4));
    $user->extplayer = $extplayer;
    $user->save();

    /* ===============================
     * REGISTER USER KE GAME API
     * (TIDAK BOLEH GAGALKAN REGISTER)
     * =============================== */
    try {
        $api = new fiver();
        $api->createUser($extplayer);
    } catch (\Exception $e) {
        // LOG SAJA, JANGAN GAGALKAN REGISTER
        \Log::error('Game API register gagal: ' . $e->getMessage());
    }

    return $user;
}


  protected function registered($user) {

    session()->flash('success', 'Pendaftaran berhasil!');


    return redirect('/register-success');
  }
}