"""
NMPA 药品数据爬虫
目标：国家药品监督管理局药品数据库
URL: https://www.nmpa.gov.cn/datasearch/

采集内容：
  - 批准文号
  - 药品通用名 / 商品名
  - 生产企业
  - 药品规格
  - 说明书
"""
import logging
from typing import Optional
from crawlers.spider import BasePharmSpider

logger = logging.getLogger(__name__)


class NMPASpider(BasePharmSpider):
    name = "nmpa"
    allowed_domains = ["nmpa.gov.cn", "cdn.nmpa.gov.cn"]

    start_urls = [
        "https://www.nmpa.gov.cn/datasearch/search-result.html",
    ]

    def parse(self, response):
        """
        Phase 2 实现具体解析逻辑。
        NMPA 页面大量使用 JS 渲染，需要 Selenium 或 Playwright 配合。
        """
        logger.info(f"Fetching NMPA data from {response.url}")
        yield {"status": "placeholder", "message": "Phase 2 实现"}


class NMPADetailSpider(BasePharmSpider):
    name = "nmpa_detail"

    def __init__(self, approval_number: Optional[str] = None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.approval_number = approval_number

    def start_requests(self):
        if self.approval_number:
            url = f"https://www.nmpa.gov.cn/datasearch/search-info.html?approvalNumber={self.approval_number}"
            yield self.make_requests_from_url(url)
