package queries

import (
	"fmt"

	"github.com/fiskerinc/cloud-services/pkg/common"

	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"github.com/pkg/errors"
)

type SupplierTimestamp string

const (
	SupplierTimestampActivated SupplierTimestamp = "activated_at"
	SupplierTimestampSignIn    SupplierTimestamp = "signin_at"
	SupplierTimestampKeys      SupplierTimestamp = "keys_at"
)

type SupplierAccountsInterface interface {
	Count(account *common.SupplierAccount) (int, error)
	Delete(account *common.SupplierAccount) (orm.Result, error)
	Insert(account *common.SupplierAccount) (orm.Result, error)
	Load(account *common.SupplierAccount) error
	Select(account *common.SupplierAccount, paging *PageQueryOptions) ([]common.SupplierAccount, error)
	Update(account *common.SupplierAccount) (orm.Result, error)
	Approve(email string) (orm.Result, error)
	UpdateTimestamp(email string, activity SupplierTimestamp) (orm.Result, error)
}

type SupplierAccounts struct {
	QueryBase
}

func (sf *SupplierAccounts) Count(account *common.SupplierAccount) (int, error) {
	return sf.count(account)
}

func (sf *SupplierAccounts) Delete(account *common.SupplierAccount) (orm.Result, error) {
	return sf.delete(account)
}

func (sf *SupplierAccounts) Insert(account *common.SupplierAccount) (orm.Result, error) {
	return sf.insert(account)
}

func (sf *SupplierAccounts) Load(account *common.SupplierAccount) error {
	query := sf.GetDBConn().Model(account)

	if account.Email != "" {
		query.Where("email = ?", account.Email)
	} else if account.Telephone != "" {
		query.Where("telephone = ?", account.Telephone)
	} else {
		return errors.New("requires email or telephone")
	}

	err := query.Select()

	return errors.WithStack(err)
}

func (sf *SupplierAccounts) Select(filter *common.SupplierAccount, paging *PageQueryOptions) ([]common.SupplierAccount, error) {
	accounts := []common.SupplierAccount{}
	query := sf.GetDBConn().Model(&accounts)

	sf.selectFilter(query, filter)
	sf.pageQuery(query, paging)

	err := query.Select()

	return accounts, err
}

func (sf *SupplierAccounts) selectFilter(query *orm.Query, filter *common.SupplierAccount) {
	if filter.Email != "" {
		query.Where("email = ?", filter.Email)
	}

	if filter.Telephone != "" {
		query.Where("telephone = ?", filter.Telephone)
	}
}

func (sf *SupplierAccounts) Update(account *common.SupplierAccount) (orm.Result, error) {
	return sf.GetDBConn().Model(account).Column("supplier_organization_id", "contact", "company", "address", "telephone", "program", "ecus").Where("email = ?", account.Email).Update()
}

func (sf *SupplierAccounts) Approve(email string) (orm.Result, error) {
	return sf.UpdateTimestamp(email, SupplierTimestampActivated)
}

func (sf *SupplierAccounts) UpdateTimestamp(email string, activity SupplierTimestamp) (orm.Result, error) {
	result, err := sf.GetDBConn().Model(&common.SupplierAccount{}).Set(fmt.Sprintf("%s = CURRENT_TIMESTAMP", string(activity))).Where("email = ?", email).Update()
	if err != nil {
		return nil, errors.WithStack(err)
	}
	if result.RowsAffected() == 0 {
		return nil, errors.WithStack(pg.ErrNoRows)
	}

	return result, nil
}
