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

from company_spider.qcwy_company.spider import search_company as qcwy_search_company
from company_spider.zhilianzhaopin_company.spider import crawl_companies


class CompanyController:
    async def search_qcwy_company(self, keyword: str) -> Optional[Dict[str, Any]]:
        return await asyncio.to_thread(qcwy_search_company, keyword)

    async def search_zhilian_company(self, keyword: str, city: Optional[str] = None) -> List[Dict[str, Any]]:
        params = {"kw": keyword}
        if city:
            params["city"] = city
        return await asyncio.to_thread(crawl_companies, params, 10)


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