# parserIndeed.py
import itertools
from category import category, jobs, developer_experience
import ipdb  # TODO delete debug
from datetime import timedelta, datetime
import typing as tp
from jobClasses import Vacancy, indeedVacancy
import requests
from bs4 import BeautifulSoup as bs
import ipdb

def getIndeedVacancies(pages=5) -> tp.List[tuple]:
    indeedVacanciesList = []
    for page in range(0, pages):
        URL = (
            "https://ru.indeed.com/jobs?q=developer&l=Санкт-Петербург&start="
            + str(page)
            + "0"
        )
        page = requests.get(URL)
        soup = bs(page.content, "html.parser")

        indeedVacanciesPart = []
        results = soup.find_all("a", attrs={"data-hide-spinner": "true"})
        for vacancy in results:
            id = vacancy["data-jk"]

            try:
                name = vacancy.find("h2").find_all("span")[1].text
            except:
                name = vacancy.find("h2").find("span").text

            companyName = vacancy.find("span", attrs="companyName").text

            try:
                salaryRange = (
                    vacancy.find("span", attrs={"class": "salary-snippet"})
                    .text.replace("\xa0", "")
                    .replace(" руб. в месяц", "")
                    .strip(" ")
                    .replace(" ", "")
                    .split("-")
                )
            except:
                salaryRange = vacancy.find("span", attrs={"class": "salary-snippet"})
            descriptions = vacancy.find("div", attrs={"class": "job-snippet"}).find_all(
                "li"
            )
            descriptions = [description.text for description in descriptions]
            if descriptions:
                if len(descriptions) > 1:
                    descriptions = descriptions[0] + descriptions[1]

                descriptions = descriptions[0]

            last_update = raw_date_to_str(
                vacancy.find("span", attrs={"class": "date"}).text.replace("\n", "")
            )

            indeedVacanciesList.append(
                (id, name, companyName, last_update, salaryRange, descriptions)
            )

    return indeedVacanciesList


def loadIndeedToSql(indeedVacList) -> None:
    return indeedVacList


def parseIndeedVacancies(rawVaccs: list) -> list:
    rawVaccs = getIndeedVacancies()
    finalVacs = []

    for vac in rawVaccs:

        job = category(jobs, vac)
        dev_exp = category(developer_experience, vac)
        if vac[4] and len(vac[4]) > 1:
            finevac = indeedVacancy(
                vac[0],
                vac[1],
                vac[2],
                vac[3],
                job,
                dev_exp,
                int(vac[4][0]),
                int(vac[4][1]),
            )
        elif vac[4] and len(vac[4]) == 1:
            finevac = indeedVacancy(
                vac[0],
                vac[1],
                vac[2],
                vac[3],
                job,
                dev_exp,
                int(vac[4][0]),
                int(vac[4][0]),
            )
        else:
            finevac = indeedVacancy(
                vac[0], vac[1], vac[2], vac[3], job, dev_exp, None, None
            )

        finalVacs.append(finevac)

    return finalVacs


def raw_date_to_str(raw_date):
    raw_date = raw_date.lower()
    if "+" in raw_date or "более" in raw_date:
        delta = timedelta(days=32)
        return (datetime.now() - delta).strftime("%Y-%m-%d")
    else:
        parts = raw_date.split()
        for part in parts:
            if part.isdigit():
                delta = timedelta(days=part.isdigit())
                return (datetime.now() - delta).strftime("%Y-%m-%d")
    return ""
