import logging
import sys

sys.path.append(r".")

import asyncio

from services.company_profile.company_profile import CompanyProfileMaker
from services.company_profile.data_classes.company_info import CompanyInfo
from utils.dynamo_db import DynamoDB

db = DynamoDB()

print("Getting all company info items")
company_info_results = db.get_all_table_items(db.company_info_table)

company_info_results_to_work = company_info_results

companies_to_review = []
print("Validating company info items")
companies_with_errors = 0
for company in company_info_results_to_work:
    try:
        CompanyInfo(**company)
    except Exception as e:
        print(e)
        companies_with_errors += 1
        try:
            CompanyInfo(**company)
        except Exception as e:
            companies_to_review.append(company)
            pass

print(companies_with_errors)
print("re-scrap the following companies")
for company in companies_to_review:
    try:
        url = company["root_url"]
        stock_ticker = company["stock_ticker"] if "stock_ticker" in company else None
        ci = CompanyProfileMaker(url, stock_ticker)
        asyncio.run(ci.main())
    except Exception as e:
        logging.error("Error in re-scraping %s", url)
        logging.error(e)
        pass
