
# 将special add 在不同时间持仓的csv合并为一个excel
import pandas as pd
import sys


arr_file = []
merged_df = None
domains_df= None


def makeurl( url,url_show):
    return '=HYPERLINK("{}","{}")'.format(url,  url_show)

def merge_df_fun():
    arr_date_range =[]
    arr_col_pct=[]
    
    global arr_file , merged_df , domains_df,token_add
    for index, file in enumerate(arr_file):
        date_range = file.replace(".csv","").split("_")
        date_range = date_range[-2]+"_"+date_range[-1]

        cur_df =   pd.read_csv(file ,dtype=object)
        cur_df=cur_df.dropna(subset=['holder_owner'])

        cur_domains_df = cur_df[['holder_owner' , 'domains_owned']]
        domains_df = pd.concat([domains_df, cur_domains_df  ]).dropna().drop_duplicates(subset=['holder_owner'])
         
        cur_df=cur_df[['holder_owner','amount_M']]
        cur_df =cur_df.astype({
            "amount_M":float,
        })

        cur_df.rename(columns={'amount_M': f'AM_{date_range}'}, inplace=True)
        arr_date_range.append(f'{date_range}')
        arr_col_pct.append(f'AM_{date_range}')
        if index==0 :
            merged_df = cur_df
            continue
        merged_df = pd.merge(merged_df, cur_df, on='holder_owner', how='outer')
  
    
    # merged_df=merged_df.fillna(-1)
    merged_df = pd.merge(merged_df, domains_df, on='holder_owner', how='left')

    merged_df["gmgn"] = merged_df.apply(lambda ser: makeurl(
    f"https://gmgn.ai/sol/token/{token_add}?tab=activity&maker={ser['holder_owner']}" , "gmgn" ), axis=1)

    merged_df["defined"] = merged_df.apply(lambda ser: makeurl(
     f"https://www.defined.fi/sol/{token_add}?maker={ser['holder_owner']}" , "defined" ), axis=1)
    
  
    merged_df.to_excel('001_01_merge_dune_hold_info_from_manydunecsv_to_一个多列的xlsx文档.xlsx' ,index=False)

 
 
if __name__ == "__main__":
 
    if len(sys.argv)<3:
        print("args queshao ")
        sys.exit(1)
    token_add = sys.argv[1]
    arr_file =sys.argv[2:]
    arr_file = [file for file in arr_file if   file.startswith( "dune_holders_xx")  and file.endswith(".csv")]
 
    arr_file.sort()
    print("read file: ",arr_file)
    merge_df_fun()
 