# /opt/docker/dev/service_finder/.roo/scripts/gitea_manager.py
#!/usr/bin/env python3
import requests
import sys
import datetime
import socket

# ================= KONFIGURÁCIÓ =================
INTERNAL_HOST = "gitea"
EXTERNAL_IP = "192.168.100.10"
PORT = "3000"
OWNER = "kincses"
REPO = "service-finder"
TOKEN = "783f58519ee0ca060491dbc07f3dde1d8e48c5dd"

HEADERS = {
    "Authorization": f"token {TOKEN}",
    "Content-Type": "application/json"
}

# Hibrid Hálózat-felismerés
def get_base_url():
    try:
        socket.gethostbyname(INTERNAL_HOST)
        return f"http://{INTERNAL_HOST}:{PORT}/api/v1"
    except socket.gaierror:
        return f"http://{EXTERNAL_IP}:{PORT}/api/v1"

BASE_URL = get_base_url()

LABELS = {
    "Status: To Do": "#ef4444", "Status: In Progress": "#f59e0b", "Status: Done": "#10b981", "Status: Blocked": "#000000",
    "Scope: Backend": "#0369a1", "Scope: Frontend": "#0284c7", "Scope: API": "#0ea5e9", "Scope: Core": "#38bdf8", "Scope: Robot": "#7dd3fc", "Scope: Database": "#ec4899",
    "Type: Script": "#8b5cf6", "Type: Model": "#3b82f6", "Type: Database": "#ec4899", "Type: Bug": "#dc2626", "Type: Feature": "#16a34a", "Type: Refactor": "#16a34a",
    "Role: Admin": "#fb923c", "Role: User": "#fdba74"
}
# ================================================

def fetch_all_pages(endpoint):
    """Gitea API lapozás (Pagination) kezelése, hogy minden elemet visszakapjunk."""
    all_data = []
    page = 1
    limit = 50
    separator = "&" if "?" in endpoint else "?"
    while True:
        url = f"{BASE_URL}{endpoint}{separator}limit={limit}&page={page}"
        res = requests.get(url, headers=HEADERS)
        if res.status_code != 200:
            break
        data = res.json()
        if not data:
            break
        all_data.extend(data)
        if len(data) < limit:
            break
        page += 1
    return all_data

def init_labels():
    existing_labels = fetch_all_pages(f"/repos/{OWNER}/{REPO}/labels")
    existing = {l['name']: l['id'] for l in existing_labels}
    
    label_ids = {}
    for name, color in LABELS.items():
        if name in existing:
            label_ids[name] = existing[name]
        else:
            post_res = requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/labels", headers=HEADERS, json={"name": name, "color": color})
            if post_res.status_code == 201: label_ids[name] = post_res.json()['id']
    return label_ids

def set_issue_state(issue_num, new_state_label, category_labels=[]):
    label_ids = init_labels()
    res = requests.get(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}/labels", headers=HEADERS)
    current_ids = [l['id'] for l in res.json()] if res.status_code == 200 else []
    
    for status in ["Status: To Do", "Status: In Progress", "Status: Done", "Status: Blocked"]:
        if status in label_ids and label_ids[status] in current_ids:
            current_ids.remove(label_ids[status])
            
    if new_state_label in label_ids and label_ids[new_state_label] not in current_ids:
        current_ids.append(label_ids[new_state_label])
        
    for cat in category_labels:
        if cat in label_ids and label_ids[cat] not in current_ids:
            current_ids.append(label_ids[cat])

    requests.put(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}/labels", headers=HEADERS, json={"labels": current_ids})

def add_comment(issue_num, message):
    requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}/comments", headers=HEADERS, json={"body": message})

# --- MÉRFÖLDKŐ (MILESTONE) KEZELÉS ---

def resolve_milestone_id(name_or_id):
    if not name_or_id: return None
    if str(name_or_id).isdigit(): return int(name_or_id)
    
    milestones = fetch_all_pages(f"/repos/{OWNER}/{REPO}/milestones")
    for ms in milestones:
        if ms['title'].lower() == str(name_or_id).lower():
            return ms['id']
    return None

def create_milestone(title, description="", due_date=None):
    existing_id = resolve_milestone_id(title)
    if existing_id:
        print(f"Mérföldkő már létezik: '{title}' (ID: {existing_id})")
        return existing_id
        
    payload = {"title": title, "description": description}
    if due_date:
        payload["due_on"] = f"{due_date}T23:59:59Z" if len(due_date) == 10 else due_date

    res = requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/milestones", headers=HEADERS, json=payload)
    if res.status_code == 201:
        ms_id = res.json()['id']
        print(f"✅ Mérföldkő sikeresen létrehozva: '{title}' (ID: {ms_id})")
        return ms_id
    print(f"❌ Hiba a mérföldkő létrehozásakor: {res.text}")
    return None

