"""雜項 Routes Blueprint。從 app.py 抽離（Phase 3e）。

收納尚未歸類的小型 routes：
- POST /api/test_url：測試外部網址連通性
- GET  /brand_assets：品牌資產庫頁面
"""
import requests
from flask import Blueprint, jsonify, render_template, request

misc_bp = Blueprint('misc', __name__)


@misc_bp.route('/api/test_url', methods=['POST'])
def test_url():
    """API: 測試網址是否有效"""
    try:
        data = request.get_json()
        url = data.get('url')
        if not url:
            return jsonify({"status": "error", "message": "網址不能為空"}), 400

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
        }
        # 設定 10 秒超時，避免卡住
        response = requests.get(url, headers=headers, timeout=10)

        if response.status_code == 200:
            return jsonify({"status": "success", "message": f"✅ 連結有效 (Status: 200)"})
        else:
            return jsonify({"status": "warning", "message": f"⚠️ 連結回應異常 (Status: {response.status_code})"})

    except Exception as e:
        return jsonify({"status": "error", "message": f"❌ 連線失敗: {str(e)}"}), 500


@misc_bp.route('/brand_assets')
def brand_assets():
    """顯示品牌資產庫"""
    return render_template('brand_assets.html', active_page='brand_assets')
