// Copyright (c) 2020 Mercari, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package cmd

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/cloudspannerecosystem/wrench/internal/fs"
	"github.com/cloudspannerecosystem/wrench/pkg/spanner"
	"github.com/spf13/cobra"
)

const (
	migrationsDirName  = "migrations"
	migrationTableName = "SchemaMigrations"
)

// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
	Use:   "migrate",
	Short: "Migrate database",
}

func init() {
	migrateCreateCmd := &cobra.Command{
		Use:   "create NAME",
		Short: "Create a set of sequential up migrations in directory",
		RunE:  migrateCreate,
	}
	migrateUpCmd := &cobra.Command{
		Use:   "up [N]",
		Short: "Apply all or N up migrations",
		RunE:  migrateUp,
	}
	migrateVersionCmd := &cobra.Command{
		Use:   "version",
		Short: "Print current migration version",
		RunE:  migrateVersion,
	}
	migrateSetCmd := &cobra.Command{
		Use:   "set V",
		Short: "Set version V but don't run migration (ignores dirty state)",
		RunE:  migrateSet,
	}

	migrateUpCmd.Flags().String(flagProtoDescriptorFile, "", "Proto descriptor file to be used with migrations")

	migrateCmd.AddCommand(
		migrateCreateCmd,
		migrateUpCmd,
		migrateVersionCmd,
		migrateSetCmd,
	)

	migrateCmd.PersistentFlags().String(flagNameDirectory, "", "Directory that migration files placed (required)")

	migrateUpCmd.PersistentFlags().StringVar(&priority, flagPriority, "", "The priority to apply DML (optional)")
}

func migrateCreate(c *cobra.Command, args []string) error {
	name := ""

	if len(args) > 0 {
		name = args[0]
	}

	dir := filepath.Join(c.Flag(flagNameDirectory).Value.String(), migrationsDirName)

	if _, err := os.Stat(dir); os.IsNotExist(err) {
		if err := os.Mkdir(dir, os.ModePerm); err != nil {
			return &Error{
				cmd: c,
				err: err,
			}
		}
	}

	filename, err := createMigrationFile(c.Context(), dir, name, 6)
	if err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	fmt.Printf("%s is created\n", filename)

	return nil
}

func migrateUp(c *cobra.Command, args []string) error {
	ctx, cancel := context.WithTimeout(c.Context(), timeout)
	defer cancel()

	limit := -1
	if len(args) > 0 {
		n, err := strconv.Atoi(args[0])
		if err != nil {
			return &Error{
				cmd: c,
				err: err,
			}
		}
		limit = n
	}

	priorityType, err := priorityTypeOf(priority)
	if err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	client, err := newSpannerClient(ctx, c)
	if err != nil {
		return err
	}
	defer client.Close()

	if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	dir := filepath.Join(c.Flag(flagNameDirectory).Value.String(), migrationsDirName)
	migrations, err := spanner.ReadMigrations(ctx, dir)
	if err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	var protoDescriptor []byte
	protoDescriptorFile := protoDescriptorFilePath(c)
	if protoDescriptorFile != "" {
		protoDescriptor, err = fs.ReadFile(ctx, protoDescriptorFile)
		if err != nil {
			return &Error{
				err: err,
				cmd: c,
			}
		}
	}

	return client.ExecuteMigrations(ctx, migrations, limit, migrationTableName, priorityType, protoDescriptor)
}

func migrateVersion(c *cobra.Command, _ []string) error {
	ctx, cancel := context.WithTimeout(c.Context(), timeout)
	defer cancel()

	client, err := newSpannerClient(ctx, c)
	if err != nil {
		return err
	}
	defer client.Close()

	if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	v, _, err := client.GetSchemaMigrationVersion(ctx, migrationTableName)
	if err != nil {
		var se *spanner.Error
		if errors.As(err, &se) && se.Code == spanner.ErrorCodeNoMigration {
			fmt.Println("No migrations.")
			return nil
		}
		return &Error{
			cmd: c,
			err: err,
		}
	}

	fmt.Println(v)

	return nil
}

func migrateSet(c *cobra.Command, args []string) error {
	ctx, cancel := context.WithTimeout(c.Context(), timeout)
	defer cancel()

	if len(args) == 0 {
		return &Error{
			cmd: c,
			err: errors.New("Parameters are not passed."),
		}
	}
	version, err := strconv.Atoi(args[0])
	if err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	client, err := newSpannerClient(ctx, c)
	if err != nil {
		return err
	}
	defer client.Close()

	if err = client.EnsureMigrationTable(ctx, migrationTableName); err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	if err := client.SetSchemaMigrationVersion(ctx, uint(version), false, migrationTableName); err != nil {
		return &Error{
			cmd: c,
			err: err,
		}
	}

	return nil
}

func createMigrationFile(ctx context.Context, dir string, name string, digits int) (string, error) {
	if name != "" && !spanner.MigrationNameRegex.MatchString(name) {
		return "", errors.New("Invalid migration file name.")
	}

	ms, err := spanner.ReadMigrations(ctx, dir)
	if err != nil {
		return "", err
	}

	var v uint = 1
	if len(ms) > 0 {
		v = ms[len(ms)-1].Version + 1
	}
	vStr := fmt.Sprint(v)

	padding := digits - len(vStr)
	if padding > 0 {
		vStr = strings.Repeat("0", padding) + vStr
	}

	var filename string
	if name == "" {
		filename = filepath.Join(dir, fmt.Sprintf("%s.sql", vStr))
	} else {
		filename = filepath.Join(dir, fmt.Sprintf("%s_%s.sql", vStr, name))
	}

	fp, err := os.Create(filename)
	if err != nil {
		return "", err
	}
	fp.Close()

	return filename, nil
}
