#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

// Twilio credentials
#define ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
#define AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
#define TWILIO_PHONE_NUMBER "+YOUR_TWILIO_PHONE_NUMBER"

// Function to calculate electricity bill based on slabs
float calculateElectricityBill(int units) {
    float rate;
    if (units <= 100) {
        rate = 3.00;
    } else if (units <= 200) {
        rate = 4.50;
    } else if (units <= 300) {
        rate = 6.50;
    } else {
        rate = 8.00;
    }
    return units * rate;
}

// Function to calculate phone bill based on call minutes
float calculatePhoneBill(int callMinutes) {
    float rate = 1.00;  // ₹1 per minute
    return callMinutes * rate;
}

// URL Encoding function for safe transmission
char* url_encode(const char* str) {
    CURL *curl = curl_easy_init();
    if (curl) {
        char *output = curl_easy_escape(curl, str, 0);
        curl_easy_cleanup(curl);
        return output;
    }
    return NULL;
}

// Function to send SMS using Twilio API
void sendSMS(const char *recipientPhoneNumber, const char *customerName, float electricityBill, float phoneBill) {
    CURL *curl;
    CURLcode res;

    // Construct the SMS message
    char message[512];
    snprintf(message, sizeof(message),
             "Dear %s, your electricity bill is ₹%.2f and your phone bill is ₹%.2f. Total amount due: ₹%.2f. Thank you!",
             customerName, electricityBill, phoneBill, electricityBill + phoneBill);

    // URL encode the message
    char *encodedMessage = url_encode(message);

    // Construct the URL dynamically
    char url[256];
    snprintf(url, sizeof(url),
             "https://a...content-available-to-author-only...o.com/2010-04-01/Accounts/%s/Messages.json",
             ACCOUNT_SID);

    // Prepare POST fields
    char postFields[1024];
    snprintf(postFields, sizeof(postFields),
             "To=%s&From=%s&Body=%s",
             recipientPhoneNumber, TWILIO_PHONE_NUMBER, encodedMessage);

    // Initialize cURL
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_USERNAME, ACCOUNT_SID);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, AUTH_TOKEN);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields);

        // Disable SSL verification (TEMPORARY FIX)
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

        // Perform the request, and check for errors
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            printf("\nSMS sent successfully to %s!\n", recipientPhoneNumber);
        }

        // Cleanup
        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    if (encodedMessage) {
        curl_free(encodedMessage);
    }
}

int main() {
    char customerName[50];
    char recipientPhoneNumber[15];
    int electricityUnits, phoneMinutes;
    float electricityBill, phoneBill, totalBill;

    // Input customer name
    printf("Enter customer name: ");
    fgets(customerName, sizeof(customerName), stdin);
    customerName[strcspn(customerName, "\n")] = '\0';  // Remove newline character

    // Input customer phone number
    printf("Enter recipient phone number (in E.164 format, e.g., +919876543210): ");
    fgets(recipientPhoneNumber, sizeof(recipientPhoneNumber), stdin);
    recipientPhoneNumber[strcspn(recipientPhoneNumber, "\n")] = '\0';  // Remove newline character

    // Input electricity usage
    printf("Enter electricity units consumed: ");
    scanf("%d", &electricityUnits);

    // Input phone usage
    printf("Enter phone call minutes used: ");
    scanf("%d", &phoneMinutes);

    // Calculate bills
    electricityBill = calculateElectricityBill(electricityUnits);
    phoneBill = calculatePhoneBill(phoneMinutes);
    totalBill = electricityBill + phoneBill;

    // Display bill summary
    printf("\n--- Bill Summary ---\n");
    printf("Customer Name: %s\n", customerName);
    printf("Electricity Bill: ₹%.2f\n", electricityBill);
    printf("Phone Bill: ₹%.2f\n", phoneBill);
    printf("Total Bill Amount: ₹%.2f\n", totalBill);

    // Send SMS notification
    sendSMS(recipientPhoneNumber, customerName, electricityBill, phoneBill);

    return 0;
}
