# MIT. Per-ticket records only; uc-stats computes all aggregate metrics.
CATEGORIES = ['billing', 'access', 'bug', 'feature', 'unknown']
PRIORITIES = ['P1', 'P2', 'P3']
TEAMS = ['finance', 'identity', 'engineering', 'product', 'support']


def score(item, arguments, config):
    gold = item['labels']
    args = arguments if isinstance(arguments, dict) else {}
    prediction = {field: args.get(field) if args.get(field) in values else 'invalid'
                  for field, values in [('category', CATEGORIES), ('priority', PRIORITIES), ('team', TEAMS)]}
    correct = prediction['category'] == gold['category']
    p1 = gold['priority'] == 'P1'
    secondary = {
        'category_accuracy': {'gold': gold['category'], 'prediction': prediction['category']},
        'priority_accuracy': {'gold': gold['priority'], 'prediction': prediction['priority']},
        'priority_f1': {'gold': gold['priority'], 'prediction': prediction['priority']},
        'team_accuracy': {'gold': gold['team'], 'prediction': prediction['team']},
        'team_f1': {'gold': gold['team'], 'prediction': prediction['team']},
        'p1_recall': {'tp': int(p1 and prediction['priority'] == 'P1'), 'fp': 0,
                      'fn': int(p1 and prediction['priority'] != 'P1')},
        'priority_off_by_one': {'score': int(prediction['priority'] in PRIORITIES and
            abs(PRIORITIES.index(prediction['priority']) - PRIORITIES.index(gold['priority'])) == 1)},
        'invalid_enum': {'score': int('invalid' in prediction.values())},
        'exact_match': {'score': int(all(prediction[k] == gold[k] for k in gold))},
    }
    # Precision over membership counts is conditional accuracy, not a pooled
    # denominator: empty confidence/stratum buckets are explicitly zero_division=0.
    for bucket in ['low', 'medium', 'high']:
        member = args.get('confidence') == bucket
        secondary['confidence_' + bucket] = {'tp': int(member and correct), 'fp': int(member and not correct), 'fn': 0}
    for key in ['seed_template', 'ambiguous', 'long_thread', 'non_english', 'no_detail']:
        member = item['strata'][key]
        secondary[key + '_accuracy'] = {'tp': int(member and correct), 'fp': int(member and not correct), 'fn': 0}
    return {'gold': gold['category'], 'prediction': prediction['category'], 'secondary': secondary}
