import * as fs from "fs";
import * as path from "path";
import { parse } from 'csv-parse';
import { bb_products } from "/home/don/code/ballistics-builder/src/drizzle/schema/schema";
import { db } from '/home/don/code/ballistics-builder/src/db/index';

type BB_PRODUCTS = {
    UPC: string;
    SKU: string;
    'Manufacturer Id': string;
    "Brand Name": string;
    'Product Name': string;
    'Long Description': string;
    'Short Description': string;
    Department: string;
    Category: string;
    SubCategory: string;
    'Thumb URL': string;
    'Image URL': string;
    'Buy Link': string;
    Keywords: string;
    Reviews: string;
    'Retail Price': string;
    'Sale Price': string;
    'Brand Page Link': string;
    'Brand Logo Image': string;
    'Product Page View Tracking': string;
    'Parent Group ID': string;
    Color: string;
    Size: string;
    Pattern: string;
    Material: string;
    'Age Group': string;
    Gender: string;
    Availability: string;
    'Google Product Category': string;
    'Medium Image URL': string;
    'Variants XML': string;
    GTIN: string;
    'Key Words': string;
    'Product Content Widget': string;
    'Google Categorization': string;
    'Item Based Commission': string;
    'Item Based Commission Rate': string;

};

(() => {
    const csvFilePath = path.resolve(__dirname, '/home/don/code/ballistics-builder/src/lib/dataImports/Brownells_cleaned.csv');

    const headers = ['SKU', 'Manufacturer Id', 'Brand Name', 'Product Name', 'Long Description', 'Short Description', 'Department', 'Category', 'SubCategory', 'Thumb URL', 'Image URL', 'Buy Link', 'Keywords', 'Reviews', 'Retail Price', 'Sale Price', 'Brand Page Link', 'Brand Logo Image', 'Product Page View Tracking', 'Parent Group ID', 'Color', 'Size', 'Pattern', 'Material', 'Age Group', 'Gender', 'UPC', 'Availability', 'Google Product Category', 'Medium Image URL', 'Variants XML', 'GTIN', 'Key Words', 'Product Content Widget', 'Google Categorization', 'Item Based Commission', 'Item Based Commission Rate', 'Item Based Commission Rule'
    ];

    const fileContent = fs.readFileSync(csvFilePath, { encoding: 'utf-8' });
    let rows: BB_PRODUCTS[] = [];

    parse(fileContent, {
        delimiter: ',',
        columns: headers,
        from_line: 2,
    }, (error, result: BB_PRODUCTS[]) => {
        if (error) {
            console.error(error);
        }

        // Insert data into the database
        for (const row of result) {
            db.insert(bb_products).values({
                
                upc: row["UPC"],
                sku: row.SKU,
                brandName: row["Brand Name"],
                productName: row["Product Name"],
                longDescription: row["Long Description"],
                shortDescription: row["Short Description"],
                department: row.Department,
                category: row.Category,
                subcategory: row.SubCategory,
                thumbUrl: row["Thumb URL"],
                imageUrl: row["Image URL"],
                buyLink: row["Buy Link"],
                keywords: row.Keywords,
                reviews: row.Reviews,
                retailPrice: row["Retail Price"],
                salePrice: row["Sale Price"],
                brandPageLink: row["Brand Page Link"],
                brandLogoImage: row["Brand Logo Image"],
                productPageViewTracking: row["Product Page View Tracking"],
                parentGroupId: row["Parent Group ID"],
                mediumImageUrl: row["Medium Image URL"],
                productContentWidget: row["Product Content Widget"],
                googleCategorization: row["Google Categorization"],
                itemBasedCommission: row["Item Based Commission"],
                itemBasedCommissionRate: row["Item Based Commission Rate"]
       

                // ...other fields from the CSV row...
            }).execute();
        }
    });
})();
