package static

import (
	"path"
	"path/filepath"
	"strings"

	"github.com/gabriel-vasile/mimetype"
	"gopkg.in/yaml.v2"
)

// Version db version info
type Version struct {
	File    string `yaml:"file"`
	Version uint   `yaml:"version"`
}

// DBVersion all versions
type DBVersion struct {
	Versions []Version `yaml:"versions"`
}

func GetDBVersions() (*DBVersion, error) {
	b, err := Asset("schema/version.yml")
	if err != nil {
		return nil, err
	}

	ver := &DBVersion{}

	if err := yaml.Unmarshal(b, ver); err != nil {
		return nil, err
	}

	return ver, nil
}

func GetSQL(s string) ([]byte, error) {
	return Asset(path.Join("schema", s))
}

func GetSpec() ([]byte, error) {
	return Asset("spec.json")
}

func GetWebFile(fp string) (body []byte, mime string, err error) {
	p := filepath.Join("public/swagger", fp)
	b, err := Asset(p)
	if err != nil {
		return nil, "", err
	}

	mt := mimetype.Detect(b)
	mime = mt.String()
	if strings.HasSuffix(fp, ".css") {
		mime = "text/css"
	} else if strings.HasSuffix(fp, ".js") {
		mime = "text/javascript"
	}

	return b, mime, nil
}
