Module:Wikibase

local module = {}

-- Helper function to get the coordinate object
local function getCoordData()
    local entityId = mw.wikibase.getEntityIdForCurrentPage()
    if not entityId then return nil end
    local entity = mw.wikibase.getEntity(entityId)
    if not entity then return nil end
    local coordinateProperty = entity:getBestStatements('P20')[1]
    if coordinateProperty and coordinateProperty.mainsnak and coordinateProperty.mainsnak.datavalue then
        return coordinateProperty.mainsnak.datavalue.value
    end
    return nil
end

-- Returns JUST the latitude (e.g. "34.0522")
function module.getRawLatitude(frame)
    local coords = getCoordData()
    if coords then return coords.latitude end
    return ''
end

-- Returns JUST the longitude (e.g. "-118.2437")
function module.getRawLongitude(frame)
    local coords = getCoordData()
    if coords then return coords.longitude end
    return ''
end

-- Gets the P20 coordinates from the current page's item and builds a link to the interactive map.
function module.buildMapLink(frame)
    -- Check if we are running from a /sandbox page.
    local currentTitle = mw.title.getCurrentTitle().text
    local isSandbox = currentTitle:find('/sandbox', 1, true)
    local stylesheet = 'Module:Wikibase' .. (isSandbox and '/sandbox' or '') .. '/styles.css'
    local templatestyles = frame:extensionTag{ name = 'templatestyles', args = { src = stylesheet } }

    local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims or not entity.claims.P20 or not entity.claims.P20[1] then
        return ''
    end

    local claim = entity.claims.P20[1]
    if not claim.mainsnak or not claim.mainsnak.datavalue or not claim.mainsnak.datavalue.value then
        return ''
    end
    
    local coords = claim.mainsnak.datavalue.value
    local lat = tonumber(coords.latitude)
    local lon = tonumber(coords.longitude)
    local qid = entity.id

    if not lat or not lon or not qid then
        return ''
    end

    local url = string.format('https://digitalbahairesources.org/map?lat=%s&lon=%s&zoom=13&qid=%s', lat, lon, qid)

    local ns = lat >= 0 and 'N' or 'S'
    local ew = lon >= 0 and 'E' or 'W'
    local displayText = string.format('%.4f° %s, %.4f° %s', math.abs(lat), ns, math.abs(lon), ew)

    -- Build a normal external link, but wrap it so .coordinates styles apply.
    local wikitextLink = string.format('[%s %s]', url, displayText)
    local wrapped = string.format('<span class="coordinates plainlinks">Coordinates: %s</span>', wikitextLink)

    return templatestyles .. wrapped
end

-- Function builds a <display_map> tag, see Extension:Maps.
function module.buildLocationMap(frame)
    local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)

    -- Fetch coordinates and the full entity object
    local entity = mw.wikibase.getEntityObject()
    if not entity or not entity.claims or not entity.claims.P20 or not entity.claims.P20[1] then
        return '<span class="error">Error: No coordinates found for this page.</span>'
    end
    
    local coords = entity.claims.P20[1].mainsnak.datavalue.value
    local lat, lon, qid = coords.latitude, coords.longitude, entity.id
    if not lat or not lon or not qid then return '' end

    -- Get parameters from the template call, with sensible defaults
    local width = args.width or '300'
    local height = args.height or '250'
    local zoom = tonumber(args.zoom) or 10
    local align = args.align or 'right'
    local fullscreen = args.fullscreen

    -- *** FIX: Build the static URL for the page's main location first. ***
    -- This URL will be used for both the popup link and the default clicktarget.
    local fullMapUrl = string.format(
        'https://digitalbahairesources.org/map?lat=%s&lon=%s&zoom=13&qid=%s',
        lat, lon, qid
    )

    -- *** FIX: Use the static URL as the default clicktarget. ***
    -- This ensures that clicking anywhere on the map goes to the central point.
    local clicktarget = args.clicktarget
    if clicktarget == nil then
        clicktarget = fullMapUrl
    end

    -- Get the item's label for the popup title
    local title = entity:getLabel() or 'Location'

    -- Create the popup text with a link to the full map (using the same static URL)
    local text = string.format("(Click on the map to explore)", title, fullMapUrl)
    
    -- Construct the marker line. Icon is omitted to use the default.
    local marker = string.format('%s,%s~%s~%s', lat, lon, title, text)

    -- Build a table of attributes for the <display_map> tag.
    local mapAttrs = {
        string.format('width="%s"', width),
        string.format('height="%s"', height),
        string.format('zoom="%s"', zoom),
        string.format('centre="%s,%s"', lat, lon),
        'scrollwheelzoom="no"',
        string.format('clicktarget="%s"', clicktarget)
    }

    -- Add the fullscreen attribute ONLY if the user has requested it
    if fullscreen == 'yes' or fullscreen == 'on' then
        table.insert(mapAttrs, 'fullscreen="yes"')
    end
    
    -- Assemble the final <display_map> wikitext
    local mapWikitext = string.format(
        '<display_map %s>%s</display_map>',
        table.concat(mapAttrs, ' '),
        marker
    )

    -- Wrap the map in a div for alignment
    local wrapperDiv = string.format('<div class="float%s" style="width: %spx;">%s</div>', align, width, mapWikitext)

    -- Tell MediaWiki to parse the wikitext we just built
    return frame:preprocess(wrapperDiv)
end

return module