import re
import urllib
from typing import Iterable
import scrapy
from lxml import etree
from scrapy import Request


class ZunHuaComSpider(scrapy.Spider):
    name = 'zhrczp_com_resume'
    allowed_domains = ['www.zhrczp.com']
    start_urls = ['https://www.zhrczp.com/member/index.php']
    cookies = {
        'Hm_lvt_115013d5b34e45eb09d0baedeb1c845a': '1745062179',
        'HMACCOUNT': 'B05D7338A384928F',
        'Hm_lpvt_115013d5b34e45eb09d0baedeb1c845a': '1745062980',
        'PHPSESSID': 'f2o89gakk79jl43hcl4ptnea3r',
        'uid': '60531',
        'shell': '9246a8c91784a3981081a37dd4bdcef9',
        'usertype': '2',
        'userdid': '0',
        'amtype': '0',
        'jobrefresh': '1',
        'gzh': '1',
        'acw_tc': '1a0c63d517450682931821154e003e6b210262ee0f2d393aa4e3b2a163053b',
        'pc_bannerFlag': '1',
    }
    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': 'none',
        '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/135.0.0.0 Safari/537.36',
        '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"',
    }

    def start_requests(self) -> Iterable[Request]:
        for page in range(1, 251):
            params = {
                'c': 'resume',
                'page': str(page),
            }
            query_string = urllib.parse.urlencode(params)
            full_url = f"{self.start_urls[0]}?{query_string}"
            yield scrapy.Request(
                url=full_url,
                method='GET',
                headers=self.headers,
                cookies=self.cookies,
                callback=self.parse,

            )

    def parse(self, response):
        status_code = response.status
        print(status_code)
        html = response.text
        res = re.findall(r"com_lookresume_check\('(.+?)','1'\)", html)
        resume_id_list = list(set(res))
        for item in resume_id_list:
            params = {
                'c': 'hr',
                'act': 'resumeInfo',
                'eid': item,
                'state': 'undefined',
                'from': '',
            }
            query_string = urllib.parse.urlencode(params)
            full_url = f"{self.start_urls[0]}?{query_string}"
            yield scrapy.Request(
                url=full_url,
                method='GET',
                headers=self.headers,
                cookies=self.cookies,
                callback=self.parse2,
                meta={'resume_id': item},
            )

    def parse2(self, response):
        resume_id = response.meta.get('resume_id')

        parts_raw = response.xpath('//div[@class="hr_resume_item"]/text()').get()
        extra_span = response.xpath('//div[@class="hr_resume_item"]/span/text()').get()

        parts = []
        if parts_raw:
            cleaned = re.sub(r'\s+', ' ', parts_raw).strip()
            parts = [p.strip() for p in cleaned.split('·') if p.strip()]
        if extra_span:
            parts.append(extra_span.strip())

        current_location = ''
        if parts and '现居' in parts[-1]:
            current_location = parts[-1]
            parts = parts[:-1]

        text = " ".join(parts)
        age = re.search(r'(\d{2})岁', text)
        height = re.search(r'(\d{2,3})\s*cm', text, re.I)
        weight = re.search(r'(\d{2,3})\s*(kg|公斤)', text, re.I)
        experience = re.search(r'(无经验|1年以下|\d{1,2}-\d{1,2}年|(?:\d{1,2})年以上|(?:\d{1,2})年经验)', text)
        education = re.search(r'(初中|高中|中专|大专|本科|硕士|博士)', text)
        marital = re.search(r'(已婚|未婚)', text)
        ethnic = re.search(r'(汉|满|回|壮|蒙古)', text)

        # 页面字段 XPath 提取
        name = response.xpath('//span[@class="hr_resume_username"]/text()').get()
        update_time_raw = response.xpath('//span[@class="hr_resume_time_l "]/text()').get()
        update_time = re.sub(r'^更新时间[:：]?', '', update_time_raw).strip() if update_time_raw else ''

        job_funcs = response.xpath('//span[@class="yun_newedition_yx_job"]/text()').getall()
        job_titles = response.xpath('//li[span[contains(text(),"意向岗位")]]/text()').get()
        industry = response.xpath('//li[span[contains(text(),"从事行业")]]/text()').get()
        salary = response.xpath('//li[span[contains(text(),"期望薪资")]]/text()').get()
        report_time = response.xpath('//li[span[contains(text(),"到岗时间")]]/text()').get()
        job_type = response.xpath('//li[span[contains(text(),"工作性质")]]/text()').get()
        job_status = response.xpath('//li[span[contains(text(),"求职状态")]]/text()').get()
        location = response.xpath('//li[span[contains(text(),"工作地点")]]/text()').get()
        yield {
            'resume_id': resume_id,
            'name': name.strip() if name else None,
            'age': age.group(1) if age else None,
            'height': height.group(1) if height else None,
            'weight': weight.group(1) if weight else None,
            'work_years': experience.group(1) if experience else None,
            'education': education.group(1) if education else None,
            'marital_status': marital.group(1) if marital else None,
            'ethnicity': ethnic.group(1) if ethnic else None,
            'current_location': current_location.replace('现居', '').strip() if current_location else None,
            'update_time': update_time[3:] if update_time else None,
            'job_function': ', '.join([j.strip() for j in job_funcs]) if job_funcs else None,
            'intended_position': job_titles.strip() if job_titles else None,
            'industry': industry.strip() if industry else None,
            'expected_salary': salary.strip() if salary else None,
            'available_time': report_time.strip() if report_time else None,
            'job_property': job_type.strip() if job_type else None,
            'job_status': job_status.strip() if job_status else None,
            'job_location': location.strip() if location else None,
            'source_id': 1,
        }
