#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""市場情報 MCP review 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_candidate_queue_writer_review_decision_approval import (
    build_mcp_fetch_candidate_queue_writer_review_decision_approval_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight import (
    build_mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight_preview,
)
from services.market_intel.mcp_fetch_target_source_governance_review import (
    build_mcp_fetch_target_source_governance_review_preview,
)
from services.market_intel.mcp_professional_source_governance import (
    build_mcp_professional_source_governance_preview,
)


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_review_decision_approval",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_review_decision_approval():
    writer_review_decision_package = {}
    writer_review_decision_result = None
    operator_review_approval = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_review_decision_approval_package")
            or payload.get("candidate_queue_writer_review_decision_approval")
            or payload.get("review_decision_approval")
            or payload.get("operator_review_approval")
            or payload.get("operator_review_decision_approval")
            or payload
        )
        writer_review_decision_package = (
            package.get("writer_review_decision_package")
            or package.get("candidate_queue_writer_review_decision")
            or package.get("writer_review_decision")
            or package.get("review_decision_package")
            or package.get("review_decision")
            or {}
        )
        writer_review_decision_result = (
            package.get("writer_review_decision_result")
            or package.get("mcp_fetch_candidate_queue_writer_review_decision")
        )
        operator_review_approval = (
            package.get("operator_review_approval")
            or package.get("operator_review_decision_approval")
            or package.get("candidate_queue_review_decision_approval")
            or package.get("writer_review_decision_approval")
            or package.get("review_decision_approval_payload")
            or package.get("approval_payload")
            or package.get("approval")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_review_decision_approval_preview(
            writer_review_decision_package=writer_review_decision_package,
            writer_review_decision_result=writer_review_decision_result,
            operator_review_decision_approval=operator_review_approval,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight():
    writer_review_decision_approval_package = {}
    writer_review_decision_approval_result = None
    operator_writer_preflight = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("writer_review_decision_approval_writer_preflight_package")
            or payload.get(
                "candidate_queue_writer_review_decision_approval_writer_preflight"
            )
            or payload.get("review_decision_approval_writer_preflight")
            or payload.get("operator_writer_preflight")
            or payload.get("operator_review_decision_approval_writer_preflight")
            or payload
        )
        writer_review_decision_approval_package = (
            package.get("writer_review_decision_approval_package")
            or package.get("candidate_queue_writer_review_decision_approval")
            or package.get("writer_review_decision_approval")
            or package.get("review_decision_approval_package")
            or package.get("review_decision_approval")
            or {}
        )
        writer_review_decision_approval_result = (
            package.get("writer_review_decision_approval_result")
            or package.get(
                "mcp_fetch_candidate_queue_writer_review_decision_approval"
            )
        )
        operator_writer_preflight = (
            package.get("operator_writer_preflight")
            or package.get("operator_review_decision_approval_writer_preflight")
            or package.get(
                "candidate_queue_review_decision_approval_writer_preflight"
            )
            or package.get("writer_review_decision_approval_writer_preflight")
            or package.get("writer_preflight_payload")
            or package.get("preflight_payload")
            or package.get("preflight")
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight_preview(
            writer_review_decision_approval_package=(
                writer_review_decision_approval_package
            ),
            writer_review_decision_approval_result=(
                writer_review_decision_approval_result
            ),
            operator_review_decision_approval_writer_preflight=(
                operator_writer_preflight
            ),
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_professional_source_governance",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_professional_source_governance():
    operator_source_governance = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("professional_source_governance_package")
            or payload.get("source_governance_package")
            or payload.get("operator_source_governance")
            or payload.get("market_source_governance")
            or payload
        )
        operator_source_governance = (
            package.get("operator_source_governance")
            or package.get("source_governance")
            or package
        )

    service = MarketIntelService()
    return jsonify(
        build_mcp_professional_source_governance_preview(
            operator_source_governance=operator_source_governance,
            phase=service.phase,
        )
    )


@market_intel_bp.route(
    "/api/market_intel/mcp_fetch_target_source_governance_review",
    methods=["GET", "POST"],
)
@login_required
def market_intel_mcp_fetch_target_source_governance_review():
    professional_source_governance_package = None
    target_review_package = None
    operator_confirmations = None
    if request.method == "POST":
        payload = request.get_json(silent=True) or {}
        package = (
            payload.get("fetch_target_source_governance_review_package")
            or payload.get("source_governed_target_review_package")
            or payload
        )
        professional_source_governance_package = (
            package.get("professional_source_governance_package")
            or package.get("source_governance_package")
            or package.get("operator_source_governance")
        )
        target_review_package = (
            package.get("target_review_package")
            or package.get("mcp_fetch_target_review")
            or package.get("target_review")
        )
        operator_confirmations = package.get("operator_confirmations", {})

    service = MarketIntelService()
    return jsonify(
        build_mcp_fetch_target_source_governance_review_preview(
            professional_source_governance_package=professional_source_governance_package,
            target_review_package=target_review_package,
            operator_confirmations=operator_confirmations,
            phase=service.phase,
        )
    )
