"""Original sparse-rating example, not a production recommendation benchmark.

Only the 24 training triples update the model; six held-out ratings are scored
after fixed training. Content cosine scores are rankings, not rating predictions.
"""
import json
import platform

import numpy as np


ITEMS = ("A", "B", "C", "D", "E", "F", "G", "H", "NEW")
# Hand-set metadata, not learned collaborative coordinates.
FEATURES = np.array([[1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0],
                     [0, 0, 1], [0, 0, 1], [1, 1, 0], [0, 1, 1],
                     [1, 0, 1]], dtype=float)
TRAIN = np.array([
    [0, 0, 5], [0, 2, 1], [0, 4, 3], [0, 6, 4],
    [1, 1, 5], [1, 3, 1], [1, 5, 3], [1, 7, 2],
    [2, 0, 1], [2, 2, 5], [2, 4, 2], [2, 7, 4],
    [3, 1, 1], [3, 3, 5], [3, 5, 2], [3, 6, 3],
    [4, 0, 3], [4, 3, 2], [4, 5, 5], [4, 6, 2],
    [5, 1, 3], [5, 2, 2], [5, 4, 5], [5, 7, 4],
], dtype=float)
HELD_OUT = np.array([[0, 1, 5], [1, 0, 4], [2, 3, 4],
                     [3, 2, 5], [4, 4, 4], [5, 5, 5]], dtype=float)
N_USERS, N_SEEN_ITEMS, DIMENSION = 6, 8, 2
UPDATES, LEARNING_RATE, REGULARIZATION, SEED = 2000, 0.05, 0.02, 7


def initialize():
    rng = np.random.default_rng(SEED)
    return {"users": rng.normal(0, .1, (N_USERS, DIMENSION)),
            "items": rng.normal(0, .1, (N_SEEN_ITEMS, DIMENSION)),
            "user_bias": np.zeros(N_USERS), "item_bias": np.zeros(N_SEEN_ITEMS)}


def predict(parameters, triples, mean):
    users, items = triples[:, 0].astype(int), triples[:, 1].astype(int)
    return (mean + parameters["user_bias"][users] + parameters["item_bias"][items]
            + np.sum(parameters["users"][users] * parameters["items"][items], axis=1))


def loss_and_gradient(parameters, mean):
    # Explicit sparse mask: missing pairs never enter this squared rating loss.
    users, items = TRAIN[:, 0].astype(int), TRAIN[:, 1].astype(int)
    error = predict(parameters, TRAIN, mean) - TRAIN[:, 2]
    mse = float(np.mean(error**2))
    penalty = sum(float(np.mean(value**2)) for value in parameters.values())
    gradient = {name: 2 * REGULARIZATION * value / value.size
                for name, value in parameters.items()}
    coefficient = 2 * error / len(TRAIN)
    np.add.at(gradient["users"], users,
              coefficient[:, None] * parameters["items"][items])
    np.add.at(gradient["items"], items,
              coefficient[:, None] * parameters["users"][users])
    np.add.at(gradient["user_bias"], users, coefficient)
    np.add.at(gradient["item_bias"], items, coefficient)
    return mse + REGULARIZATION * penalty, mse, gradient


def train():
    parameters = initialize()
    mean = float(np.mean(TRAIN[:, 2]))
    initial_loss, initial_mse, _ = loss_and_gradient(parameters, mean)
    for _ in range(UPDATES):
        _, _, gradient = loss_and_gradient(parameters, mean)
        # Simultaneous full-batch gradient descent, using all old parameters.
        parameters = {name: value - LEARNING_RATE * gradient[name]
                      for name, value in parameters.items()}
    final_loss, final_mse, _ = loss_and_gradient(parameters, mean)
    return parameters, mean, {"initial_objective": initial_loss,
        "final_objective": final_loss, "initial_training_MSE": initial_mse,
        "final_training_MSE": final_mse}


def content_scores(user):
    history = TRAIN[TRAIN[:, 0] == user]
    if not len(history):
        return None
    centered = history[:, 2] - history[:, 2].mean()
    profile = np.sum(centered[:, None] * FEATURES[history[:, 1].astype(int)], axis=0)
    norm = np.linalg.norm(profile)
    if norm == 0:
        return None
    return FEATURES @ profile / (np.linalg.norm(FEATURES, axis=1) * norm)


