"""Synthetic return-expression repairs. The first option is the bug; the second is the reference."""

from dataclasses import dataclass
import random


@dataclass(frozen=True)
class Task:
    slug: str
    args: str
    contract: str
    expressions: tuple
    visible: tuple
    held_out: tuple

    def options(self):
        order = list(self.expressions)
        random.Random(self.slug).shuffle(order)
        return dict(zip("ABC", order))


TASKS = (
    Task("clamp", "x, lo, hi", "Clamp x to the inclusive interval [lo, hi].",
         ("min(lo, max(hi, x))", "max(lo, min(hi, x))", "min(hi, x)"),
         (((9, 0, 5), 5),), (((-2, 0, 5), 0), ((3, 0, 5), 3))),
    Task("ceil-div", "x, y", "Return ceil(x / y) for integers and positive y.",
         ("x // y", "-(-x // y)", "(x + y) // y"),
         (((7, 3), 3),), (((6, 3), 2), ((-7, 3), -2))),
    Task("center", "x, lo, hi", "Return the signed distance from the midpoint.",
         ("x - (hi - lo) / 2", "x - (hi + lo) / 2", "abs(x - (hi + lo) / 2)"),
         (((9, 4, 10), 2.0),), (((5, 4, 10), -2.0),)),
    Task("page-start", "page, size", "Return the zero-based start offset for a one-based page.",
         ("page * size", "(page - 1) * size", "max(0, page - size)"),
         (((2, 10), 10),), (((1, 10), 0), ((4, 3), 9))),
    Task("percent-change", "old, new", "Return signed fractional change. The old value is positive.",
         ("new / old", "(new - old) / old", "abs(new - old) / old"),
         (((10, 15), 0.5),), (((10, 5), -0.5), ((5, 5), 0.0))),
    Task("last-n", "xs, n", "Return the last n elements. For n=0 return an empty list.",
         ("xs[-n:]", "xs[-n:] if n else []", "xs[:n]"),
         ((([1, 2, 3], 0), []),), ((([1, 2, 3], 2), [2, 3]),)),
    Task("drop-last", "xs, n", "Drop the last n elements. For n=0 return the whole list.",
         ("xs[:-n]", "xs[:-n] if n else xs", "xs[n:]"),
         ((([1, 2, 3], 0), [1, 2, 3]),), ((([1, 2, 3], 1), [1, 2]),)),
    Task("interleave", "xs", "Return elements at even zero-based indices.",
         ("xs[1::2]", "xs[::2]", "xs[::3]"),
         ((([2, 4, 6, 8],), [2, 6]),), ((([],), []), (([1],), [1]))),
    Task("middle", "xs", "Return the lower middle element of a nonempty list.",
         ("xs[len(xs) // 2]", "xs[(len(xs) - 1) // 2]", "xs[(len(xs) + 1) // 2]"),
         ((([10, 20, 30, 40],), 20),), ((([1, 2, 3],), 2), (([9],), 9))),
    Task("dedupe", "xs", "Remove duplicates while preserving first occurrence order.",
         ("sorted(set(xs))", "list(dict.fromkeys(xs))", "list(dict.fromkeys(reversed(xs)))"),
         ((([3, 1, 3, 2],), [3, 1, 2]),), ((([2, 1, 2, 1],), [2, 1]),)),
    Task("prefix", "s, prefix", "Remove one exact leading prefix when present.",
         ("s.lstrip(prefix)", "s.removeprefix(prefix)", "s.replace(prefix, '', 1)"),
         ((("aaaa", "aa"), "aa"),), ((("x-ab", "ab"), "x-ab"),)),
    Task("suffix", "s, suffix", "Remove one exact trailing suffix when present.",
         ("s.rstrip(suffix)", "s.removesuffix(suffix)", "s.replace(suffix, '', 1)"),
         ((("aaaa", "aa"), "aa"),), ((("ab-x", "ab"), "ab-x"),)),
    Task("field", "s", "Return everything after the first colon, or an empty string if absent.",
         ("s.split(':')[-1]", "s.partition(':')[2]", "s.split(':')[1]"),
         ((("a:b:c",), "b:c"),), ((("abc",), ""), (("a:",), ""))),
    Task("normalize-space", "s", "Trim and collapse all whitespace runs to one space.",
         ("s.strip()", "' '.join(s.split())", "s.replace('  ', ' ').strip()"),
         (((" a\t b ",), "a b"),), ((("\n  ",), ""), (("a    b",), "a b"))),
    Task("word-count", "s", "Count whitespace-separated words.",
         ("len(s.split(' '))", "len(s.split())", "s.count(' ') + 1"),
         (((" a  b ",), 2),), ((("",), 0), (("a\tb",), 2))),
    Task("window-intersection", "a, b, c, d", "Return the overlap length of half-open intervals [a,b) and [c,d).",
         ("min(b, d) - max(a, c)", "max(0, min(b, d) - max(a, c))", "max(0, max(b, d) - min(a, c))"),
         (((0, 3, 8, 9), 0),), (((0, 5, 2, 6), 3), ((0, 2, 2, 4), 0))),
    Task("around", "x, center, radius", "Return whether x lies within radius, including both endpoints.",
         ("abs(x - center) < radius", "abs(x - center) <= radius", "x - center <= radius"),
         (((5, 3, 2), True),), (((0, 3, 2), False), ((1, 3, 2), True))),
    Task("mean", "xs", "Return the arithmetic mean, or 0.0 for an empty list.",
         ("sum(xs) / len(xs)", "sum(xs) / len(xs) if xs else 0.0", "sum(xs) / max(1, len(xs)) if xs else None"),
         ((([],), 0.0),), ((([2, 3],), 2.5),)),
    Task("ratio", "hits, total", "Return hits / total, or 0.0 when total is zero.",
         ("hits / total", "hits / total if total else 0.0", "hits / max(1, total) if total else None"),
         (((0, 0), 0.0),), (((1, 4), 0.25),)),
    Task("bucket", "offset, width", "Return the zero-based bucket for an offset, using floor division.",
         ("int(offset / width)", "offset // width", "(offset + width - 1) // width"),
         (((-1, 5), -1),), (((5, 5), 1), ((6, 5), 1))),
)
