import json
import sys

def filter_students(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        data = json.load(f)

    # Group by ma_sv
    students_by_ma_sv = {}
    for row in data:
        ma_sv = row.get('ma_sv')
        if not ma_sv:
            continue
            
        if ma_sv not in students_by_ma_sv:
            students_by_ma_sv[ma_sv] = []
        students_by_ma_sv[ma_sv].append(row)

    filtered_data = []
    
    for ma_sv, records in students_by_ma_sv.items():
        chosen_record = None
        
        # Try to find a record with 'lop_cn' or 'lop_chuyen_nganh'
        for record in records:
            lop = record.get('lop_cn') or record.get('lop_chuyen_nganh')
            if lop and str(lop).strip():
                chosen_record = record
                break
                
        # If none found with class, just pick the first one
        if not chosen_record:
            chosen_record = records[0]
            
        filtered_data.append(chosen_record)

    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(filtered_data, f, ensure_ascii=False, indent=2)
        
    print(f"Original records: {len(data)}")
    print(f"Unique students after filtering: {len(filtered_data)}")

if __name__ == "__main__":
    input_file = "ALL_xuất điểm đợt học 08032026.json"
    output_file = "ALL_xuất điểm đợt học 08032026_filtered.json"
    filter_students(input_file, output_file)
