package controllers

import (
	"fmt"
	"net/http"
	"os"

	csvimporter "catalogue.midpdsa.com/modules/csv"
	"catalogue.midpdsa.com/src/db/catalogue"
	"github.com/gin-gonic/gin"
)

type CSVImportController struct {
	queries *catalogue.Queries
}

func NewCSVImportController(queries *catalogue.Queries) *CSVImportController {
	return &CSVImportController{queries: queries}
}

func (ctrl *CSVImportController) ImportAll(c *gin.Context) {
	csvDir := c.DefaultQuery("path", CSVDir())

	importer := csvimporter.NewImporter(ctrl.queries)

	results, err := importer.ImportAll(csvDir)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	var totalInserted, totalErrors int
	for _, r := range results {
		totalInserted += r.Inserted
		totalErrors += r.Errors
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "Import completed",
		"results": results,
		"total":   totalInserted,
		"errors":  totalErrors,
	})
}

func (ctrl *CSVImportController) ImportAGShock(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("AG_shock.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportAGShock(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportAGStrut(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("AG_strut.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportAGStrut(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportAGLeg(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("AG_leg.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportAGLeg(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportAGPesado(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("AG_pesado.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportAGPesado(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportMonroeLD(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("monroe_ld.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportMonroeLD(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportMonroeHD(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("monroe_hd.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportMonroeHD(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportTRWSachs(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("trw_sachs.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportTRWSachs(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportWagner(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("wagner_brake.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportWagner(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportBrembo(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("brembo.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportBrembo(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportCano(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("cano_Hoja1.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportCano(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportCanoDisk(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("cano_disk.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportCanoDisk(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportGRCBrakes(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("frenos-deseable.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportGRCBrakes(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportGRCShocks(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("amort-deseable.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportGRCShocksHeavy(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func (ctrl *CSVImportController) ImportPrestone(c *gin.Context) {
	csvPath := c.DefaultQuery("path", CSVPath("prestone.csv"))

	importer := csvimporter.NewImporter(ctrl.queries)

	result, err := importer.ImportPrestone(csvPath)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"result": result})
}

func init() {
	fmt.Println("CSV Import module loaded")
	_ = os.Getenv("MIP_DB")
}
