import sys
from pathlib import Path

# aggiunge ../src al PYTHONPATH
ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
sys.path.insert(0, str(SRC))


import unittest
from pucktrick.noisy import *
from pucktrick.utils import * 
import pandas as pd
from pandas.testing import assert_frame_equal
class TestCategorical(unittest.TestCase):
    def test_saluta(self):
        percentage=0.5
        num_rows = 20
        selection_criteria = "f4=='apple'"
        selection_criteria='all'
        fake_df=create_fake_table(num_rows)
        column=['f3']
        print("orginal")
        print(fake_df)
        strategy =  {'affected_features': column, 'selection_criteria': selection_criteria, 'percentage': percentage, 'mode': 'new', 'perturbate_data': {'distribution': 'random', 'param': None, 'value': None, 'condition_logic': None}}
        error,noise_df=noise(fake_df,strategy)
        if error==1:
                print("error")
        print(" primo noisy")
        print(noise_df)
        
        percentage=0.7
        strategy =  {'affected_features': column, 'selection_criteria': selection_criteria, 'percentage': percentage, 'mode': 'extended', 'perturbate_data': {'distribution': 'random', 'param': None, 'value': None, 'condition_logic': None}}
        error,noise1_df=noise(noise_df,strategy,fake_df)
        print("error "+str(error))
        if error==0:
            noise_df=noise1_df
        print("secondo noisy")
        print(noise_df)
        if selection_criteria=='all':
            dif = fake_df[column] != noise_df[column]
            diff_number = dif.sum().sum()/len(column)
            print(diff_number)
            diff_p=diff_number/num_rows
            print(diff_p)
        else:
            selection_criteria,isAffected=affectedSelected(column,selection_criteria)
            if isAffected==1:
                df_pref2=fake_df[column].add_prefix("puck_")
                fake_df=pd.concat([fake_df,df_pref2],axis=1)
                noise_df=pd.concat([noise_df,df_pref2],axis=1)
            dif = fake_df.query(selection_criteria)[column] != noise_df.query(selection_criteria)[column]
            diff_number = dif.sum().sum()/len(column)
            diff_p=diff_number/len(fake_df.query(selection_criteria))
    
        # Verifica che il numero di modifiche sia corretto
        self.assertLessEqual(abs(diff_p-percentage),0.15)
        


if __name__ == "__main__":
    unittest.main()
