package com.toshibabd.customermanagement;

import android.net.Uri;
import android.util.Log;
import com.toshibabd.customermanagement.model.Customer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

/**
 * Handles communication with the SMS Gateway API using OkHttp, bypassing the PHP server.
 * Uses the secure HTTPS endpoint and proven parameter names.
 */
public class SmsSender {

    private static final String TAG = "SmsSender";

    // ⭐️ API Key (Your Credentials)
    private static final String SMS_API_KEY = "c8605ed1f98049dc";

    // ⭐️ SECRET KEY (Your Credentials)
    private static final String SMS_SECRET_KEY = "bd2f98ac";

    // ⭐️ SENDER ID (Using "FLOWTECH" as it's typically safer than "FLOW TECH" in URLs)
    private static final String SMS_SENDER_ID = "FLOW TECH";

    // ⭐️ SECURE BASE URL (Your Working Endpoint)
    private static final String SMS_BASE_URL = "https://smpp.ajuratech.com:7790/sendtext?";

    // Client for network requests
    private final OkHttpClient client = new OkHttpClient();

    /**
     * Sends a custom text message via SMS API directly from the Android app.
     * @param customer The customer object to get the mobile number.
     * @param text The custom message text to send.
     * @return true if the API call returns a successful HTTP status and the service reports "ACCEPTD", false otherwise.
     */
    public boolean sendCustomSms(Customer customer, String text) {
        String recipientNumber = customer.getMobileNumber();

        // 1. URL Encode the message text
        String encodedMessage = Uri.encode(text);

        // 2. Construct the full API URL using the correct parameter names (callerID, toUser, messageContent)
        String url = SMS_BASE_URL +
                "apikey=" + SMS_API_KEY +
                "&secretkey=" + SMS_SECRET_KEY +
                "&callerID=" + SMS_SENDER_ID +
                "&toUser=" + recipientNumber +
                "&messageContent=" + encodedMessage;

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        try (Response response = client.newCall(request).execute()) {
            String responseBody = "";
            if (response.body() != null) {
                responseBody = response.body().string();
            }

            if (response.isSuccessful()) {
                // Check for the known success response string
                Log.d(TAG, "SMS response to " + recipientNumber + ": " + responseBody);
                return responseBody.contains("ACCEPTD");
            } else {
                Log.e(TAG, "SMS API Error (" + response.code() + ") to " + recipientNumber + ": " + responseBody);
                return false;
            }
        } catch (IOException e) {
            Log.e(TAG, "SMS Network Failure to " + recipientNumber + ": " + e.getMessage());
            return false;
        }
    }
}