package config

import (
	"SafelineAPI/internal/app/logger"
	"log"
)

type ApplyCert struct {
	Days              int    `json:"Days"`
	Email             string `json:"Email"`
	SavePath          string `json:"SavePath"`
	DNSProviderConfig `json:"DNSProviderConfig"`
}

func (applyCert *ApplyCert) GetDays() int {
	return applyCert.Days
}

func (applyCert *ApplyCert) GetEmail() string {
	return applyCert.Email
}

func (applyCert *ApplyCert) Verify() bool {
	var flag = false
	if applyCert.Days == 0 {
		applyCert.Days = 30
	}

	if applyCert.SavePath == "" {
		applyCert.SavePath = "/tmp/ssl"
	}

	if applyCert.DNSProvider == "" {
		logger.Warning.Printf("No %sDNS provider%s set: please check the %sApplyCert.DNSProviderConfig.DNSProvider%s parameter", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		flag = true
	}
	if applyCert.Email == "" {
		logger.Warning.Printf("No %scertificate request email%s set: please check the %sApplyCert.Email%s parameter", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		flag = true
	}
	if !flag {
		log.Printf("%sApplyCert%s configuration check completed!", logger.Cyan, logger.Reset)
	}
	return flag
}

func (applyCert *ApplyCert) VerifyCommand() bool {
	var flag = false
	if applyCert.Days == 0 {
		applyCert.Days = 30
	}

	if applyCert.SavePath == "" {
		applyCert.SavePath = "/tmp/ssl"
	}

	if applyCert.DNSProvider == "" {
		logger.Warning.Printf("No %sDNS provider%s set: please check the %s-D%s command-line argument", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		flag = true
	}
	if applyCert.Email == "" {
		logger.Warning.Printf("No %scertificate request email%s set: please check the %s-e%s command-line argument", logger.Cyan, logger.Reset, logger.Yellow, logger.Reset)
		flag = true
	}
	if !flag {
		log.Printf("%sApplyCert%s configuration check completed!", logger.Cyan, logger.Reset)
	}
	return flag

}
