"""
The shared logged in browser for Phase 2.

One Chrome, one saved profile on this Mac, kept alive between runs so the daily
read does not need Steven each time. He logs into each site once. The cookies
live in data/browser_profile and stay on the machine.

Nothing here places a bid. It reads screens only. No platform API is used.
"""

import os

PROFILE_DIR = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data", "browser_profile"
)

# The sites we drive. Steven logs into each one once in the saved profile.
SITES = {
    "motorway": {
        "name": "Motorway",
        "login_url": "https://pro.motorway.co.uk/vehicles",
        # Steven's saved search "Normal search", captured as URL filters: age 2
        # to 10, price up to 11000, within 210 miles, mileage up to 90000,
        # grades 1 to 3, owners up to 4, auction only. This pre filters the
        # stock to his brief at source. If he changes the saved search, re
        # capture this URL by applying it once and reading the address bar.
        "stock_url": ("https://pro.motorway.co.uk/vehicles?ageFrom=2&ageTo=10"
                      "&displayPriceTo=11000&maxDistance=210&mileageTo=90000"
                      "&numericGrade=1&numericGrade=2&numericGrade=3"
                      "&previousKeepersCountTo=4&sellerType=private&listType=auction"),
        "base_url": "https://pro.motorway.co.uk",
    },
    "carwow": {
        "name": "Carwow",
        "login_url": "https://dealers.carwow.co.uk/dealers/login",
        # Steven's saved Carwow filter (id 1741) matching his brief. If he edits
        # or replaces it, re capture by applying the filter and reading the
        # address bar for the saved_filter_id.
        "stock_url": "https://dealers.carwow.co.uk/dealers/listings/filtered/stock?saved_filter_id=1741",
        "base_url": "https://dealers.carwow.co.uk",
    },
    "glass": {
        "name": "Glass's",
        "login_url": "https://uk.glass.co.uk/auth/login",
        "base_url": "https://uk.glass.co.uk",
    },
    "cazana": {
        "name": "Cazana",
        "login_url": "https://trade.percayso-vehicle-intelligence.co.uk/",
        "base_url": "https://trade.percayso-vehicle-intelligence.co.uk",
    },
}

# A normal looking desktop Chrome. Headed by default so it behaves like a real
# browser and does not trip automation blocks. The daily run can go headless
# once each site is proven.
LAUNCH_ARGS = {
    "headless": False,
    "viewport": {"width": 1440, "height": 900},
    "args": ["--disable-blink-features=AutomationControlled"],
}


def open_context(playwright, headless=None):
    """Open the persistent context that carries Steven's logins. Only one
    process may use the profile at a time, so never run two of these at once.
    Used for the login walkthrough."""
    os.makedirs(PROFILE_DIR, exist_ok=True)
    opts = dict(LAUNCH_ARGS)
    if headless is not None:
        opts["headless"] = headless
    return playwright.chromium.launch_persistent_context(PROFILE_DIR, **opts)


def state_path(site_key):
    """Where a site's saved session state lives, for sites whose login cookie
    does not survive a browser restart (Carwow)."""
    return os.path.join(os.path.dirname(PROFILE_DIR), f"{site_key}_state.json")


def open_reader_context(playwright, site_key, headless=True):
    """Open a context for reading a site. If we saved a session state for this
    site (because its login is session only, like Carwow), load that so the run
    is logged in. Otherwise fall back to the persistent profile (Motorway)."""
    sp = state_path(site_key)
    if os.path.exists(sp):
        b = playwright.chromium.launch(
            headless=headless, args=["--disable-blink-features=AutomationControlled"]
        )
        ctx = b.new_context(storage_state=sp, viewport=LAUNCH_ARGS["viewport"])
        # The launched browser is a separate object from its context. Callers
        # only ever call ctx.close(), which closes the context but leaves the
        # whole Chrome process running. Over a daily run that is one leaked
        # browser per valuation, which piled up to hundreds of processes and
        # hung the run. So make closing the context also close its browser.
        _orig_close = ctx.close

        def _close_with_browser(*args, **kwargs):
            try:
                _orig_close(*args, **kwargs)
            finally:
                try:
                    b.close()
                except Exception:
                    pass

        ctx.close = _close_with_browser
        return ctx
    return open_context(playwright, headless=headless)
