from flask import Blueprint, request, jsonify
import uuid
import json
from pathlib import Path
from modules.core.audit import audit_log

from modules.core.security import requires_app_auth
from .service import start_architecture_job
from .store import ARCH_JOBS, ARCH_LOCK

architecture_bp = Blueprint("architecture", __name__)

ARCH_FOLDER = Path("architecture")

@architecture_bp.route("/architecture/start", methods=["POST"])
@requires_app_auth
def architecture_start():
    data = request.get_json(force=True) or {}
    question = (data.get("question") or "").strip()

    if not question:
        return jsonify({"error": "Empty question"}), 400

    job_id = str(uuid.uuid4())
    audit_log("ARCHITECTURE", f"job_id={job_id}")

    with ARCH_LOCK:
        ARCH_JOBS[job_id] = {
            "status": "RUNNING",
            "logs": []
        }

    start_architecture_job(job_id, question)
    return jsonify({"job_id": job_id})


@architecture_bp.route("/architecture/<job_id>/status", methods=["GET"])
@requires_app_auth
def architecture_status(job_id):
    job_dir = ARCH_FOLDER / job_id
    status_file = job_dir / "status.json"

    # fallback 1: status persistido
    if status_file.exists():
        try:
            return jsonify(json.loads(status_file.read_text(encoding="utf-8")))
        except Exception:
            return jsonify({"status": "ERROR", "detail": "Invalid status file"}), 500

    # fallback 2: status em memória
    with ARCH_LOCK:
        job = ARCH_JOBS.get(job_id)

    if job:
        return jsonify({"status": job.get("status", "PROCESSING")})

    return jsonify({"status": "NOT_FOUND"}), 404


@architecture_bp.route("/architecture/<job_id>/logs", methods=["GET"])
@requires_app_auth
def architecture_logs(job_id):
    with ARCH_LOCK:
        job = ARCH_JOBS.get(job_id, {})
    return jsonify({"logs": job.get("logs", [])})

@architecture_bp.route("/architecture/<job_id>/result", methods=["GET"])
@requires_app_auth
def architecture_result(job_id):
    job_dir = ARCH_FOLDER / job_id
    result_file = job_dir / "architecture.json"

    # ainda não terminou
    if not result_file.exists():
        return jsonify({"error": "not ready"}), 404

    try:
        raw = result_file.read_text(encoding="utf-8")
        plan = json.loads(raw)
        return jsonify(plan)

    except Exception as e:
        return jsonify({"error": str(e)}), 500