<?php

declare(strict_types=1);

namespace App\Services;

use App\Enums\Status;
use Illuminate\Support\Facades\Log;
use Kavenegar;
use Kavenegar\Exceptions\ApiException;
use Kavenegar\Exceptions\HttpException;
use Mortezaa97\SmsManager\Traits\SmsLogger;
use Psr\Log\LoggerInterface;
use InvalidArgumentException;
use Throwable;

class SmsService
{
    /**
     * Sends a verification SMS using Kavenegar.
     *
     * @param string $receptor The recipient phone number.
     * @param string $template The SMS template name.
     * @param string|null $paramOne First template parameter.
     * @param string|null $paramTwo Second template parameter.
     * @param string|null $paramThree Third template parameter.
     * @return bool True if SMS sent successfully, false otherwise.
     * @throws InvalidArgumentException If receptor is invalid.
     */
    public function sendVerifyWithKavenegar($receptor, $template, $paramOne = null, $paramTwo = null, $paramThree = null, $param_10 = null, $param_20 = null): bool {
        try {
            $result = Kavenegar::VerifyLookup($receptor,$paramOne,$paramTwo,$paramThree,$template,null,$param_10,$param_20);

            if ($result && is_array($result) && !empty($result)) {
                $result = reset($result); // Assume single result; use first if multiple
                $status = $result->status ?? null;
                SmsLogger::Log($result->message, $receptor, $result->sender, $result->cost, $template);
                if ($status === 1 || $status === '1') {
                    return true;
                }
                return false;
            }
            return false;

        } catch (\Exception $e) {
            throw $e;
        }
    }
}