"""
公司搜索控制器 — 使用新 crawler service 替代已删除的 company_spider
"""

import asyncio
from typing import Any, Dict, List, Optional

from loguru import logger

from app.services.crawler.qcwy import QcwyService
from app.services.crawler.zhilian import ZhilianService


class CompanyController:
    def __init__(self):
        self._qcwy = QcwyService()
        self._zhilian = ZhilianService()

    async def search_qcwy_company(self, keyword: str) -> Optional[Dict[str, Any]]:
        try:
            return await asyncio.to_thread(self._qcwy.get_company_info, keyword)
        except Exception as e:
            logger.error(f"Qcwy company search failed: {e}")
            return None

    async def search_zhilian_company(
        self, keyword: str, city: Optional[str] = None
    ) -> List[Dict[str, Any]]:
        try:
            result = await asyncio.to_thread(
                self._zhilian.search_company_jobs_by_name, keyword
            )
            if result and isinstance(result, dict):
                data = result.get("data", {})
                if isinstance(data, dict):
                    return data.get("list", [])
                return []
            return []
        except Exception as e:
            logger.error(f"Zhilian company search failed: {e}")
            return []


def create_company_controller() -> CompanyController:
    return CompanyController()
