#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""市場情報 AI summary persistence 後續 POST API。"""

from flask import jsonify, request

from auth import login_required
from routes.market_intel_review_routes import _extract_run_payload, market_intel_review_bp
from routes.market_intel_review_post_routes import (
    _build_review_ai_summary_persistence_writer_preflight_stack,
)
from services.market_intel import MarketIntelService
from services.market_intel.candidate_queue_review_ai_summary_persistence_run_package import (
    build_candidate_queue_review_ai_summary_persistence_run_package,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_run_readiness import (
    build_candidate_queue_review_ai_summary_persistence_run_readiness,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_run_receipt import (
    build_candidate_queue_review_ai_summary_persistence_run_receipt,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_run_closeout import (
    build_candidate_queue_review_ai_summary_persistence_run_closeout,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary,
)


def _build_ai_summary_persistence_run_readiness(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    stack = _build_review_ai_summary_persistence_writer_preflight_stack(
        service=service,
        sample_result=sample_result,
        payload_error=payload_error,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    run_package = build_candidate_queue_review_ai_summary_persistence_run_package(
        ai_summary_persistence_writer_preflight=stack[-1],
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_run_readiness(
        ai_summary_persistence_run_package=run_package,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_run_receipt(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    run_readiness = _build_ai_summary_persistence_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_run_receipt(
        ai_summary_persistence_run_readiness=run_readiness,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        postwrite_smoke_result=smoke_result,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_run_closeout(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    run_receipt = _build_ai_summary_persistence_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_run_closeout(
        run_receipt=run_receipt,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_gate(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    closeout = _build_ai_summary_persistence_run_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate(
        summary_persistence_closeout=closeout,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_run_package(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    gate = _build_ai_summary_persistence_telegram_dispatch_gate(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package(
        telegram_dispatch_gate=gate,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_run_readiness(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    run_package = _build_ai_summary_persistence_telegram_dispatch_run_package(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness(
        telegram_dispatch_run_package=run_package,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_run_receipt(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    run_readiness = _build_ai_summary_persistence_telegram_dispatch_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt(
        telegram_dispatch_run_readiness=run_readiness,
        telegram_dispatch_receipt=writer_output,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_closeout(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    run_receipt = _build_ai_summary_persistence_telegram_dispatch_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout(
        telegram_dispatch_run_receipt=run_receipt,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_archive(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    closeout = _build_ai_summary_persistence_telegram_dispatch_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive(
        telegram_dispatch_closeout=closeout,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_run_readiness",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_run_readiness():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_run_receipt",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_run_receipt():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_run_closeout",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_run_closeout():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_run_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_gate():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_gate(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_package():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_run_package(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_readiness():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_run_receipt():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_closeout():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    archive = _build_ai_summary_persistence_telegram_dispatch_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary(
        telegram_dispatch_archive=archive,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200
