
# from datetime import datetime, timezone
# from playwright.async_api import async_playwright, Playwright
# from playwright.sync_api import expect
 
from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)

baseclass.makedirpath(baseclass.ana_dalao_relation_by_solscan_path )

 
old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

 
 
arr_realtion_address =[]

now_time = time.time()
find_depth = "depth_01"

df = pd.read_excel(baseclass.dalao_tran_gmgnOrdex_path /
                "input_dalao.xlsx", dtype=object)
obj_ex_address ={
    
    "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1":"buy-achipompomhat.sol",
"CoXccZ3RqJGHQspgei4NdMCW2No1EtTCgevy9CYwaNey":"cryptoxiao.sol",
"6dEm5Z5NGqNuavGV2fmiRnE55V9jpygsy8RngLxszBGA":"coinbaseventures.sol"
}
arr_exclude_address= list(obj_ex_address.keys())

arr_str_dalaoaddress = df[f'dalao_{find_depth}'].drop_duplicates().dropna().tolist()
arr_str_dalaoaddress=[add for add in arr_str_dalaoaddress if add not in  arr_exclude_address]
# arr_str_dalaoaddress=arr_str_dalaoaddress[0:2]
arr_realtion_address  = arr_realtion_address + arr_str_dalaoaddress
str_tokenaddress_dalao_source = "4GULMPKBJLruChBZWksZzukAg1AjSCmCTMn9ny2Xpump"


baseclass.makedirpath(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source    )
baseclass.makedirpath(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source /find_depth )
baseclass.makedirpath(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source /find_depth/"mid" )
baseclass.makedirpath(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source /find_depth/"final" )


def drop_ex_txhash(gdf):
    is_ex = False
    if( ( gdf["action"] != "TRANSFER" )
        | (gdf["timestamp"]<now_time - 2*30* 24* 3600 )
        |( gdf["from"].isin(arr_exclude_address))
        |( gdf["to"].isin(arr_exclude_address))
        ).any( ):
        is_ex = True
    
    if is_ex:
        return None
    if (  set( gdf["from"].tolist() ).intersection(gdf["to"].tolist())   ):
        is_ex = True
    if is_ex:
        return None
    return gdf


def sum_same_tx_from_to(gdf ):
    gdf["amount"] = sum(gdf["amount"])
    gdf=gdf.drop_duplicates()
    return gdf

def sum_same_from_to(gdf ):
    gdf["timestamp"] = max(gdf["timestamp"] )
    gdf["datetime"] = max(gdf["datetime"] )
    gdf["amount"] = sum(gdf["amount"])
    gdf=gdf.drop_duplicates()
    return gdf

def handle_same_rel_address(gdf):
    gdf["amount"] = max(gdf["amount"])
    gdf["datetime"] = max(gdf["datetime"])
    gdf["timestamp"] = max(gdf["timestamp"] )
    gdf=gdf.drop_duplicates()
    return gdf

def get_dalao_len(gdf):
    gdf["sol_dalao_count"] = len(gdf)
    
    return  gdf


def main():
    global arr_exclude_address
    for str_dalaoaddress in arr_str_dalaoaddress:
        print(f"enter {str_dalaoaddress}")
        cur_arr_realtion_address =arr_realtion_address.copy()
        cur_arr_realtion_address.remove(str_dalaoaddress)
        

        df_special_token_transfer =pd.read_csv(baseclass.dalao_solscan_info_path / f"special_token_transfer_{str_dalaoaddress}.csv" ,dtype=object)
        df_special_token_transfer = df_special_token_transfer.astype({
            "Time":int,
        })
        
 

 
    
        df_special_token_transfer.columns=["txhash","timestamp","action","from","to","amount","decimals","tokenaddress"]
        df_special_token_transfer=df_special_token_transfer.astype({
            "timestamp":int,
            "amount":float,
            "decimals":int,
        })
        
    

        df_special_token_transfer = df_special_token_transfer.groupby(by=["txhash"],  group_keys=False).apply(lambda gdf: drop_ex_txhash(gdf))
 
 
        if(len(df_special_token_transfer)==0):
            continue
        token_decimal  =  (df_special_token_transfer["decimals"].tolist()[0])
        df_special_token_transfer["amount"] = df_special_token_transfer["amount"] / 10**token_decimal
        df_special_token_transfer["amount"] = df_special_token_transfer["amount"].round(2) 
        df_special_token_transfer["datetime"] = pd.to_datetime(df_special_token_transfer['timestamp'], unit='s')  
        df_special_token_transfer = df_special_token_transfer[["timestamp", "txhash", "datetime" ,"from","to","amount"]]     
        
        df_special_token_transfer = df_special_token_transfer.groupby(by = ["txhash","from","to" ],  group_keys=False).apply(lambda gdf: sum_same_tx_from_to(gdf))
        
        df_special_token_transfer=df_special_token_transfer.sort_values(by="timestamp",ascending=False)
        df_special_token_transfer.to_csv(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  "mid" /f"mid01_sol_transfer_{str_dalaoaddress}.csv",index=False)
 
        df_special_token_transfer = df_special_token_transfer[[ "timestamp", "datetime", "from","to","amount"]]       
        df_special_token_transfer = df_special_token_transfer.groupby(by = [ "from","to" ],  group_keys=False).apply(lambda gdf: sum_same_from_to(gdf))
        
        df_special_token_transfer["is_exend"] = 0
        df_special_token_transfer["is_relationend"] = 0


        df_special_token_transfer['dalaoaddress'] = str_dalaoaddress
        

        df_special_token_transfer.loc[( df_special_token_transfer["from"].isin(cur_arr_realtion_address) | df_special_token_transfer["to"].isin(cur_arr_realtion_address)),"is_relationend"] = 1
 
        df_special_token_transfer.loc[( df_special_token_transfer["from"].isin(arr_exclude_address) | df_special_token_transfer["to"].isin(arr_exclude_address)),"is_exend"] = 1
        df_special_token_transfer=df_special_token_transfer.sort_values(by="timestamp",ascending=False)
        df_special_token_transfer.to_csv(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  "mid" /f"mid02_sol_transfer_{str_dalaoaddress}.csv",index=False)
 
        df_special_token_transfer['rel_address'] = df_special_token_transfer.apply(lambda row: row['from'] if row['from']!= str_dalaoaddress else row['to'], axis=1)
        


        df_special_token_transfer["sol_dalao_count"] = 0
        df_special_token_transfer=df_special_token_transfer.groupby(by = [ "rel_address" ],  group_keys=False).apply(lambda gdf: handle_same_rel_address(gdf))
        df_special_token_transfer=df_special_token_transfer[ ['timestamp', 'datetime',   'amount', 'is_exend', 'is_relationend', 'dalaoaddress' , 'rel_address','sol_dalao_count'  ]]
        df_special_token_transfer=df_special_token_transfer.drop_duplicates()

        

        df_special_token_transfer =   df_special_token_transfer.groupby(by=["dalaoaddress"] ,group_keys=False  ).apply(lambda gdf:  get_dalao_len(gdf))
        

        df_special_token_transfer=df_special_token_transfer.sort_values(by="timestamp",ascending=False)
        df_special_token_transfer.to_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  "final"/f"final_sol_transfer_{str_dalaoaddress}.xlsx",index=False)
 
 
main()