# python3 test/test_sms.py 01027774695
import os
import requests
import time
import hmac
import hashlib
import base64
import json
import sys
from dotenv import load_dotenv


def get_env_variable(key, env_file):
    """Reads an environment variable from a given .env file."""
    with open(env_file, "r") as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("#"):
                k, v = line.split("=", 1)
                if k == key:
                    return v.strip()
    return None


def main():
    if len(sys.argv) < 2:
        print("Usage: python test/test_sms.py <recipient_phone_number>")
        sys.exit(1)

    recipient_phone = sys.argv[1]

    # Load environment variables from .env or .env.sample
    env_path = os.path.join(os.getcwd(), ".env")
    if not os.path.exists(env_path):
        print("Info: .env file not found. Using .env.sample as a fallback.")
        env_path = os.path.join(os.getcwd(), ".env.sample")

    if not os.path.exists(env_path):
        print("Error: No configuration file found (.env or .env.sample).")
        sys.exit(1)

    access_key = get_env_variable("NAVER_CLOUD_ACCESS_KEY", env_path)
    secret_key = get_env_variable("NAVER_CLOUD_SECRET_KEY", env_path)
    service_id = get_env_variable("NAVER_CLOUD_SERVICE_ID", env_path)
    sender_phone = get_env_variable("NAVER_SENDER_PHONE_NUMBER", env_path)

    if not all([access_key, secret_key, service_id, sender_phone]):
        print(
            f"Error: One or more required environment variables are missing in {env_path}."
        )
        sys.exit(1)

    timestamp = str(int(time.time() * 1000))
    api_path = f"/sms/v2/services/{service_id}/messages"
    api_url = f"https://sens.apigw.ntruss.com{api_path}"

    # Create the signature for the API request
    message = f"POST {api_path}\n{timestamp}\n{access_key}"
    h = hmac.new(bytes(secret_key, "UTF-8"), bytes(message, "UTF-8"), hashlib.sha256)
    signature = base64.b64encode(h.digest()).decode("UTF-8")

    # Construct the JSON request body
    json_body = {
        "type": "SMS",
        "contentType": "COMM",
        "countryCode": "82",
        "from": sender_phone,
        "content": "[Baron 로그인] Test message from Python script.",
        "messages": [{"to": recipient_phone}],
    }

    headers = {
        "Content-Type": "application/json; charset=utf-8",
        "x-ncp-apigw-timestamp": timestamp,
        "x-ncp-iam-access-key": access_key,
        "x-ncp-apigw-signature-v2": signature,
    }

    print("========================================")
    print("  Attempting to send SMS via SENS API (Python)")
    print("========================================")
    print(f"  Recipient: {recipient_phone}")
    print(f"  Timestamp: {timestamp}")
    print(f"  Service ID: {service_id}")
    print("========================================")
    print()

    try:
        response = requests.post(api_url, headers=headers, json=json_body)
        response.raise_for_status()  # Raise an exception for HTTP errors
        print("API Response:")
        print(json.dumps(response.json(), indent=2, ensure_ascii=False))
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
        if hasattr(e, "response") and e.response is not None:
            print("API Error Response:")
            try:
                print(json.dumps(e.response.json(), indent=2, ensure_ascii=False))
            except json.JSONDecodeError:
                print(e.response.text)
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

    print()
    print("========================================")
    print("  Request complete.")
    print("========================================")


if __name__ == "__main__":
    main()
