#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Google Drive 自動匯入路由
"""

from flask import Blueprint, render_template, jsonify, request
import logging
from services.import_service import import_service
from services.google_drive_service import drive_service

# 建立 Blueprint
auto_import_bp = Blueprint('auto_import', __name__)

logger = logging.getLogger(__name__)


@auto_import_bp.route('/auto_import')
def auto_import_index():
    """自動匯入主頁"""
    return render_template('auto_import_index.html', active_page='auto_import')


@auto_import_bp.route('/api/import_jobs', methods=['GET'])
def get_import_jobs():
    """取得匯入任務清單"""
    try:
        limit = request.args.get('limit', 20, type=int)
        jobs = import_service.get_recent_jobs(limit=limit)

        return jsonify({
            'success': True,
            'data': jobs
        })

    except Exception as e:
        logger.error(f"取得匯入任務清單失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'取得匯入任務清單失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/import_jobs/<int:job_id>', methods=['GET'])
def get_import_job(job_id):
    """取得單一匯入任務狀態"""
    try:
        job = import_service.get_job_status(job_id)

        if job:
            return jsonify({
                'success': True,
                'data': job
            })
        else:
            return jsonify({
                'success': False,
                'message': '找不到該任務'
            }), 404

    except Exception as e:
        logger.error(f"取得匯入任務狀態失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'取得匯入任務狀態失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/import_config', methods=['GET'])
def get_import_config():
    """取得匯入配置"""
    try:
        folder_path = import_service.get_config('gdrive_folder_path', '業績報表/當日業績')
        file_pattern = import_service.get_config('gdrive_file_pattern', '即時業績_當日')

        return jsonify({
            'success': True,
            'data': {
                'folder_path': folder_path,
                'file_pattern': file_pattern
            }
        })

    except Exception as e:
        logger.error(f"取得匯入配置失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'取得匯入配置失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/import_config', methods=['POST'])
def set_import_config():
    """設定匯入配置"""
    try:
        data = request.json
        folder_path = data.get('folder_path')
        file_pattern = data.get('file_pattern')

        if folder_path:
            import_service.set_config(
                'gdrive_folder_path',
                folder_path,
                'string',
                'Google Drive 資料夾路徑'
            )

        if file_pattern:
            import_service.set_config(
                'gdrive_file_pattern',
                file_pattern,
                'string',
                'Excel 檔案名稱模式'
            )

        return jsonify({
            'success': True,
            'message': '配置已更新'
        })

    except Exception as e:
        logger.error(f"設定匯入配置失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'設定匯入配置失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/test_drive_connection', methods=['POST'])
def test_drive_connection():
    """測試 Google Drive 連接"""
    try:
        # 嘗試認證
        if drive_service.authenticate():
            return jsonify({
                'success': True,
                'message': 'Google Drive 連接成功'
            })
        else:
            return jsonify({
                'success': False,
                'message': 'Google Drive 認證失敗'
            }), 400

    except Exception as e:
        logger.error(f"測試 Google Drive 連接失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'測試連接失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/list_drive_files', methods=['POST'])
def list_drive_files():
    """列出 Google Drive 檔案"""
    try:
        data = request.json
        folder_path = data.get('folder_path', '業績報表/當日業績')
        file_pattern = data.get('file_pattern', '即時業績_當日')

        files = drive_service.list_files_in_folder(folder_path, file_pattern)

        return jsonify({
            'success': True,
            'data': files,
            'count': len(files)
        })

    except Exception as e:
        logger.error(f"列出 Google Drive 檔案失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'列出檔案失敗: {str(e)}'
        }), 500


@auto_import_bp.route('/api/manual_import', methods=['POST'])
def manual_import():
    """手動觸發匯入"""
    try:
        result = import_service.auto_import_from_drive()

        return jsonify(result)

    except Exception as e:
        logger.error(f"手動匯入失敗: {str(e)}")
        return jsonify({
            'success': False,
            'message': f'手動匯入失敗: {str(e)}'
        }), 500
