/*
 * Copyright 2022 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package meta

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"

	gv "github.com/hashicorp/go-version"
	"gopkg.in/yaml.v2"
)

const ManifestFile = ".hz"

type Manifest struct {
	Version    string `yaml:"hz version"`
	HandlerDir string `yaml:"handlerDir"`
	ModelDir   string `yaml:"modelDir"`
	RouterDir  string `yaml:"routerDir"`
}

var GoVersion *gv.Version

func init() {
	// valid by unit test already, so no need to check error
	GoVersion, _ = gv.NewVersion(Version)
}

func (manifest *Manifest) InitAndValidate(dir string) error {
	m, err := loadConfigFile(filepath.Join(dir, ManifestFile))
	if err != nil {
		return fmt.Errorf("can not load \".hz\", err: %v", err)
	}

	if len(m.Version) == 0 {
		return fmt.Errorf("can not get hz version form \".hz\", current project doesn't belong to hertz framework")
	}

	*manifest = *m
	_, err = gv.NewVersion(manifest.Version)
	if err != nil {
		return fmt.Errorf("invalid hz version in \".hz\", err: %v", err)
	}

	return nil
}

const hzTitle = "// Code generated by hz. DO NOT EDIT."

func (manifest *Manifest) String() string {
	conf, _ := yaml.Marshal(*manifest)

	return hzTitle + "\n\n" +
		string(conf)
}

func (manifest *Manifest) Persist(dir string) error {
	file := filepath.Join(dir, ManifestFile)
	fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0o644))
	if err != nil {
		return err
	}
	defer fd.Close()
	_, err = fd.WriteString(manifest.String())
	return err
}

// loadConfigFile load config file from path
func loadConfigFile(path string) (*Manifest, error) {
	file, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}
	var manifest Manifest
	file = bytes.TrimPrefix(file, []byte(hzTitle))
	if err = yaml.Unmarshal(file, &manifest); err != nil {
		return nil, fmt.Errorf("decode \".hz\" failed, err: %v", err)
	}
	return &manifest, nil
}
