import scrapy
import json
import re


def first_or_empty(xpobj, path):
    text = xpobj.xpath(path).get()
    return text.strip() if text else ""


class YutianTopSpider(scrapy.Spider):
    name = 'yutian_top_compary'
    allowed_domains = ['yutian.top']
    base_url = "https://zp.yutian.top"
    start_urls = ['https://zp.yutian.top/search']
    headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'content-type': 'application/json;charset=UTF-8',
        'origin': 'https://www.yutian.top',
        'pragma': 'no-cache',
        'priority': 'u=1, i',
        'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
    }

    def start_requests(self):
        for i in range(1, 40):
            yield scrapy.Request(
                url=f'https://zp.yutian.top/search?keywords=&page={i}',
                method='GET',
                headers=self.headers,
                dont_filter=True,
                callback=self.parse,
            )

    def parse(self, response):
        status_code = response.status
        if status_code != 200:
            self.logger.error(f"Request failed with status code: {status_code}")
            return
        for node in response.xpath('//*[@onclick]'):
            onclick = node.attrib.get("onclick", "")
            match = re.search(r"/position/\d+\.html", onclick)
            if match:
                url = self.base_url + match.group(0)
                yield scrapy.Request(
                    url=url,
                    headers=self.headers,
                    callback=self.parse_compary,
                    dont_filter=True,
                )

    def parse_compary(self, response):
        self.logger.info(f"Parsing compary: {response.url}")
        if response.status != 200:
            self.logger.error(f"Not 200 page: {response.url}")
            return

        title = first_or_empty(response, "//div[@class='top-title']/span/text()")
        nature = ""
        category = ""
        region = ""
        experience = ""
        education = ""
        infokey = response.xpath("//span[@class='job-info-item']/span[@class='label']/text()").getall()
        infovalue = response.xpath("//span[@class='job-info-item']/span[@class='value hide-text']/text()").getall()
        if infokey and infovalue:
            for index, key in enumerate(infokey):
                if '工作性质' in key:
                    nature = infovalue[index].strip()
                if "职位类别" in key:
                    category = infovalue[index].strip()
                if "工作区域" in key:
                    region = infovalue[index].strip()
                if "招聘人数" in key:
                    openings = infovalue[index].strip()
                if "工作年限" in key:
                    experience = infovalue[index].strip()
                if "学历要求" in key:
                    education = infovalue[index].strip()
        salary = first_or_empty(response, "//div[@class='salary']/text()")
        description = first_or_empty(response, "//div[@class='job-describe']/text()")
        contact_name = first_or_empty(response,
                                      "//div[@class='bg-mask']/div[@class='connect-info-item']/span[@class='value']/text()")
        contact_info = first_or_empty(response,
                                      "(//div[@class='bg-mask']//div[@class='connect-info-item'])[2]/span[@class='value']/text()")
        benefits = ""
        if openings == "若干":
            openings = 1
        else:
            openings = 1
        company_name = first_or_empty(response, "//div[@class='company-name']/a/text()")
        meta = {
            "title": title,  # 职位名称
            "nature": nature,  # 职位性质
            "category": category,  # 职位类别
            "region": region,  # 职位区域
            "experience": experience,  # 工作经历要求
            "education": education,  # 学历要求
            "salary": salary,  # 职位薪资
            "position_status": 1,  # 职位状态
            "description": description,  # 职位描述(详情)
            "contact_name": contact_name,  # 联系人姓名
            "contact_info": contact_info,  # 联系方式
            "benefits": benefits,  # 职位福利
            "openings": openings,  # 招聘人数
            "website_id": 2,  # 网站ID
            "company_name": company_name,  # 所属企业名称
        }
        company_id = first_or_empty(response, "//div[@class='job-detail']/@data-io-company-id")
        yield scrapy.Request(
            url=f"https://zp.yutian.top/company/{company_id}.html",
            headers=self.headers,
            callback=self.parse_company,
            meta=meta,
            dont_filter=True,
        )

    def parse_company(self, response):
        name = first_or_empty(response, '//h1[@class="company-header-top-detail-name hide-txt"]/text()')
        category_and_size_key = response.xpath('//div[@class="company-header-bottom-item-label"]/text()').getall()
        category_and_size_value = response.xpath(
            '//div[@class="company-header-bottom-item-text hide-txt"]/text()').getall()

        category = ""
        size = ""
        if len(category_and_size_key) == 2 and len(category_and_size_value) == 2:
            if category_and_size_key[0].strip() == "公司类别 ：":
                category = category_and_size_value[0].strip()
            if category_and_size_key[1].strip() == "公司规模 ：":
                size = category_and_size_value[1].strip()
        elif len(category_and_size_key) == 1 and len(category_and_size_value) == 1:
            key = category_and_size_key[0].strip()
            val = category_and_size_value[0].strip()
            if key == "公司类别 ：":
                category = val
            elif key == "公司规模 ：":
                size = val

        introduction = first_or_empty(response, '//div[@class="job-left-content-des"]/text()')
        address = first_or_empty(response, '//div[@class="job-left-content-address"]/text()')

        company_type = ""
        founded_date = ""
        benefits_str = ""

        company_data = {
            "name": name,  # 公司名称
            "category": category,  # 公司行业类别
            "size": size,  # 公司规模
            "company_type": company_type,  # 公司性质（暂未提取）
            "founded_date": founded_date,  # 成立时间（暂未提取）
            "introduction": introduction,  # 公司简介
            "address": address,  # 公司地址
            "benefits": benefits_str,  # 员工福利（暂未提取）
            "website_id": 2,  # 来源网站 ID
        }
        data = {
            "compary": company_data,
            "position": response.meta,
        }

        yield data
