#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""市場情報 MCP run package route extension。"""

from flask import jsonify, request

from auth import login_required
from routes.market_intel_routes import market_intel_bp
from services.market_intel import MarketIntelService
from services.market_intel.mcp_fetch_run_package import (
    build_mcp_fetch_run_package_preview,
)
from services.market_intel.mcp_fetch_run_readiness import (
    build_mcp_fetch_run_readiness_preview,
)
from services.market_intel.mcp_fetch_run_receipt import (
    build_mcp_fetch_run_receipt_preview,
)
from services.market_intel.mcp_fetch_result_parser_review import (
    build_mcp_fetch_result_parser_review_preview,
)
from services.market_intel.mcp_fetch_candidate_handoff_review import (
    build_mcp_fetch_candidate_handoff_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_review import (
    build_mcp_fetch_candidate_queue_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_preflight import (
    build_mcp_fetch_candidate_queue_writer_preflight_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_cli_review import (
    build_mcp_fetch_candidate_queue_writer_cli_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_run_package_review import (
    build_mcp_fetch_candidate_queue_writer_run_package_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_run_readiness import (
    build_mcp_fetch_candidate_queue_writer_run_readiness_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_run_receipt_review import (
    build_mcp_fetch_candidate_queue_writer_run_receipt_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_run_closeout_review import (
    build_mcp_fetch_candidate_queue_writer_run_closeout_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_post_closeout_inventory_review import (
    build_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_review_handoff import (
    build_mcp_fetch_candidate_queue_writer_review_handoff_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_review_inventory import (
    build_mcp_fetch_candidate_queue_writer_review_inventory_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_review_decision import (
    build_mcp_fetch_candidate_queue_writer_review_decision_preview,
)


@market_intel_bp.route("/api/market_intel/mcp_fetch_run_package", methods=["GET", "POST"])
@login_required
def market_intel_mcp_fetch_run_package():
    target_review_package = {}
    target_review_result = None
    operator_run_controls = {}
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = payload.get("run_package") or payload.get("fetch_run_package") or payload
        target_review_package = package.get("target_review_package", {})
        target_review_result = package.get("target_review_result")
        operator_run_controls = package.get("operator_run_controls", {})

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_run_package_preview(
            target_review_package=target_review_package,
            target_review_result=target_review_result,
            operator_run_controls=operator_run_controls,
            phase=service.phase,
        )
    )


@market_intel_bp.route("/api/market_intel/mcp_fetch_run_readiness", methods=["GET", "POST"])
@login_required
def market_intel_mcp_fetch_run_readiness():
    run_package = {}
    run_package_result = None
    operator_readiness = {}
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = payload.get("run_readiness") or payload.get("readiness_package") or payload
        run_package = (
            package.get("run_package")
            or package.get("fetch_run_package")
            or package.get("run_package_payload")
            or {}
        )
        run_package_result = (
            package.get("run_package_result")
            or package.get("mcp_fetch_run_package")
        )
        operator_readiness = (
            package.get("operator_readiness")
            or package.get("operator_run_readiness")
            or {}
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_run_readiness_preview(
            run_package=run_package,
            run_package_result=run_package_result,
            operator_readiness=operator_readiness,
            phase=service.phase,
        )
    )


@market_intel_bp.route("/api/market_intel/mcp_fetch_run_receipt", methods=["GET", "POST"])
@login_required
def market_intel_mcp_fetch_run_receipt():
    run_readiness_package = {}
    run_readiness_result = None
    manual_fetch_receipt = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = payload.get("run_receipt") or payload.get("receipt_review") or payload
        run_readiness_package = (
            package.get("run_readiness_package")
            or package.get("run_readiness")
            or package.get("readiness_package")
            or {}
        )
        run_readiness_result = (
            package.get("run_readiness_result")
            or package.get("mcp_fetch_run_readiness")
        )
        manual_fetch_receipt = (
            package.get("manual_fetch_receipt")
            or package.get("fetch_receipt")
            or package.get("receipt")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_run_receipt_preview(
            run_readiness_package=run_readiness_package,
            run_readiness_result=run_readiness_result,
            manual_fetch_receipt=manual_fetch_receipt,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_result_parser_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_result_parser_review():
    run_receipt_package = {}
    run_receipt_result = None
    parser_result = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("parser_review")
            or payload.get("result_parser_review")
            or payload
        )
        run_receipt_package = (
            package.get("run_receipt_package")
            or package.get("run_receipt")
            or package.get("receipt_review")
            or {}
        )
        run_receipt_result = (
            package.get("run_receipt_result")
            or package.get("mcp_fetch_run_receipt")
        )
        parser_result = (
            package.get("parser_result")
            or package.get("fetch_result_parser")
            or package.get("parsed_result")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_result_parser_review_preview(
            run_receipt_package=run_receipt_package,
            run_receipt_result=run_receipt_result,
            parser_result=parser_result,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_handoff_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_handoff_review():
    parser_review_package = {}
    parser_review_result = None
    candidate_handoff = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("candidate_handoff_review")
            or payload.get("handoff_review")
            or payload
        )
        parser_review_package = (
            package.get("parser_review_package")
            or package.get("parser_review")
            or package.get("result_parser_review")
            or {}
        )
        parser_review_result = (
            package.get("parser_review_result")
            or package.get("mcp_fetch_result_parser_review")
        )
        candidate_handoff = (
            package.get("candidate_handoff")
            or package.get("handoff")
            or package.get("handoff_payload")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_handoff_review_preview(
            parser_review_package=parser_review_package,
            parser_review_result=parser_review_result,
            candidate_handoff=candidate_handoff,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_review():
    handoff_review_package = {}
    handoff_review_result = None
    candidate_queue_review = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("candidate_queue_review")
            or payload.get("queue_review")
            or payload
        )
        handoff_review_package = (
            package.get("handoff_review_package")
            or package.get("candidate_handoff_review")
            or package.get("handoff_review")
            or {}
        )
        handoff_review_result = (
            package.get("handoff_review_result")
            or package.get("mcp_fetch_candidate_handoff_review")
        )
        candidate_queue_review = (
            package.get("candidate_queue_review")
            or package.get("queue_review")
            or package.get("review_payload")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_review_preview(
            handoff_review_package=handoff_review_package,
            handoff_review_result=handoff_review_result,
            candidate_queue_review=candidate_queue_review,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_preflight",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_preflight():
    queue_review_package = {}
    queue_review_result = None
    writer_preflight = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_preflight_review")
            or payload.get("candidate_queue_writer_preflight")
            or payload.get("writer_preflight")
            or payload
        )
        queue_review_package = (
            package.get("queue_review_package")
            or package.get("candidate_queue_review")
            or package.get("queue_review")
            or {}
        )
        queue_review_result = (
            package.get("queue_review_result")
            or package.get("mcp_fetch_candidate_queue_review")
        )
        writer_preflight = (
            package.get("writer_preflight")
            or package.get("candidate_queue_writer_preflight")
            or package.get("preflight")
            or package.get("preflight_payload")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_preflight_preview(
            queue_review_package=queue_review_package,
            queue_review_result=queue_review_result,
            writer_preflight=writer_preflight,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_cli_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_cli_review():
    writer_preflight_package = {}
    writer_preflight_result = None
    writer_cli_review = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_cli_review_package")
            or payload.get("candidate_queue_writer_cli_review")
            or payload.get("writer_cli_review")
            or payload
        )
        writer_preflight_package = (
            package.get("writer_preflight_package")
            or package.get("candidate_queue_writer_preflight")
            or package.get("writer_preflight")
            or package.get("preflight_package")
            or {}
        )
        writer_preflight_result = (
            package.get("writer_preflight_result")
            or package.get("mcp_fetch_candidate_queue_writer_preflight")
        )
        writer_cli_review = (
            package.get("writer_cli_review")
            or package.get("candidate_queue_writer_cli_review")
            or package.get("cli_review")
            or package.get("review_payload")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_cli_review_preview(
            writer_preflight_package=writer_preflight_package,
            writer_preflight_result=writer_preflight_result,
            writer_cli_review=writer_cli_review,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_run_package_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_run_package_review():
    writer_cli_review_package = {}
    writer_cli_review_result = None
    writer_run_package_review = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_run_package_review_package")
            or payload.get("candidate_queue_writer_run_package_review")
            or payload.get("writer_run_package_review")
            or payload.get("run_package_review")
            or payload
        )
        writer_cli_review_package = (
            package.get("writer_cli_review_package")
            or package.get("candidate_queue_writer_cli_review")
            or package.get("writer_cli_review")
            or package.get("cli_review_package")
            or {}
        )
        writer_cli_review_result = (
            package.get("writer_cli_review_result")
            or package.get("mcp_fetch_candidate_queue_writer_cli_review")
        )
        writer_run_package_review = (
            package.get("writer_run_package_review")
            or package.get("candidate_queue_writer_run_package_review")
            or package.get("run_package_review")
            or package.get("package_review")
            or package.get("review_payload")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_run_package_review_preview(
            writer_cli_review_package=writer_cli_review_package,
            writer_cli_review_result=writer_cli_review_result,
            writer_run_package_review=writer_run_package_review,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_run_readiness",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_run_readiness():
    writer_run_package_review_package = {}
    writer_run_package_review_result = None
    operator_readiness = {}
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_run_readiness_package")
            or payload.get("candidate_queue_writer_run_readiness")
            or payload.get("writer_run_readiness")
            or payload.get("run_readiness")
            or payload
        )
        writer_run_package_review_package = (
            package.get("writer_run_package_review_package")
            or package.get("candidate_queue_writer_run_package_review")
            or package.get("writer_run_package_review")
            or package.get("run_package_review_package")
            or {}
        )
        writer_run_package_review_result = (
            package.get("writer_run_package_review_result")
            or package.get("mcp_fetch_candidate_queue_writer_run_package_review")
        )
        operator_readiness = (
            package.get("operator_readiness")
            or package.get("writer_run_readiness")
            or package.get("candidate_queue_writer_run_readiness")
            or package.get("readiness_payload")
            or {}
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_run_readiness_preview(
            writer_run_package_review_package=writer_run_package_review_package,
            writer_run_package_review_result=writer_run_package_review_result,
            operator_readiness=operator_readiness,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_run_receipt_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_run_receipt_review():
    writer_run_readiness_package = {}
    writer_run_readiness_result = None
    writer_run_receipt = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_run_receipt_review_package")
            or payload.get("candidate_queue_writer_run_receipt_review")
            or payload.get("writer_run_receipt")
            or payload.get("run_receipt_review")
            or payload
        )
        writer_run_readiness_package = (
            package.get("writer_run_readiness_package")
            or package.get("candidate_queue_writer_run_readiness")
            or package.get("writer_run_readiness")
            or package.get("run_readiness_package")
            or {}
        )
        writer_run_readiness_result = (
            package.get("writer_run_readiness_result")
            or package.get("mcp_fetch_candidate_queue_writer_run_readiness")
        )
        writer_run_receipt = (
            package.get("writer_run_receipt")
            or package.get("candidate_queue_writer_run_receipt")
            or package.get("run_receipt")
            or package.get("receipt_payload")
            or package.get("receipt")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_run_receipt_review_preview(
            writer_run_readiness_package=writer_run_readiness_package,
            writer_run_readiness_result=writer_run_readiness_result,
            writer_run_receipt=writer_run_receipt,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_run_closeout_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_run_closeout_review():
    writer_run_receipt_review_package = {}
    writer_run_receipt_review_result = None
    operator_closeout = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_run_closeout_review_package")
            or payload.get("candidate_queue_writer_run_closeout_review")
            or payload.get("writer_run_closeout")
            or payload.get("run_closeout_review")
            or payload
        )
        writer_run_receipt_review_package = (
            package.get("writer_run_receipt_review_package")
            or package.get("candidate_queue_writer_run_receipt_review")
            or package.get("writer_run_receipt_review")
            or package.get("run_receipt_review_package")
            or {}
        )
        writer_run_receipt_review_result = (
            package.get("writer_run_receipt_review_result")
            or package.get("mcp_fetch_candidate_queue_writer_run_receipt_review")
        )
        operator_closeout = (
            package.get("operator_closeout")
            or package.get("writer_run_closeout")
            or package.get("candidate_queue_writer_run_closeout")
            or package.get("closeout_payload")
            or package.get("closeout")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_run_closeout_review_preview(
            writer_run_receipt_review_package=writer_run_receipt_review_package,
            writer_run_receipt_review_result=writer_run_receipt_review_result,
            operator_closeout=operator_closeout,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_post_closeout_inventory_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review():
    writer_run_closeout_review_package = {}
    writer_run_closeout_review_result = None
    operator_inventory = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_post_closeout_inventory_review_package")
            or payload.get("candidate_queue_writer_post_closeout_inventory_review")
            or payload.get("post_closeout_inventory_review")
            or payload.get("operator_inventory_review")
            or payload
        )
        writer_run_closeout_review_package = (
            package.get("writer_run_closeout_review_package")
            or package.get("candidate_queue_writer_run_closeout_review")
            or package.get("writer_run_closeout_review")
            or package.get("run_closeout_review_package")
            or {}
        )
        writer_run_closeout_review_result = (
            package.get("writer_run_closeout_review_result")
            or package.get("mcp_fetch_candidate_queue_writer_run_closeout_review")
        )
        operator_inventory = (
            package.get("operator_inventory")
            or package.get("post_closeout_inventory")
            or package.get("writer_post_closeout_inventory")
            or package.get("inventory_payload")
            or package.get("inventory")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review_preview(
            writer_run_closeout_review_package=writer_run_closeout_review_package,
            writer_run_closeout_review_result=writer_run_closeout_review_result,
            operator_inventory=operator_inventory,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_review_handoff",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_review_handoff():
    writer_post_closeout_inventory_review_package = {}
    writer_post_closeout_inventory_review_result = None
    operator_handoff = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_review_handoff_package")
            or payload.get("candidate_queue_writer_review_handoff")
            or payload.get("review_handoff")
            or payload.get("operator_handoff_review")
            or payload.get("operator_handoff")
            or payload
        )
        writer_post_closeout_inventory_review_package = (
            package.get("writer_post_closeout_inventory_review_package")
            or package.get("candidate_queue_writer_post_closeout_inventory_review")
            or package.get("writer_post_closeout_inventory_review")
            or package.get("post_closeout_inventory_review_package")
            or package.get("post_closeout_inventory_review")
            or package.get("operator_inventory_review")
            or {}
        )
        writer_post_closeout_inventory_review_result = (
            package.get("writer_post_closeout_inventory_review_result")
            or package.get(
                "mcp_fetch_candidate_queue_writer_post_closeout_inventory_review"
            )
        )
        operator_handoff = (
            package.get("operator_handoff")
            or package.get("candidate_queue_review_handoff")
            or package.get("writer_review_handoff")
            or package.get("handoff_payload")
            or package.get("handoff")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_review_handoff_preview(
            writer_post_closeout_inventory_review_package=(
                writer_post_closeout_inventory_review_package
            ),
            writer_post_closeout_inventory_review_result=(
                writer_post_closeout_inventory_review_result
            ),
            operator_handoff=operator_handoff,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_review_inventory",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_review_inventory():
    writer_review_handoff_package = {}
    writer_review_handoff_result = None
    operator_review_inventory = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_review_inventory_package")
            or payload.get("candidate_queue_writer_review_inventory")
            or payload.get("review_inventory")
            or payload.get("operator_review_inventory")
            or payload
        )
        writer_review_handoff_package = (
            package.get("writer_review_handoff_package")
            or package.get("candidate_queue_writer_review_handoff")
            or package.get("writer_review_handoff")
            or package.get("review_handoff_package")
            or package.get("review_handoff")
            or {}
        )
        writer_review_handoff_result = (
            package.get("writer_review_handoff_result")
            or package.get("mcp_fetch_candidate_queue_writer_review_handoff")
        )
        operator_review_inventory = (
            package.get("operator_review_inventory")
            or package.get("candidate_queue_review_inventory")
            or package.get("writer_review_inventory")
            or package.get("review_inventory_payload")
            or package.get("inventory_payload")
            or package.get("inventory")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_review_inventory_preview(
            writer_review_handoff_package=writer_review_handoff_package,
            writer_review_handoff_result=writer_review_handoff_result,
            operator_review_inventory=operator_review_inventory,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_review_decision",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_review_decision():
    writer_review_inventory_package = {}
    writer_review_inventory_result = None
    operator_review_decision = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_review_decision_package")
            or payload.get("candidate_queue_writer_review_decision")
            or payload.get("review_decision")
            or payload.get("operator_review_decision")
            or payload
        )
        writer_review_inventory_package = (
            package.get("writer_review_inventory_package")
            or package.get("candidate_queue_writer_review_inventory")
            or package.get("writer_review_inventory")
            or package.get("review_inventory_package")
            or package.get("review_inventory")
            or {}
        )
        writer_review_inventory_result = (
            package.get("writer_review_inventory_result")
            or package.get("mcp_fetch_candidate_queue_writer_review_inventory")
        )
        operator_review_decision = (
            package.get("operator_review_decision")
            or package.get("candidate_queue_review_decision")
            or package.get("writer_review_decision")
            or package.get("review_decision_payload")
            or package.get("decision_payload")
            or package.get("decision")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_review_decision_preview(
            writer_review_inventory_package=writer_review_inventory_package,
            writer_review_inventory_result=writer_review_inventory_result,
            operator_review_decision=operator_review_decision,
            phase=service.phase,
        )
    )
