#!/usr/bin/env python3
import json
import subprocess
import sys
from openai import OpenAI
from bs4 import BeautifulSoup

# --- ASETUKSET ---
SOURCE_DOMAIN = "www.katiska.eu"
SOURCE_URL = "https://www.katiska.eu"
TARGET_URL = "https://www.poochierevival.info"
TARGET_PATH = "/var/www/backend/poochierevival.info/public_html"  # Muuta tarvittaessa oikeaan WordPress-hakemistoon
OPENAI_API_KEY = "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"

client = OpenAI(api_key=OPENAI_API_KEY)

# --- HTML-sisällön esikäsittely ---
def strip_internal_links(html, domain=SOURCE_DOMAIN):
    soup = BeautifulSoup(html, "html.parser")
    for a in soup.find_all("a"):
        href = a.get("href", "")
        if domain in href:
            a.unwrap()
    return str(soup)

# --- Otsikon käännös erillisellä promptilla ---
def translate_title(title):
    prompt = f"Translate the following blog post title from Finnish to fluent English:\n\n{title}"
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )
    return response.choices[0].message.content.strip()

# --- Artikkelin käännös HTML-rakenteen säilyttäen ---
def translate_html_with_gpt(html):
    prompt = f"""
Translate the following Finnish blog article into fluent English. Keep the HTML structure intact.
Do not add any introductions or commentary. Only return the translated content.
Remove all internal links but preserve the visible text.

Finnish content:
{html}
"""
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.5
    )
    return response.choices[0].message.content.strip()

# --- Julkaisu poochierevival.info-sivustolle WP-CLI:llä ---
def create_post(title, content):
    cmd = [
        "wp", "--allow-root",
        f"--path={TARGET_PATH}",
        f"--url={TARGET_URL}",
        "post", "create",
        "--post_type=post",
        "--post_status=draft",
        "--user=Jagster",
        f"--post_title={title}",
        f"--post_content={content}"
    ]
    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    if result.returncode == 0:
        print(result.stdout.strip())
        return True
    else:
        print("Virhe julkaistessa:", result.stderr.strip(), file=sys.stderr)
        return False

# --- Lisää 'translated' tagi alkuperäiseen artikkeliin ---
def tag_original_post(post_id):
    cmds = [
        ["wp", "--allow-root", "--url=" + SOURCE_URL, "post", "term", "add", str(post_id), "post_tag", "translated"],
        ["wp", "--allow-root", "--url=" + SOURCE_URL, "post", "term", "remove", str(post_id), "post_tag", "translate"]
    ]
    for cmd in cmds:
        subprocess.run(cmd)
   #cmd = [
   #     "wp", "--allow-root",
   #     "--url=" + SOURCE_URL,
   #     "post", "term", "add", str(post_id), "post_tag", "translated"
   # ]
   # cmd = [
   #     "wp", "--allow-root",
   #     "--url=" + SOURCE_URL,
   #     "post", "term", "remove", str(post_id), "post_tag", "translate"
   # ]

    #subprocess.run(cmd)

# --- Pääprosessi ---
def main():
    #raw = sys.stdin.read()
    import fileinput
    raw = "".join(fileinput.input())
    if not raw.strip():
        print("Ei syötettä.")
        sys.exit(1)

    post = json.loads(raw)
    post_id = post["ID"]
    title = post["post_title"]
    html = post["post_content"]

    cleaned_html = strip_internal_links(html)
    translated_html = translate_html_with_gpt(cleaned_html)
    translated_title = translate_title(title)

    success = create_post(translated_title, translated_html)
    if success:
        tag_original_post(post_id)
    else:
        sys.exit(2)

if __name__ == "__main__":
    main()