def main():
    parameters, mean, record = train()
    item_means = np.array([TRAIN[TRAIN[:, 1] == i, 2].mean()
                           for i in range(N_SEEN_ITEMS)])
    truths = HELD_OUT[:, 2]
    predicted = {"global_training_mean": np.full(len(truths), mean),
                 "item_training_mean": item_means[HELD_OUT[:, 1].astype(int)],
                 "biased_matrix_factorization": predict(parameters, HELD_OUT, mean)}
    evaluation = {name: {"predictions": values.tolist(),
        "RMSE": float(np.sqrt(np.mean((values-truths)**2)))}
        for name, values in predicted.items()}
    seen = {int(i) for u, i, r in TRAIN if u == 0}
    candidates = [i for i in range(N_SEEN_ITEMS) if i not in seen]
    candidate_triples = np.array([[0, i, 0] for i in candidates], dtype=float)
    mf_scores = predict(parameters, candidate_triples, mean)
    content = content_scores(0)
    ranking_mf = sorted(zip(candidates, mf_scores), key=lambda pair: (-pair[1], pair[0]))
    content_candidates = [i for i in range(len(ITEMS)) if i not in seen]
    ranking_content = sorted(content_candidates, key=lambda i: (-content[i], i))
    assert all(np.isfinite(value).all() for value in parameters.values())
    assert set(map(tuple, TRAIN[:, :2])).isdisjoint(set(map(tuple, HELD_OUT[:, :2])))
    print(json.dumps({"scope": "Hand-set explicit ratings and binary item metadata; "
        "actual observed-only matrix-factorization training, no external dataset.",
        "versions": {"python": platform.python_version(), "numpy": np.__version__},
        "settings": {"users": N_USERS, "seen_items": N_SEEN_ITEMS, "dimension": DIMENSION,
            "updates": UPDATES, "learning_rate": LEARNING_RATE,
            "regularization": REGULARIZATION, "seed": SEED,
            "objective": "mean observed squared error + .02*sum(mean(square(each parameter array)))",
            "prediction": "fixed training mean + user_bias + item_bias + user_vector dot item_vector"},
        "training_triples": TRAIN.tolist(), "held_out_triples": HELD_OUT.tolist(),
        "item_names": ITEMS, "item_features": FEATURES.tolist(),
        "training_mean": mean, "training_item_means": item_means.tolist(),
        "training_record": record, "final_parameters": {k: v.tolist() for k, v in parameters.items()},
        "held_out_evaluation": evaluation,
        "user0_unseen_known_item_rating_predictions": [{"item": ITEMS[i], "score": float(s)}
            for i, s in ranking_mf],
        "user0_content_cosine_ranking_including_NEW": [{"item": ITEMS[i], "score": float(content[i])}
            for i in ranking_content],
        "cold_start": {"new_item": "NEW has metadata but no collaborative factor; no MF score emitted.",
            "new_user": "No history means content profile undefined and MF user ID unknown; "
                        "a non-personalized fallback may use training item means.",
            "new_user_content_profile": content_scores(N_USERS),
            "fallback_item_mean_order": [ITEMS[i] for i in sorted(range(N_SEEN_ITEMS),
                key=lambda i: (-item_means[i], i))]},
        "limits": ["Explicit ratings: missing pairs are unobserved, not observed zero-star ratings.",
            "This observed-only loss does not train a binary implicit-feedback recommender.",
            "Fixed settings selected before computing six held-out errors; no tuning on them.",
            "Single seed and 2000 updates do not establish convergence or best model performance.",
            "Six held-out pairs from known IDs are not new-user/new-item generalization.",
            "Content cosine values are not rating predictions or calibrated probabilities.",
            "RMSE is not top-k ranking quality; no ranking or online recommendation benefit measured.",
            "Toy factors have no guaranteed semantic axes; coordinates are not identifiable.",
            "No external rating dataset, movie posters, or third-party user data used."]},
        ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
