package com.pm.sms;

import ch.qos.logback.classic.Logger;

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

// 网线通道 - API方式
public class ApiSmsSender implements SmsSender {
    private static final String API_URL = "https://sms.provider.com/send";
    
    @Override
    public boolean sendAlert(String phone, String message) {
        try {
            HttpClient client = HttpClient.newHttpClient();
            String jsonBody = String.format(
                "{\"key\":\"YOUR_API_KEY\",\"phone\":\"%s\",\"msg\":\"%s\"}", 
                phone, URLEncoder.encode(message, "UTF-8"));
            
            HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(API_URL))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();
            
            HttpResponse<String> response = client.send(
                request, HttpResponse.BodyHandlers.ofString());
            
            return response.statusCode() == 200;
        } catch (Exception e) {
            Logger log = null;
            log.error("API短信发送失败", e);
            return false;
        }
    }
}