from django.conf import settings
from datetime import timedelta

import firebase_admin
from firebase_admin import messaging,credentials
from geographiclib import geodesic
from django.core.mail import send_mail
from django.conf import settings

from accounts.models import DriverLocation
from booking.models import RideAdditionalCharges
from django.utils import timezone
from datetime import timedelta
import logging
from django.conf import settings
from booking.notifications import send_firebase_notification, save_notification_history

# cred = credentials.Certificate('loop-driver-8a139-firebase-adminsdk-fbsvc-64d99f4fb3.json')
# firebase_admin.initialize_app(cred)



def sms(body, to_phone):
    from twilio.rest import Client

    account_sid =settings.TWILIO_ACCOUNT_SID
    auth_token = settings.TWILIO_AUTH_TOKEN
    fromph = settings.TWILIO_PHONE_NUMBER
    to_phone = str(to_phone)
    # Here we'll build a new Twilio_client with different credentials
    twilio_client = Client(account_sid, auth_token)

    try:
        message = twilio_client.messages.create(
            to=to_phone,
            from_=fromph,
            body=body)
        logging.info(f"Twilio SMS send to {to_phone}")
        print(f"Twilio SMS{message.sid} send to {to_phone}")
    except Exception as e:
        logging.info(f"Twilio SMS Error: {e}")





def notify_driver_suspend(user):
    title = "Account update"
    body = f"You've been taken offline. Contact support for details."
    device_token = user.devices.first().device_token
    data = {"type": "ACCOUNT_STATUS", "booking_id": f"{user.id}", "screen": "account_status", "status": user.is_active}
    save_notification_history(user, title, body, data, "Account Suspended")
    send_firebase_notification(device_token, title, body, data)


def notify_driver_account_reactivate(user):
    title = "Account active"
    body = f"You can go online and receive ride requests again."
    device_token = user.devices.first().device_token
    data ={"type": "ACCOUNT_STATUS", "booking_id": f"{user.id}", "screen": "dashboard", "status": user.is_active}
    save_notification_history(user, title, body, data, "Account Reactivated")
    send_firebase_notification(device_token, title, body, data)

def notify_driver_reactivate_email(user):
    email_subject = f"Your Loop driver account is active again"
    app_link = ""
    email_body = f"""
                Hi {user.first_name},\n\n

                Your driver account has been reactivated. You can go online again.\n
                Open the app: {app_link}

                Contact {settings.SUPPORT_EMAIL} for next steps\n\n.

                Drive safe,\n
                Team Loop\n
                Support: {settings.SUPPORT_EMAIL}\n


    """
    send_mail(
        email_subject,
        email_body,
        settings.DEFAULT_FROM_EMAIL,
        [user.email],
        fail_silently=False,
    )



def notify_user_account_reactivate(user):
    title = "Account reactivated"
    body = f"Welcome back. You can book rides again."
    device_token = user.devices.first().device_token
    data ={"type": "ACCOUNT_STATUS", "booking_id": f"{user.id}", "screen": "home", "status": "active"}
    save_notification_history(user, title, body, data, "Account Reactivated")
    send_firebase_notification(device_token, title, body, data)

def notify_user_reactivate_email(user):
    email_subject = f"Your Loop account is active again"
    app_link = ""
    email_body = f"""
                Hi {user.first_name},\n\n

                Your account has been reactivated. You can book rides again from the app.\n
                Open the app: {app_link}

                Contact {settings.SUPPORT_EMAIL} for next steps\n\n.


                Thank you,\n
                Team Loop\n
                Need help? {settings.SUPPORT_EMAIL}\n


    """
    send_mail(
        email_subject,
        email_body,
        settings.DEFAULT_FROM_EMAIL,
        [user.email],
        fail_silently=False,
    )



def notify_driver_suspend_email(user):
    email_subject = f"Your Loop driver account is Suspended"
    app_link = ""
    email_body = f"""
                Hi {user.first_name},\n\n

                Your account has been suspended and you cannot accept rides.\n\n

                Reason: "Suspected activity that violates our terms of service. \n\n"

                Contact {settings.SUPPORT_EMAIL} for next steps\n\n.

                Drive safe,\n
                Team Loop\n
                Support: {settings.SUPPORT_EMAIL}\n


    """
    send_mail(
        email_subject,
        email_body,
        settings.DEFAULT_FROM_EMAIL,
        [user.email],
        fail_silently=False,
    )


def notify_user_suspend(user):
    title = "Account update"
    body = f"Your Loop account has been restricted. Contact support for help."
    device_token = user.devices.first().device_token
    data = {"type": "ACCOUNT_STATUS", "booking_id": "{user.id}", "screen": "support", "status": user.is_active}
    save_notification_history(user, title, body, data, "Account Suspended")
    send_firebase_notification(device_token, title, body, data)

def notify_user_suspend_email(user):
    email_subject = f"Your Loop account status has changed"
    app_link = ""
    email_body = f"""
                Hi {user.first_name},

                Your account has been suspended.

                Reason: "Suspected activity that violates our terms of service."

                If you think this is a mistake, contact us at {settings.SUPPORT_EMAIL} with your registered mobile number.

                Thank you,
                Team Loop
                Need help? {settings.SUPPORT_EMAIL}
    """
    send_mail(
        email_subject,
        email_body,
        settings.DEFAULT_FROM_EMAIL,
        [user.email],
        fail_silently=False,
    )