<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class General extends Model
{
    use HasFactory;

    public static function passwordComplexity($password)
    {
        // $password = 'Fost0QAer{';
        $uppercase = [];
        $lowercase = [];
        $special_character = [];
        $number = [];
        $error_data = [];

        if (strlen($password) >= 12) {
            for ($i = 0; $i < strlen($password); $i++) {
                if (ctype_upper($password[$i])) {
                    array_push($uppercase, $password[$i]);
                } else if (ctype_lower($password[$i])) {
                    array_push($lowercase, $password[$i]);
                } elseif (ctype_punct($password[$i])) {
                    array_push($special_character, $password[$i]);
                } elseif (is_numeric($password[$i])) {
                    array_push($number, $password[$i]);
                }
            }

            if (count($uppercase) < 1) {
                $error_data[0] = 'Password must contain at least 1 uppercase';
            }
            if (count($lowercase) < 1) {
                $error_data[count($error_data)] = 'Password must contain at least 1 lowercase';
            }
            if (count($special_character) < 1) {
                $error_data[count($error_data)] = 'Password must contain at least 1 special charater';
            }
            if (count($number) < 1) {
                $error_data[count($error_data)] = 'Password must contain at least 1 number';
            }
        } else {
            $error_data[0] = 'Password must be a minimum of 12 characters';
        }
        return $error_data;
    }

    public static function generatePassword()
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $special_characters = ".,;/'?><][}$*(){";
        $special_characterLength = strlen($special_characters);
        $charactersLength = strlen($special_characters);
        $randomString = '';

        for ($i = 0; $i < 3; $i++) {
            $randomString .= $special_characters[random_int(0, $charactersLength - 1)];
        }
        for ($i = 0; $i < 9; $i++) {
            $randomString .= $characters[random_int(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    public static function admin_OTP($email)
    {
        $otp = rand(1000, 9999);
        AdminOtp::create([
            'otp' => $otp,
            'email' => $email,
        ]);
        return $otp;
        try {
        } catch (\Throwable $th) {
            Log::info($th->getMessage());
        }
    }

    public static function admin_otp_mail($subj, $mailto, $msg)
    {
        $curl = curl_init();
        $mailurl = env('MAIL_URL');
        $mailauth = env('MAIL_AUTH');
        curl_setopt_array($curl, array(
            CURLOPT_URL => $mailurl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => '{
    "subject":"' . $subj . '",
    "recipient":"' . $mailto . '",
    "msg":"' . $msg . '"
  }',
            CURLOPT_HTTPHEADER => array(
                $mailauth,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        Log::channel('coi')->info('curl executed and closed.');
        // echo $response;
        Log::channel('coi')->info('curl response: ' . $response);
    }
}
