from typing import Any, Dict, Optional

from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field

from app.controllers.company import CompanyController, create_company_controller
from app.schemas.base import Fail, Success


router = APIRouter(tags=["公司搜索"])


class ZhilianSearchRequest(BaseModel):
    keyword: str = Field(..., description="公司名称关键词")
    city: Optional[str] = Field(None, description="城市名称，如北京")


class QcwySearchRequest(BaseModel):
    keyword: str = Field(..., description="公司名称关键词")


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


@router.post("/zhilian/search", summary="智联招聘公司搜索")
async def zhilian_search_company(
    request: ZhilianSearchRequest,
    controller: CompanyController = Depends(get_company_controller),
) -> Dict[str, Any]:
    try:
        data = await controller.search_zhilian_company(request.keyword, request.city)
        return Success(data=data)
    except Exception as exc:
        return Fail(msg=f"智联公司搜索失败: {exc}")


@router.post("/qcwy/search", summary="前程无忧公司搜索")
async def qcwy_search_company(
    request: QcwySearchRequest,
    controller: CompanyController = Depends(get_company_controller),
) -> Dict[str, Any]:
    try:
        data = await controller.search_qcwy_company(request.keyword)
        return Success(data=data)
    except Exception as exc:
        return Fail(msg=f"前程无忧公司搜索失败: {exc}")
