<?php
namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class HablameService
{
    public function send(string $phone, string $message): bool
    {
        try {
            $response = Http::timeout(10)->asForm()->post(env('SMS_URL'), [
                'account'  => env('SMS_ACCOUNT'),
                'apiKey'   => env('SMS_API_KEY'),
                'token'    => env('SMS_TOKEN'),
                'sc'       => env('SMS_SC'),
                'toNumber' => $phone,
                'sms'      => $message,
            ]);

            $body = $response->json();

            if (isset($body['status']) && $body['status'] === '1x000') {
                Log::info("SMS Háblame enviado a {$phone}");
                return true;
            }

            Log::warning('SMS Háblame: respuesta inesperada', ['body' => $body]);
            return false;

        } catch (\Throwable $e) {
            Log::error('SMS Háblame: excepción', ['msg' => $e->getMessage()]);
            return false;
        }
    }
}
