<?php

namespace App\Services;

use App\Models\User;
use App\Services\WhmcsServices;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;

class UserServices
{

    protected WhmcsServices $whmcsServices;

    public function __construct(WhmcsServices $whmcsServices)
    {
        $this->whmcsServices =  $whmcsServices;
    }

    public function save($user, $f2=''){
        $dates = Carbon::now()->format('Y-m-d');
        $date = explode('-', $dates);
        $year = $date[0];
        $month = $date[1];
        $day = $date[2];
        $random = rand(11111,99999);

        $ok = User::create([
            'name' => $user['name'],
            'lastname' => $user['lastname'],
            'email' => $user['email'],
            'country' => $user['country'],
            'address' => $user['address'],
            'city' => $user['city'],
            'state' => $user['state'],
            'post_code' => $user['post_code'],
            'phone_number' => $user['phone'],
            'company' => $user['company'],
            'google2fa_secret' => $f2 != '' ? $f2: '0',
            'googlef2_status' => $f2 != '' ? 1: '0',
            'google_date' => $f2 != '' ? Carbon::now()->format('Y-m-d'): '0',
            'password' => Hash::make($user['password']),
            'otp' => md5(session()->get('verify')),
            'role' => 'User',
            'ip_address' => $user['ip_address'],
            'register_year' => $year,
            'register_month' => $month,
            'register_day' => $day,
            'support_pin' => $random
        ]);

        if($ok){
            return $this->whmcsServices->saveClient($user);
        }
    }



    public function update($data) {
        auth()->user()->update($data);
        $this->whmcsServices->updateClient($data);
    }




}