# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py:percent
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.16.4
#   kernelspec:
#     display_name: torch
#     language: python
#     name: python3
# ---

# %%
import pandas as pd

# Load the preprocessed data CSV file
file_path = '../../data_import/exports/raw_data.csv'  # Adjust this path to your actual file location
data = pd.read_csv(file_path, dtype=str)

# Initialize a counter for the total number of changes
total_changes = 0

# Initialize a dictionary to count changes per ships_idx
ships_idx_changes = {}

# Process each group by ships_idx
for ships_idx, group in data.groupby('ships_idx'):
    # Find duplicated tag_descriptions within the group
    duplicated_descriptions = group['tag_description'].duplicated(keep=False)
    
    # Count how many tag_descriptions are duplicated within this ships_idx
    num_changes = duplicated_descriptions.sum()

    # If there are any duplicates
    if num_changes > 0:
        # Increment the total changes count
        total_changes += num_changes
        
        # Record the number of changes for this ships_idx
        ships_idx_changes[ships_idx] = num_changes

        # Apply the concatenation of tag_name to tag_description for duplicates
        data.loc[duplicated_descriptions & (data['ships_idx'] == ships_idx), 'tag_description'] = \
            data['tag_name'] + ' ' + data['tag_description']

# Output the changes per ships_idx
for ships_idx, count in ships_idx_changes.items():
    print(f"Changes made in ships_idx {ships_idx}: {count}")

# Output the total number of changes
print(f"Total number of changes made: {total_changes}")

# Optionally, save the updated DataFrame back to a CSV
output_file_path = 'outputs/raw_data_add_tag.csv'
data.to_csv(output_file_path, index=False, encoding='utf-8-sig')

print(f"Updated data saved to {output_file_path}")


# %%
