package config

import (
	"flag"

	"git.paulbsd.com/paulbsd/coronafana/utils"
	"gopkg.in/ini.v1"
)

// GetConfig fetch configuration
func (config *Config) GetConfig() (err error) {
	var configfile string

	flag.Usage = utils.Usage

	flag.StringVar(&configfile, "configfile", "coronafana.ini", "Config file to use with coronafana section")
	flag.Parse()

	cfg, err := ini.Load(configfile)
	if err != nil {
		return
	}

	*config = Config{
		DbHostname: "localhost",
		DbName:     "database",
		DbUsername: "username",
		DbPassword: "password",
		DbTable:    "corona",
		CoronaURL:  "https://coronavirus.politologue.com/data/coronavirus/coronacsv.aspx?format=json",
	}

	err = cfg.Section("coronafana").MapTo(config)
	if err != nil {
		return
	}

	config.DbSchemaGlobal = `
		CREATE TABLE IF NOT EXISTS coronaglobaldata
		(
			id int(8) NOT NULL AUTO_INCREMENT,
			date datetime NOT NULL,
			infection int(8) DEFAULT NULL,
			deces int(8) DEFAULT NULL,
			guerisons varchar(50) DEFAULT NULL,
			tauxdeces float(10) DEFAULT NULL,
			tauxguerison float(10) DEFAULT NULL,
			tauxinfection float(10) DEFAULT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY date (date) USING BTREE
		)
		ENGINE=InnoDB DEFAULT CHARSET=utf8;
	`

	config.DbInsertGlobal = `
		INSERT INTO coronaglobaldata (date, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection)
		VALUES (:date, :infection, :deces, :guerisons, :tauxdeces, :tauxguerison, :tauxinfection)
		ON DUPLICATE KEY UPDATE date=:date,infection=:infection, deces=:deces, guerisons=:guerisons, tauxdeces=:tauxdeces, tauxguerison=:tauxguerison, tauxinfection=:tauxinfection;
	`

	config.DbMaxDateGlobal = `
		SELECT IFNULL(SUBDATE(MAX(date), INTERVAL 3 DAY), "0001-01-01 00:00:00")
		FROM coronaglobaldata;
	`

	config.DbSchemaPays = `
		CREATE TABLE IF NOT EXISTS coronapaysdata
		(
			id int(8) NOT NULL AUTO_INCREMENT,
			date datetime NOT NULL,
			pays varchar(50) NOT NULL,
			infection int(8) DEFAULT NULL,
			deces int(8) DEFAULT NULL,
			guerisons varchar(50) DEFAULT NULL,
			tauxdeces float(10) DEFAULT NULL,
			tauxguerison float(10) DEFAULT NULL,
			tauxinfection float(10) DEFAULT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY date (date,pays) USING BTREE
		)
		ENGINE=InnoDB DEFAULT CHARSET=utf8;
	`

	config.DbInsertPays = `
		INSERT INTO coronapaysdata (date, pays, infection, deces, guerisons, tauxdeces, tauxguerison, tauxinfection)
		VALUES (:date, :pays, :infection, :deces, :guerisons, :tauxdeces, :tauxguerison, :tauxinfection)
		ON DUPLICATE KEY UPDATE date=:date, pays=:pays,infection=:infection, deces=:deces, guerisons=:guerisons, tauxdeces=:tauxdeces, tauxguerison=:tauxguerison, tauxinfection=:tauxinfection;
	`

	config.DbMaxDatePays = `
		SELECT IFNULL(SUBDATE(MAX(date), INTERVAL 3 DAY), "0001-01-01 00:00:00")
		FROM coronapaysdata;
	`

	return
}

// Config is the global config of g2g
type Config struct {
	DbHostname      string `ini:"db_hostname"`
	DbName          string `ini:"db_name"`
	DbUsername      string `ini:"db_username"`
	DbPassword      string `ini:"db_password"`
	DbTable         string `ini:"db_table"`
	DbSchemaGlobal  string
	DbInsertGlobal  string
	DbMaxDateGlobal string
	DbSchemaPays    string
	DbInsertPays    string
	DbMaxDatePays   string
	CoronaURL       string
}
