## Likeability score
## ───────────────────
## Column settings:
##   Column heading : Likeability
##   Lookup name    : likeability
##   Column type    : Column built from other columns
##
## Estimates engagement level based on kudos/hits ratio,
## weighted by word count and chapter count.
##
## Requires: #kudos, #hits, #words, #chapters columns
## (mapped from FFF metadata — kudos and hits require
## custom columns mapped in the FFF Custom Columns tab).
##
## Returns empty if any required field is missing or zero.
##
## ⚠ The score is relative, not absolute. It is not comparable
## across fandoms — a niche fandom with 200 readers will score
## very differently from a mainstream one. Most useful for sorting
## within a single fandom or pairing.

program:
k = field('#kudos');
h = field('#hits');
w = field('#words');
c = field('#chapters');

## Return empty if any value is missing or zero
if or(k == '', h == '', w == '', c == '', k <=# 0, h <=# 0, w <=# 0, c <=# 0)
then ''
else
    ## Chapter factor: rewards multi-chapter works
    cf = switch_if(
        c <=# 1,  1,
        c <=# 3,  1.4,
        c <=# 8,  2.2,
        c <=# 20, 3.5,
        5
    );

    ## Word factor: rewards longer works
    wf = switch_if(
        w <# 5000,   0.8,
        w <# 20000,  1,
        w <# 60000,  1.15,
        w <# 150000, 1.3,
        1.45
    );

    ## Score: (k² × cf × wf) / h, scaled to a readable range
    round(divide(multiply(divide(multiply(multiply(k, k), multiply(cf, wf)), h), 100), 1000))
fi
