Dokumentasi untuk modul ini dapat dibuat di Modul:MatchResult/doc

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    
    -- Branding & Laga Eksklusif
    local derby = args.laga_spesial or args.derby or ""
    local comp = args.kompetisi or args.competition or ""
    local phase = args.fase or args.babak or ""
    local date = args.tanggal or args.date or ""
    
    -- Informasi Stadion & Info Tambahan
    local venue = args.stadion or args.venue or ""
    local referee = args.wasit or args.referee or ""
    local attendance = args.penonton or args.attendance or ""
    local weather = args.cuaca or args.weather or ""
    
    -- Tim & Papan Skor
    local homeTeam = args.tuan_rumah or args.home or "Tuan Rumah"
    local awayTeam = args.tamu or args.away or "Tamu"
    local homeScore = args.skor_home or args.home_score or "0"
    local awayScore = args.skor_away or args.away_score or "0"
    local status = args.status or "FT"
    local aggregate = args.agregat or args.aggregate or ""

    -- Detail Pencetak Gol & Kejadian
    local homeGoals = args.gol_home or args.home_goals or ""
    local awayGoals = args.gol_away or args.away_goals or ""

    -- STATISTIK SUPER LENGKAP
    local homeXG = args.xg_home or ""
    local awayXG = args.xg_away or ""
    local homePoss = args.possession_home or ""
    local awayPoss = args.possession_away or ""
    
    -- Tembakan
    local homeShotsTotal = args.shots_total_home or ""
    local awayShotsTotal = args.shots_total_away or ""
    local homeShotsOn = args.shots_on_home or ""
    local awayShotsOn = args.shots_on_away or ""
    local homeShotsOff = args.shots_off_home or ""
    local awayShotsOff = args.shots_off_away or ""
    local homeShotsBlocked = args.shots_blocked_home or ""
    local awayShotsBlocked = args.shots_blocked_away or ""

    -- Operan & Penguasaan
    local homePasses = args.passes_home or ""
    local awayPasses = args.passes_away or ""
    local homePassAcc = args.pass_acc_home or ""
    local awayPassAcc = args.pass_acc_away or ""

    -- Pertahanan & Pertandingan
    local homeCorners = args.corners_home or ""
    local awayCorners = args.corners_away or ""
    local homeOffsides = args.offsides_home or ""
    local awayOffsides = args.offsides_away or ""
    local homeSaves = args.saves_home or ""
    local awaySaves = args.saves_away or ""
    local homeFouls = args.fouls_home or ""
    local awayFouls = args.fouls_away or ""
    local homeTackles = args.tackles_home or ""
    local awayTackles = args.tackles_away or ""
    local homeThrowIns = args.throwins_home or ""
    local awayThrowIns = args.throwins_away or ""
    
    -- Kartu (Otomatis)
    local homeYellow = args.kartu_kuning_home or args.yellow_home or ""
    local awayYellow = args.kartu_kuning_away or args.yellow_away or ""
    local homeRed = args.kartu_merah_home or args.red_home or ""
    local awayRed = args.kartu_merah_away or args.red_away or ""

    -- Penentuan Warna Indikator Hasil
    local hScoreNum = tonumber(homeScore) or 0
    local aScoreNum = tonumber(awayScore) or 0
    local statusBg = "#6c757d" -- Seri
    if hScoreNum > aScoreNum then
        statusBg = "#16a34a" -- Home Win
    elseif hScoreNum < aScoreNum then
        statusBg = "#dc2626" -- Away Win
    end

    local html = {}
    
    -- Container Utama
    table.insert(html, '<div style="border: 1px solid #cbd5e1; border-radius: 10px; background: #ffffff; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, sans-serif; margin: 8px 0; box-shadow: 0 2px 6px rgba(0,0,0,0.08);">')
    
    -- 1. HEADER EKSKLUSIF (Laga Spesial / Derby / Tournament)
    if derby ~= "" then
        table.insert(html, string.format('<div style="background: linear-gradient(135deg, #0f172a 0%%, #1e1b4b 50%%, #312e81 100%%); color: #fbbf24; padding: 6px 8px; font-size: 85%%; text-align: center; font-weight: 800; letter-spacing: 1px; text-transform: uppercase; border-bottom: 2px solid #fbbf24;">🏆 %s 🏆</div>', derby))
    end

    -- Header Kompetisi & Tanggal
    if comp ~= "" or date ~= "" or phase ~= "" then
        local headerText = comp
        if phase ~= "" then
            headerText = (headerText ~= "" and (headerText .. " • ") or "") .. phase
        end
        if date ~= "" then
            headerText = (headerText ~= "" and (headerText .. " • ") or "") .. date
        end
        table.insert(html, string.format('<div style="background: #1e293b; color: #cbd5e1; padding: 4px 8px; font-size: 78%%; text-align: center; font-weight: 600;">%s</div>', headerText))
    end

    -- 2. PAPAN SKOR UTAMA
    table.insert(html, '<table style="width: 100%; border-collapse: collapse; text-align: center; margin: 10px 0; background: transparent;">')
    table.insert(html, string.format([[
        <tr>
            <td style="width: 38%%; font-weight: 800; font-size: 92%%; color: #0f172a; padding: 2px 6px; vertical-align: middle;">%s</td>
            <td style="width: 24%%; padding: 2px; vertical-align: middle;">
                <span style="background: %s; color: #ffffff; padding: 4px 10px; border-radius: 6px; font-weight: 900; font-size: 125%%; display: inline-block; letter-spacing: 1.5px; box-shadow: 0 2px 4px rgba(0,0,0,0.15);">%s - %s</span>
                <div style="font-size: 70%%; color: #64748b; margin-top: 4px; font-weight: 800; text-transform: uppercase;">%s</div>
                %s
            </td>
            <td style="width: 38%%; font-weight: 800; font-size: 92%%; color: #0f172a; padding: 2px 6px; vertical-align: middle;">%s</td>
        </tr>
    ]], homeTeam, statusBg, homeScore, awayScore, status, (aggregate ~= "" and string.format('<div style="font-size: 68%%; color: #475569; margin-top: 2px;">(Agg: %s)</div>', aggregate) or ""), awayTeam))
    
    -- Baris Gol (Ikon Otomatis)
    if homeGoals ~= "" or awayGoals ~= "" then
        table.insert(html, string.format([[
            <tr style="font-size: 75%%; color: #334155; vertical-align: top;">
                <td style="padding: 4px 6px; text-align: right; line-height: 1.4;">%s</td>
                <td style="padding: 4px 0; font-size: 85%%;">⚽</td>
                <td style="padding: 4px 6px; text-align: left; line-height: 1.4;">%s</td>
            </tr>
        ]], homeGoals, awayGoals))
    end
    table.insert(html, '</table>')

    -- Helper Baris Statistik
    local function makeStatRow(label, hVal, aVal, isBold)
        if hVal ~= "" or aVal ~= "" then
            local weight = isBold and "800" or "700"
            return string.format(
                '<div style="display: flex; justify-content: space-between; align-items: center; padding: 3px 0; border-bottom: 1px solid #f1f5f9;">' ..
                '<span style="font-weight: %s; width: 30%%; text-align: right; color: #0f172a;">%s</span>' ..
                '<span style="color: #64748b; width: 40%%; text-align: center; font-size: 88%%;">%s</span>' ..
                '<span style="font-weight: %s; width: 30%%; text-align: left; color: #0f172a;">%s</span>' ..
                '</div>', 
                weight, hVal ~= "" and hVal or "0", label, weight, aVal ~= "" and aVal or "0"
            )
        end
        return ""
    end

    -- Format Kartu Otomatis
    local function formatCards(yellow, red)
        local parts = {}
        if yellow ~= "" and yellow ~= "0" then
            table.insert(parts, string.format('%s <span style="background: #eab308; color: #fff; padding: 1px 4px; border-radius: 3px; font-size: 75%%; font-weight: bold;">K</span>', yellow))
        end
        if red ~= "" and red ~= "0" then
            table.insert(parts, string.format('%s <span style="background: #ef4444; color: #fff; padding: 1px 4px; border-radius: 3px; font-size: 75%%; font-weight: bold;">M</span>', red))
        end
        if #parts == 0 then return "" end
        return table.concat(parts, " &nbsp;")
    end

    local homeCardsFormatted = formatCards(homeYellow, homeRed)
    local awayCardsFormatted = formatCards(awayYellow, awayRed)

    -- 3. TABEL STATISTIK SUPER LENGKAP
    local hasStats = homePoss ~= "" or homeShotsOn ~= "" or homeXG ~= "" or homeCorners ~= "" or homeFouls ~= "" or homeCardsFormatted ~= "" or homePasses ~= ""
    
    if hasStats then
        table.insert(html, '<div style="border-top: 1px solid #e2e8f0; padding: 6px 10px; font-size: 75%%; background: #f8fafc;">')
        table.insert(html, '<div style="text-align: center; font-weight: 800; color: #334155; margin-bottom: 6px; font-size: 82%%; letter-spacing: 0.5px; text-transform: uppercase;">STATISTIK PERTANDINGAN</div>')
        
        table.insert(html, makeStatRow("Ekspektasi Gol (xG)", homeXG, awayXG, true))
        table.insert(html, makeStatRow("Penguasaan Bola", homePoss, awayPoss, true))
        table.insert(html, makeStatRow("Total Tembakan", homeShotsTotal, awayShotsTotal))
        table.insert(html, makeStatRow("Tembakan Akurat", homeShotsOn, awayShotsOn, true))
        table.insert(html, makeStatRow("Tembakan Meleset", homeShotsOff, awayShotsOff))
        table.insert(html, makeStatRow("Tembakan Diblok", homeShotsBlocked, awayShotsBlocked))
        table.insert(html, makeStatRow("Total Operan", homePasses, awayPasses))
        table.insert(html, makeStatRow("Akurasi Operan", homePassAcc, awayPassAcc))
        table.insert(html, makeStatRow("Tendangan Sudut", homeCorners, awayCorners))
        table.insert(html, makeStatRow("Offside", homeOffsides, awayOffsides))
        table.insert(html, makeStatRow("Penyelamatan Kiper", homeSaves, awaySaves))
        table.insert(html, makeStatRow("Tekel Berhasil", homeTackles, awayTackles))
        table.insert(html, makeStatRow("Lemparan Dalam", homeThrowIns, awayThrowIns))
        table.insert(html, makeStatRow("Pelanggaran", homeFouls, awayFouls))
        table.insert(html, makeStatRow("Kartu Disiplin", homeCardsFormatted, awayCardsFormatted, true))
        
        table.insert(html, '</div>')
    end

    -- 4. FOOTER INFORMASI LAGA
    if venue ~= "" or referee ~= "" or attendance ~= "" or weather ~= "" then
        table.insert(html, '<div style="border-top: 1px solid #e2e8f0; padding: 6px 8px; font-size: 70%%; color: #64748b; background: #ffffff; text-align: center; line-height: 1.5;">')
        local details = {}
        if venue ~= "" then table.insert(details, "<b>Stadion:</b> " .. venue) end
        if referee ~= "" then table.insert(details, "<b>Wasit:</b> " .. referee) end
        if attendance ~= "" then table.insert(details, "<b>Penonton:</b> " .. attendance) end
        if weather ~= "" then table.insert(details, "<b>Cuaca:</b> " .. weather) end
        table.insert(html, table.concat(details, " | "))
        table.insert(html, '</div>')
    end

    table.insert(html, '</div>')

    return table.concat(html, "")
end

return p