Documentation for this module may be created at Module:Repost/doc

-- Module:Repost
-- Templates for external article reposts (Mirror.xyz, Paragraph.com, etc.)
-- Handles repost notices, article styling, and image formatting

local p = {}

-- Helper: get frame arguments
local function getArgs(frame)
    local args = {}
    local parent = frame:getParent()
    for k, v in pairs(parent.args) do
        if v ~= '' then args[k] = v end
    end
    for k, v in pairs(frame.args) do
        if v ~= '' then args[k] = v end
    end
    return args
end

-- Known platforms with their display names and URLs
local platforms = {
    goldenlight = {
        name = 'goldenlight.mirror.xyz',
        url = 'https://goldenlight.mirror.xyz',
        category = 'Goldenlight reposts'
    },
    mirror = {
        name = 'Mirror.xyz',
        url = 'https://mirror.xyz',
        category = 'Mirror reposts'
    },
    paragraph = {
        name = 'Paragraph.com',
        url = 'https://paragraph.xyz',
        category = 'Paragraph reposts'
    }
}

-- Goldenlight repost notice box (legacy compatibility)
-- Parameters: url (required), date (required), title (optional)
function p.goldenlight(frame)
    local args = getArgs(frame)
    local url = args.url or args[1] or ''
    local date = args.date or args[2] or ''
    local title = args.title or args[3]

    if url == '' or date == '' then
        return '<span class="error">Goldenlight repost: url and date are required</span>'
    end

    local platform = platforms.goldenlight

    local html = mw.html.create('div')
        :addClass('repost-notice')
        :addClass('repost-goldenlight')

    local icon = html:tag('div'):addClass('repost-notice-icon')
    icon:wikitext('ℹ️')

    local content = html:tag('div'):addClass('repost-notice-content')
    content:tag('div')
        :addClass('repost-notice-title')
        :wikitext("'''Reposted from [" .. platform.url .. " " .. platform.name .. "]'''")

    local desc = 'This article was originally published on ' .. date .. '. '
    desc = desc .. '[' .. url .. ' View original article]'
    if title then
        desc = desc .. " titled ''" .. title .. "''"
    end
    desc = desc .. '.'

    content:tag('div')
        :addClass('repost-notice-text')
        :wikitext(desc)

    return tostring(html) .. '[[Category:' .. platform.category .. ']]'
end

-- Generic repost notice for any platform
-- Parameters: platform (optional), source, sourceUrl, url (required), date (required), title (optional)
function p.notice(frame)
    local args = getArgs(frame)

    -- Check for known platform shorthand
    local platformKey = args.platform and string.lower(args.platform)
    local platform = platforms[platformKey]

    local source = args.source or (platform and platform.name) or 'external source'
    local sourceUrl = args.sourceUrl or args.sourceurl or (platform and platform.url)
    local url = args.url or args[1] or ''
    local date = args.date or args[2] or ''
    local title = args.title or args[3]
    local category = args.category or (platform and platform.category)

    if url == '' then
        return '<span class="error">Repost notice: url is required</span>'
    end

    local html = mw.html.create('div')
        :addClass('repost-notice')

    if platformKey then
        html:addClass('repost-' .. platformKey)
    end

    local icon = html:tag('div'):addClass('repost-notice-icon')
    icon:wikitext('ℹ️')

    local content = html:tag('div'):addClass('repost-notice-content')

    local titleText = "'''Reposted from "
    if sourceUrl then
        titleText = titleText .. '[' .. sourceUrl .. ' ' .. source .. ']'
    else
        titleText = titleText .. source
    end
    titleText = titleText .. "'''"

    content:tag('div')
        :addClass('repost-notice-title')
        :wikitext(titleText)

    local desc = ''
    if date ~= '' then
        desc = 'This article was originally published on ' .. date .. '. '
    else
        desc = 'This article was originally published externally. '
    end
    desc = desc .. '[' .. url .. ' View original article]'
    if title then
        desc = desc .. " titled ''" .. title .. "''"
    end
    desc = desc .. '.'

    content:tag('div')
        :addClass('repost-notice-text')
        :wikitext(desc)

    local result = tostring(html)
    if category then
        result = result .. '[[Category:' .. category .. ']]'
    end

    return result
end

-- Article body wrapper (opening tag)
-- Creates a styled container for reposted article content
function p.article(frame)
    return '<div class="repost-article-body">'
end

-- Article body wrapper (closing tag)
function p.articleEnd(frame)
    return '</div>'
end

-- Image with caption (repost style)
-- Parameters: file (required), caption (required), alt, width, align
function p.image(frame)
    local args = getArgs(frame)
    local file = args.file or args[1] or ''
    local caption = args.caption or args[2] or ''
    local alt = args.alt or caption
    local width = args.width or '600'
    local align = args.align or 'center'

    if file == '' then
        return '<span class="error">Repost image: file is required</span>'
    end

    -- Validate alignment
    local validAligns = { left = true, right = true, center = true, none = true }
    if not validAligns[align] then
        align = 'center'
    end

    local html = mw.html.create('div')
        :addClass('repost-image')
        :addClass('repost-image-' .. align)

    -- Image
    local imgWikitext = '[[File:' .. file .. '|' .. width .. 'px|alt=' .. alt .. ']]'
    html:tag('div')
        :addClass('repost-image-file')
        :wikitext(imgWikitext)

    -- Caption
    if caption ~= '' then
        local captionDiv = html:tag('div')
            :addClass('repost-caption')
        captionDiv:css('max-width', width .. 'px')
        captionDiv:wikitext(caption)
    end

    return tostring(html)
end

return p