def list_milestones():
    milestones = fetch_all_pages(f"/repos/{OWNER}/{REPO}/milestones")
    print(f"\n{'ID':<5} | {'Mérföldkő Címe':<40} | {'Haladás'}")
    print("-" * 65)
    for ms in milestones:
        open_issues = ms.get('open_issues', 0)
        closed_issues = ms.get('closed_issues', 0)
        total = open_issues + closed_issues
        if total > 0:
            completeness = int((closed_issues / total) * 100)
        else:
            completeness = 0
        print(f"#{ms['id']:<4} | {ms['title'][:40]:<40} | {completeness}%")

# --- PROJEKT (BOARD) KEZELÉS ---

def create_repo_project(title, board_type="kanban", description=""):
    """Create a new project board in the repository."""
    payload = {
        "title": title,
        "board_type": board_type,  # "kanban" or "basic"
        "description": description
    }
    res = requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/projects", headers=HEADERS, json=payload)
    if res.status_code == 201:
        project_id = res.json()['id']
        print(f"✅ Projekt sikeresen létrehozva: '{title}' (ID: {project_id})")
        return project_id
    else:
        print(f"❌ Hiba a projekt létrehozásakor: {res.status_code} - {res.text}")
        return None

def list_repo_projects():
    """List all projects in the repository."""
    projects = fetch_all_pages(f"/repos/{OWNER}/{REPO}/projects")
    print(f"\n{'ID':<5} | {'Projekt Címe':<40} | {'Típus'}")
    print("-" * 65)
    for proj in projects:
        print(f"#{proj['id']:<4} | {proj['title'][:40]:<40} | {proj.get('board_type', 'unknown')}")

def create_project_board(project_id, title):
    """Create a column (board) within a project."""
    res = requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/projects/{project_id}/columns", headers=HEADERS, json={"title": title})
    if res.status_code == 201:
        column_id = res.json()['id']
        print(f"✅ Projekt oszlop sikeresen létrehozva: '{title}' (ID: {column_id})")
        return column_id
    else:
        print(f"❌ Hiba az oszlop létrehozásakor: {res.status_code} - {res.text}")
        return None

# --- KÁRTYA (ISSUE) KEZELÉS ---

def create_issue(title, body, categories, milestone_ref=None, due_date=None, assignees=None):
    ms_id = resolve_milestone_id(milestone_ref)
    
    payload = {"title": title, "body": body}
    if ms_id: 
        payload["milestone"] = ms_id
    if due_date:
        payload["due_date"] = f"{due_date}T23:59:59Z" if len(due_date) == 10 else due_date
    if assignees:
        payload["assignees"] = assignees
        
    res = requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues", headers=HEADERS, json=payload)
    if res.status_code == 201:
        issue_num = res.json()['number']
        set_issue_state(issue_num, "Status: To Do", categories)
        ms_text = f" (Milestone: {ms_id})" if ms_id else ""
        print(f"✅ Siker: #{issue_num} feladat létrehozva{ms_text}.")
        return True
    print(f"❌ Hiba a kártya létrehozásakor: {res.text}")
    return False

def start_issue(issue_num):
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    set_issue_state(issue_num, "Status: In Progress")
    requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}/stopwatch/start", headers=HEADERS)
    add_comment(issue_num, f"▶️ **Munka megkezdve:** {now}")
    print(f"✅ Siker: A #{issue_num} időmérése elindult.")

def finish_issue(issue_num, custom_message=None):
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    set_issue_state(issue_num, "Status: Done")
    requests.post(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}/stopwatch/stop", headers=HEADERS)
    requests.patch(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}", headers=HEADERS, json={"state": "closed"})
    
    comment_body = f"✅ **Munka befejezve:** {now}\n\n**Technikai Összefoglaló:**\n{custom_message}\n\n⏱️ *A ráfordított időt a Gitea rögzítette.*" if custom_message else f"✅ **Munka befejezve:** {now}\n⏱️ *A ráfordított időt a Gitea rögzítette.*"
    add_comment(issue_num, comment_body)
    print(f"✅ Siker: A #{issue_num} lezárva, időmérés megállítva.")

def get_issue(issue_num):
    res = requests.get(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}", headers=HEADERS)
    if res.status_code != 200:
        print(f"Hiba: Nem sikerült lekérni a #{issue_num} feladatot. Státusz: {res.status_code}")
        sys.exit(1)
    
    data = res.json()
    ms_title = data.get('milestone', {}).get('title', 'Nincs') if data.get('milestone') else 'Nincs'
    print("=" * 60)
    print(f"Feladat #{issue_num} - {data.get('state', 'unknown').upper()} (Mérföldkő: {ms_title})")
    print("=" * 60)
    print(f"Cím: {data.get('title', 'Nincs cím')}")
    print("-" * 60)
    print(data.get('body', 'Nincs leírás'))
    print("=" * 60)

