package main

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"database/sql"

	_ "github.com/mattn/go-sqlite3"
	"github.com/xuri/excelize/v2"
)

var schema = `
CREATE TABLE IF NOT EXISTS data (
    description text,
	sku text,
    level2 text,
    level3 text,
	revenue float,
	cost float,
	quantity integer,
	year integer,
	month integer
);
`

type Data struct {
	Description string
	SKU         string
	Level2      string
	Level3      string
	Revenue     float64
	Cost        float64
	Quantity    int64
	Year        int64
	Month       int64
}

func ProcessExcel(path string) {
	fmt.Println("Processing excel file...")
	start := time.Now()

	f, err := excelize.OpenFile(path)
	checkErr(err)

	rows, err := f.GetRows("Sheet1")
	checkErr(err)

	const offset = 3
	data := []Data{}
	for i, row := range rows {
		if i >= offset {
			new_data := Data{SKU: row[33], Level2: row[29], Level3: row[30], Description: row[44]}

			//Parse Rev
			rev, err := strconv.ParseFloat(row[72], 64)
			checkErr(err)
			new_data.Revenue = rev

			//Parse Cost
			cost, err := strconv.ParseFloat(row[16], 64)
			checkErr(err)
			new_data.Cost = cost

			//Parse Quantity
			qty, err := strconv.ParseInt(row[71], 10, 64)
			checkErr(err)
			new_data.Quantity = qty

			//Parse Time
			day, err := time.Parse("2006-01", row[26])
			checkErr(err)
			new_data.Year = int64(day.Year())
			new_data.Month = int64(day.Month())

			data = append(data, new_data)
		}
	}

	db, err := sql.Open("sqlite3", "data.db")
	checkErr(err)
	defer db.Close()

	db.Exec("DROP TABLE IF EXISTS data")
	db.Exec(schema)

	values := []string{}
	for _, elm := range data {
		values = append(values, fmt.Sprintf(`("%s","%s","%s","%s",%f,%f,%d,"%d","%d")`, strings.ReplaceAll(elm.Description, "\"", ""), elm.SKU, elm.Level2, elm.Level3, elm.Revenue, elm.Cost, elm.Quantity, elm.Year, elm.Month))
	}

	stmt := "INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month) VALUES " + strings.Join(values, ",")
	_, err = db.Exec(stmt)
	checkErr(err)

	elapsed := time.Since(start)
	fmt.Printf("Operation complete in %s\n", elapsed)
}
