from typing import Iterable
import scrapy
from lxml import etree


def extract_company_data(xpathobj):
    def first_or_empty(path):
        lst = xpathobj.xpath(path)
        return lst[0].strip() if lst else ""

    name = first_or_empty('//h1/a/text()')
    # 公司介绍段落
    intro_list = [t.strip() for t in xpathobj.xpath('//div[@class="company_img_auto"]/p/text()') if t.strip()]
    if not intro_list:
        intro_list = [t.strip() for t in xpathobj.xpath('//div[@class="company_img_auto"]/text()') if t.strip()]
    if not intro_list:
        intro_list = [t.strip() for t in xpathobj.xpath('//div[@class="company_img_auto"]/p/span/text()') if t.strip()]
    # 判断一下有无职位

    job_list = xpathobj.xpath('//div[@class="comshow_job"]/div[@class="firm_post"]')
    introduction = "\r\n".join(intro_list)

    # 如果没有名称或介绍，直接忽略
    if not (name and (introduction or job_list)):
        return None

    # 公司详情信息
    info = [t.strip() for t in xpathobj.xpath('//div[@class="com_details_info"]/text()') if t.strip()]
    category = info[1] if len(info) > 1 else ""
    company_type = info[2] if len(info) > 2 else ""
    size = info[3] if len(info) > 3 else ""
    founded_date = info[4] if len(info) > 4 else ""

    # 公司福利
    benefits = [b.strip() for b in xpathobj.xpath('//div[@class="com_welfare "]/span/text()') if b.strip()]
    benefits_str = " | ".join(benefits)

    address = first_or_empty('//div[@class="firm_name"]/div/text()')

    return {
        "name": name,
        "category": category,
        "size": size,
        "company_type": company_type,
        "founded_date": founded_date,
        "introduction": introduction,
        "address": address,
        "benefits": benefits_str,
        "website": 1,
    }


def get_company_href(xpathobj):
    hrefs = xpathobj.xpath('//div[@class="firm_name"]/span/a/@href')
    return [href.strip() for href in hrefs if href.strip()]


class ZunHuaComSpider(scrapy.Spider):
    name = 'zhrczp_com_compary'
    allowed_domains = ['zhrczp.com', 'www.zhrczp.com']
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    def start_requests(self) -> Iterable[scrapy.Request]:

        for page in range(1, 186):
            self.logger.info(f"Fetching company list page: {page}")
            yield scrapy.Request(
                url=f"https://www.zhrczp.com/company/list/0-0-0-0-0-0-{page}.html",
                headers=self.headers,
                callback=self.parse,
                dont_filter=True,
            )
        for page in range(1, 10):
            self.logger.info(f"Fetching company list page: {page}")
            yield scrapy.Request(
                url=f"https://www.zhrczp.com/company/list/0-0-0-0-0-1-{page}.html",
                headers=self.headers,
                callback=self.parse,
                dont_filter=True,
            )

    def parse(self, response):
        xpathobj = etree.HTML(response.text)
        company_href = get_company_href(xpathobj)
        if company_href:
            for href in company_href:
                self.logger.debug(href)
                yield scrapy.Request(
                    url=href,
                    headers=self.headers,
                    callback=self.parse_company,
                    dont_filter=True,
                )

    def parse_company(self, response):
        xpathobj = etree.HTML(response.text)
        company_data = extract_company_data(xpathobj)
        if company_data:
            yield company_data
