# COSMOTOWN LOGIN LOGIN
# EMAIL: netcrestflow@outlook.com
# PASSWORD: Ajho188uh4M3#
# DOMAIN: 	vlgroupslimited.Ltd


# ASURA HOSTING ACCOUNT LOGIN
# EMAIL: jueljustice@gmail.com
# PASSWORD: 9nyc63xG6Psimpli2020

# Name Server Information:
# Nameserver 1: ns23.asurahosting.com 198.251.89.30 
# Nameserver 2: ns24.asurahosting.com (198.251.89.123)

# ==============================================
# Control Panel Login Information (cPanel):

# cPanel Username: vanline1
# cPanel Password: rs2P7rOi53
# cPanel Login URL: https://c6.asurahosting.com/cpanel 

# Once your domain has propogated, you may also use: www.vlgroupslimited.ltd:2082/
# ==============================================
# Temporary FTP Information:

# Temporary FTP Hostname: 198.251.89.30
# Username: vanline1
# Password: rs2P7rOi53


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import auth, messages
from django.core.mail import send_mail
from django.conf import settings
from django.urls import reverse
from django.shortcuts import render, redirect

from .models import ShippingDetail


def home_view(request):
    return render(request, "index.html")

def about_view(request):
    return render(request, "about.html")

def services_view(request):
    return render(request, "services.html")

def _normalize_code(raw: str) -> str:
    return raw.strip()

# /track/  (search form)
def track_search(request):
    if request.method == "POST":
        code = _normalize_code(request.POST.get("tracking_code"))
        if not code:
            messages.error(request, "Please enter a tracking code.")
            return redirect("logiskoapp:track_search")
        return redirect("logiskoapp:track_detail", code=code)
    # GET just renders the form; if redirected after a miss we’ll show messages and ?q=
    return render(request, "track.html")



def track_detail(request, code):
    code_norm = code.strip()

    try:
        s = ShippingDetail.objects.get(
            tracking_number__iexact=code_norm
        )
    except ShippingDetail.DoesNotExist:
        messages.error(
            request,
            "Tracking code not found. Please check and try again."
        )
        url = (
            f'{reverse("logiskoapp:track_search")}'
            f'?q={code_norm}'
        )
        return redirect(url)

    events = s.tracking_events.all()

    ctx = {
        "s": s,
        "events": events,
        "route_color": s.route_color,
        "is_moving": s.status == ShippingDetail.Status.MOVING,
    }

    return render(request, "track-result.html", ctx)



def contact_view(request):
    if request.method == "POST":
        name = request.POST.get("name")
        phone = request.POST.get("phone")    
        email = request.POST.get("email")
        transport_type = request.POST.get("transport_type")
        miles_range = request.POST.get("miles_range")
        item_description = request.POST.get("description")
        
        try:
            send_mail(
                "Message from your client",
                f"Name: {name}, email: {email}, phone: {phone}, Transportation means: {transport_type}, Shipping distance: {miles_range}miles, Item Description: {item_description}",
                settings.EMAIL_HOST_USER,
                ["info@vlgroupslimited.ltd",]
            )
        except:
            pass
        
        return redirect("/")
