"""Public deal share-link landing page (V2 §2 direction — step 10).

A deal shared via link/QR opens .../d/<code>. This page tries the app's
custom URI scheme first (kitongapay://deal/<code>); if the app isn't
installed, the attempt silently does nothing and the visitor sees the
fallback: a plain-text preview plus store links. No auth, no full deal
detail (amount/title only) — this URL may be forwarded to anyone.
"""

from django.conf import settings
from django.shortcuts import render

from escrow.models import Deal


def deal_open(request, code):
    deal = Deal.objects.filter(code=code).select_related("seller").first()

    joinable = bool(
        deal is not None
        and deal.buyer_id is None
        and deal.status in {Deal.Status.DRAFT, Deal.Status.AWAITING_PAYMENT}
    )
    context = {
        "code": code,
        "deep_link": f"{settings.MOBILE_DEEP_LINK_SCHEME}://deal/{code}",
        "app_store_url": settings.APP_STORE_URL,
        "play_store_url": settings.PLAY_STORE_URL,
        "deal": deal,
        "joinable": joinable,
    }
    return render(request, "core/deal_open.html", context)
