import pandas as pd
from db_bk.models import Order

import os


def parse_csv_to_orders(file_path):
    orders = []
    df = pd.read_csv(file_path, delimiter=';', encoding='cp1252', date_format='%d/%m/%Y')

    for _, row in df.iterrows():
        print(row['Status pedido'])
        order = Order(
            id=row['Pedido'],
            date=row['Data'],
            partial_total=row['Subtotal produtos'],
            taxes=row['Impostos'],
            point_sale=row['Canal de venda'],
            shipment=row['Frete tipo'],
            shipment_value=row['Frete valor'],
            payment_form=row['Pagamento tipo'],
            payment_date=row['Pagamento data'],
            shipment_date=row['Envio data'],
            access_code=row['Envio codigo'],
            store_note=row['Obs. loja'],
            status=row['Status pedido'],
            discount_coupon=row['Cupom desconto']
        )
        orders.append(order)
    return orders


def load_orders_csv(directory_path):
    all_orders = []

    for filename in os.listdir(directory_path):
        if filename.endswith('.csv'):
            file_path = os.path.join(directory_path, filename)
            orders = parse_csv_to_orders(file_path)
            all_orders.extend(orders)

    for order in all_orders:
        print(order.id, order.date, order.status)


load_orders_csv('../csv/tray-orders/')
