import pandas as pd, os
from products.models import Products
from collections import Counter

BASE_PATH = os.getcwd()


def update_products_new_arrival():
    file_path = f"{BASE_PATH}/products/output.xlsx"
    df = pd.read_excel(file_path)
    df = df.dropna(how="all")
    file_contents = df.to_dict(orient="records")

    edp_numbers = []

    for row in file_contents:
        edp_number = row["EDP #"]
        edp_numbers.append(edp_number)

    edp_numbers = set(edp_numbers)

    products = Products.objects.filter(edp_number__in=edp_numbers).values_list(
        "edp_number", flat=True
    )

    print(products.count())
    
    products.update(is_new_arrival=True)
    
    print(f"Updated status of {products.count()} new arrival statuses.")
    
def remove_new_arrival_status():
    file_path = f"{BASE_PATH}/products/output.xlsx"
    df = pd.read_excel(file_path)
    df = df.dropna(how="all")
    file_contents = df.to_dict(orient="records")

    edp_numbers = []

    for row in file_contents:
        edp_number = row["EDP #"]
        edp_numbers.append(edp_number)
        
    products = Products.objects.filter(is_new_arrival=True).exclude(edp_number__in=edp_numbers)
    products.update(is_new_arrival=False)