def update_issue(issue_num, title=None, body=None):
    """Update an issue with new title and/or body."""
    payload = {}
    if title is not None:
        payload["title"] = title
    if body is not None:
        payload["body"] = body
    
    if not payload:
        print("Nincs módosítandó mező. Használd --title vagy --body paramétert.")
        return False
    
    res = requests.patch(f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_num}", headers=HEADERS, json=payload)
    if res.status_code in (200, 201):
        print(f"✅ Siker: A #{issue_num} feladat frissítve.")
        return True
    else:
        print(f"❌ Hiba a frissítéskor: {res.status_code} - {res.text}")
        return False

def list_issues(state="open"):
    issues = fetch_all_pages(f"/repos/{OWNER}/{REPO}/issues?state={state}")
    print(f"\n--- {state.upper()} FELADATOK ---")
    print(f"{'ID':<4} | {'Cím':<40} | {'Mérföldkő'}")
    print("-" * 70)
    for i in issues:
        ms = i.get('milestone', {}).get('title', '-') if i.get('milestone') else '-'
        print(f"#{i['number']:<3} | {i['title'][:40]:<40} | {ms}")

# ================= FŐPROGRAM =================

if __name__ == "__main__":
    raw_args = sys.argv[1:]
    if not raw_args:
        print("Használat: python3 gitea_manager.py [parancs] [argumentumok]")
        print("  list              - Nyitott kártyák listázása")
        print("  list closed       - Lezárt kártyák listázása")
        print("  ms list           - Mérföldkövek listázása")
        print("  ms create \"Név\" - Új mérföldkő létrehozása")
        print("  project list      - Projekt táblák listázása")
        print("  project create \"Cím\" [board_type] [description] - Új projekt létrehozása")
        print("  board create <project_id> \"Oszlop neve\" - Új oszlop létrehozása projektben")
        print("  create \"Cím\" \"Leírás\" [Mérföldkő] [Címkék...] [--due YYYY-MM-DD] [--assign username]")
        print("  start <id>        - Munka megkezdése")
        print("  finish <id> [msg] - Munka lezárása")
        print("  get <id>          - Kártya lekérése")
        print("  update <id> [--title \"Új cím\"] [--body \"Új leírás\"] - Kártya frissítése")
        sys.exit(1)

    # Paraméterek kinyerése (--due, --assign, --title, --body)
    args = []
    due_date = None
    assignees = []
    update_title = None
    update_body = None
    
    i = 0
    while i < len(raw_args):
        if raw_args[i] == "--due" and i + 1 < len(raw_args):
            due_date = raw_args[i+1]
            i += 2
        elif raw_args[i] == "--assign" and i + 1 < len(raw_args):
            assignees.append(raw_args[i+1])
            i += 2
        elif raw_args[i] == "--title" and i + 1 < len(raw_args):
            update_title = raw_args[i+1]
            i += 2
        elif raw_args[i] == "--body" and i + 1 < len(raw_args):
            update_body = raw_args[i+1]
            i += 2
        else:
            args.append(raw_args[i])
            i += 1

    action = args[0].lower() if args else ""

    if action == "list":
        list_issues(args[1] if len(args) > 1 else "open")
    
    elif action == "ms":
        if len(args) > 1 and args[1].lower() == "create":
            create_milestone(args[2], args[3] if len(args) > 3 else "", due_date)
        else:
            list_milestones()
    
    elif action == "project":
        if len(args) > 1 and args[1].lower() == "create":
            title = args[2] if len(args) > 2 else "New Project"
            board_type = args[3] if len(args) > 3 else "kanban"
            description = args[4] if len(args) > 4 else ""
            create_repo_project(title, board_type, description)
        else:
            list_repo_projects()
    
    elif action == "board" and len(args) > 2 and args[1].lower() == "create":
        project_id = args[2]
        column_title = args[3] if len(args) > 3 else "New Column"
        create_project_board(project_id, column_title)
            
    elif action == "start" and len(args) > 1:
        start_issue(args[1])
        
    elif action == "finish" and len(args) > 1:
        finish_issue(args[1], args[2] if len(args) > 2 else None)
        
    elif action == "get" and len(args) > 1:
        get_issue(args[1])
        
    elif action == "create" and len(args) > 2:
        title, body = args[1], args[2]
        milestone_ref = None
        categories = []
        
        if len(args) > 3:
            arg3 = args[3]
            if any(arg3.startswith(prefix) for prefix in ["Status:", "Scope:", "Type:", "Role:"]):
                categories = args[3:]
            else:
                milestone_ref = arg3
                categories = args[4:]
                
        create_issue(title, body, categories, milestone_ref, due_date, assignees)
    
    elif action == "update" and len(args) > 1:
        issue_id = args[1]
        update_issue(issue_id, update_title, update_body)