import sys
import os

sys.path.append(os.path.dirname(sys.path[0]))
import pandas as pd
from urllib.parse import quote
import datetime
from utils.secure_db_client import get_remote_engine
from fastapi import FastAPI
import uvicorn

app = FastAPI()

engine = get_remote_engine(site_name='us', db_type="doris_adv", database="advertising_manager")


def build_url(search_term: str, page: int) -> str:
    site_url = "https://www.amazon.com/"
    url_template = f"{site_url}s?k={{search_term}}&page={{page_number}}"
    q = quote(str(search_term), "utf-8")
    q = q.replace("'", "%27").replace("/", "%2F")
    q = (q.replace(" ", "+")
           .replace("&", "%26")
           .replace("#", "%23")
           .replace("(", "%28")
           .replace(")", "%29"))
    return url_template.format(search_term=q, page_number=page)


@app.get("/generate_sp_search_term")
def generate_sp_search_term():
    """读取 sp_keyword_position_keyword 的搜索词，构建5页url，写入 us_sp_search_term_syn"""
    try:
        # 读取关键词
        df = engine.read_sql("SELECT DISTINCT keyword_text FROM sp_keyword_position_keyword")
        df.columns = ['search_term']
        keyword_count = len(df)
        if keyword_count == 0:
            return {"code": 400, "msg": "sp_keyword_position_keyword 表没有数据"}

        # 每条记录复制5次（5页）
        df5 = df.loc[df.index.repeat(5)].copy()
        s = datetime.datetime.now().strftime("%Y-%m-%d-%H")
        df5["page"] = df5.groupby(level=0).cumcount() + 1
        df5["time_batch"] = s
        df5["url"] = df5.apply(lambda r: build_url(r["search_term"], int(r["page"])), axis=1)

        need_cols = ["search_term", "url", "time_batch"]
        df5 = df5[need_cols]
        df5.drop_duplicates(['search_term', 'url'], inplace=True)
        total = len(df5)

        # 清空表
        with engine.begin() as conn:
            conn.execute("TRUNCATE TABLE us_sp_search_term_syn")

        # 写入
        engine.to_sql(df5, 'us_sp_search_term_syn', if_exists='append')

        return {"code": 200, "msg": "成功", "keyword_count": keyword_count, "url_count": total}
    except Exception as e:
        return {"code": 500, "msg": str(e)}


@app.get("/health")
def health():
    return {"status": "ok"}


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=8099)
