
# 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)
 
find_depth = "depth_01"
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]
find_depth = "depth_01"
 
arr_str_dalaoaddress = df[f'dalao_{find_depth}'].drop_duplicates().dropna().tolist()

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


def get_reladd_count(gdf):
    gdf["reladd_count"] = len(gdf)
    return gdf


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 )
        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,
        "sol_dalao_count":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)

    df_sol_transfer["reladd_count"]  = 0
    

    df_sol_transfer = df_sol_transfer.groupby(by = [ "rel_address" ],  group_keys=False).apply(lambda gdf : get_reladd_count(gdf))

    df_sol_transfer = df_sol_transfer.sort_values(by=[ "sol_dalao_count"  , "dalaoaddress" ,"timestamp"],ascending=False)

    df_sol_dalao_count = df_sol_transfer[[  "dalaoaddress", "sol_dalao_count"  ]].drop_duplicates()

    # str_tokenaddress_dalao_source

    df_sol_dalao_count=df_sol_dalao_count.sort_values(by=["sol_dalao_count"],ascending=False)
    
    df_sol_dalao_count["scan_activites"] = df_sol_dalao_count.apply(lambda ser : baseclass.makeurl(f"https://solscan.io/account/{ser['dalaoaddress']}#defiactivities" , f"{ser['dalaoaddress'][0:6]}") , axis=1)
 
    df_sol_dalao_count["scan_token_source"] = df_sol_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_sol_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()