
# 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---------------")


df = pd.read_excel(baseclass.dalao_tran_gmgnOrdex_path /
                "input_dalao.xlsx", dtype=object)
arr_str_dalaoaddress = df['dalaoaddress'].tolist()

# arr_str_dalaoaddress=arr_str_dalaoaddress[0:5]
str_tokenaddress_dalao_source = "4GULMPKBJLruChBZWksZzukAg1AjSCmCTMn9ny2Xpump"
find_depth = "depth_01"




def main():
    df_sol_transfer = None
    for str_dalaoaddress in arr_str_dalaoaddress:
        if not (baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  "final"/f"final_sol_transfer_{str_dalaoaddress}.xlsx" ).exists():
            continue
        print(f"enter {str_dalaoaddress}")
        cur_df_sol_transfer = pd.read_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/"final"/ f"final_sol_transfer_{str_dalaoaddress}.xlsx",dtype=object )
        cur_df_sol_transfer["dalaoaddress"] = str_dalaoaddress
        df_sol_transfer = pd.concat([df_sol_transfer,cur_df_sol_transfer])
    df_sol_transfer = df_sol_transfer.astype({

        "timestamp":int,
        "amount":float,
        "is_exend":int,
        "is_relationend":int,
        
    })
 
 
    df_sol_transfer.to_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  f"fir_sol_transfer_{find_depth}.xlsx" ,index=False)
    mask_end = ( (df_sol_transfer["is_exend"]==1 ) | (df_sol_transfer["is_relationend"]==1))  
    end_df = df_sol_transfer[ mask_end]
    end_df.to_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  f"endaddress_sol_transfer_{find_depth}.xlsx" ,index=False)
 
    df_sol_transfer = df_sol_transfer[ ~mask_end].reset_index(drop=True)

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

    df_sol_transfer["dalao_count"] = 0
    

    df_sol_transfer =   df_sol_transfer.groupby(by=["dalaoaddress"] ,group_keys=False  ).apply(lambda gdf:  get_dalao_len(gdf))
    df_sol_transfer = df_sol_transfer.sort_values(by=["dalao_count"],ascending=False)
    df_dalao_count = df_sol_transfer[[  "dalaoaddress", "dalao_count"  ]].drop_duplicates()

    # str_tokenaddress_dalao_source

 
    df_dalao_count["scan_activites"] = df_dalao_count.apply(lambda ser : baseclass.makeurl(f"https://solscan.io/account/{ser['dalaoaddress']}#defiactivities" , f"{ser['dalaoaddress'][0:6]}") , axis=1)
 
    df_dalao_count["scan_token_source"] = df_dalao_count.apply(lambda ser : baseclass.makeurl(f"https://solscan.io/account/{ser['dalaoaddress']}?token_address={str_tokenaddress_dalao_source}#transfers" , f"{ser['dalaoaddress'][0:6]}") , axis=1)
 

#  https://gmgn.ai/sol/token/4GULMPKBJLruChBZWksZzukAg1AjSCmCTMn9ny2Xpump?tab=activity&maker=58YHxodnzZKxVBE5EsATs38ywEFV7M8ijjBoBkYAJY8J

    df_dalao_count=df_dalao_count.sort_values(by=["dalao_count"],ascending=False)
    
    df_dalao_count.to_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  f"counts_sol_transfer_{find_depth}.xlsx" ,index=False)
    df_sol_transfer.to_excel(baseclass.ana_dalao_relation_by_solscan_path /str_tokenaddress_dalao_source/find_depth/  f"sec_sol_transfer_{find_depth}.xlsx" ,index=False)
     


    return

main()