def load_actress_data(file_path):
    actress_data = {}
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            name, dates = line.strip().split('｜')
            actress_data[name.strip()] = dates.strip()
    return actress_data

def write_section(f, header, data):
    f.write(f"[{header}]\n")
    for line in sorted(data):
        f.write(line + '\n')
    f.write('\n')

def main():
    # Load data from files
    actress_data = load_actress_data('./result/vixen-actress.txt')
    blacked_actress_data = load_actress_data('./result/blacked-actress.txt')
    tushy_actress_data = load_actress_data('./result/tushy-actress.txt')

    # Prepare results
    all_three = []
    actress_tushy = []
    actress_blacked = []
    tushy_blacked = []
    only_actress = []
    only_tushy = []
    only_blacked = []

    all_actresses = set(actress_data.keys())
    blacked_actresses = set(blacked_actress_data.keys())
    tushy_actresses = set(tushy_actress_data.keys())

    # Find actresses that are in all three files
    common_three = all_actresses & blacked_actresses & tushy_actresses
    for name in common_three:
        all_three.append(f"{name} ｜ {actress_data[name]} & {tushy_actress_data[name]} & {blacked_actress_data[name]}")

    # Find actresses that are in actress.txt and tushy-actress.txt
    common_actress_tushy = all_actresses & tushy_actresses - blacked_actresses
    for name in common_actress_tushy:
        actress_tushy.append(f"{name} ｜ {actress_data[name]} & {tushy_actress_data[name]}")

    # Find actresses that are in actress.txt and blacked-actress.txt
    common_actress_blacked = all_actresses & blacked_actresses - tushy_actresses
    for name in common_actress_blacked:
        actress_blacked.append(f"{name} ｜ {actress_data[name]} & {blacked_actress_data[name]}")

    # Find actresses that are in tushy-actress.txt and blacked-actress.txt
    common_tushy_blacked = tushy_actresses & blacked_actresses - all_actresses
    for name in common_tushy_blacked:
        tushy_blacked.append(f"{name} ｜ {tushy_actress_data[name]} & {blacked_actress_data[name]}")

    # Find actresses that are only in actress.txt
    only_in_actress = all_actresses - tushy_actresses - blacked_actresses
    for name in only_in_actress:
        only_actress.append(f"{name} ｜ {actress_data[name]}")

    # Find actresses that are only in tushy-actress.txt
    only_in_tushy = tushy_actresses - all_actresses - blacked_actresses
    for name in only_in_tushy:
        only_tushy.append(f"{name} ｜ {tushy_actress_data[name]}")

    # Find actresses that are only in blacked-actress.txt
    only_in_blacked = blacked_actresses - all_actresses - tushy_actresses
    for name in only_in_blacked:
        only_blacked.append(f"{name} ｜ {blacked_actress_data[name]}")

    # Write results to output file
    with open('./result/tvb-actress-all.txt', 'w', encoding='utf-8') as output_file:
        write_section(output_file, 'all', all_three)
        write_section(output_file, 'vixen-actress and tushy-actress', actress_tushy)
        write_section(output_file, 'vixen-actress and blacked-actress', actress_blacked)
        write_section(output_file, 'tushy-actress and blacked-actress', tushy_blacked)
        write_section(output_file, 'only in vixen-actress', only_actress)
        write_section(output_file, 'only in tushy-actress', only_tushy)
        write_section(output_file, 'only in blacked-actress', only_blacked)

if __name__ == "__main__":
    main()