// Package app defines all app models
package app

import (
	"github.com/shopspring/decimal"

	"quantex.com.ar/multidb"
	notifyall "quantex.com/qfixpt/src/client/notify/all"
)

//revive:disable:max-public-structs // This is a file containing app public models

type Config struct {
	Global
	Service
}

type Global struct {
	GitUser            string
	GitPass            string
	CertEncryptionKey  string
	Async              Async `toml:"MQTT"`
	MultiDB            multidb.Config
	AllowedOrigins     []string
	ExchangeRateAPIKey string
	RatingStartDate    string
	ScreenshotFolder   string
	QApixPort          string
	QApixHost          string // Optional
	Notify             notifyall.Config
}

type Service struct {
	QApixToken         string                       // TODO find a better way to authenticate
	External           map[string]ExtAuth           `toml:"External"`
	AuthorizedServices map[string]AuthorizedService `toml:"AuthorizedServices"`
	APIBasePort        string
	EnableJWTAuth      bool // Enable JWT authentication for service-to-service communication
	FIX                FIXConfig
}

type FIXConfig struct {
	SettingsFile string // path to fix_pt.cfg file
}

type ExtAuth struct {
	Host  string
	Port  string
	Name  string
	Token string
}

type AuthorizedService struct {
	Name        string
	Permissions []ServicePermission
	Token       *string
}

func (s *AuthorizedService) HasPermissions(requiredPerm ServicePermission) bool {
	for _, perm := range s.Permissions {
		if perm == requiredPerm || perm == ServicePermissionFullAccess {
			return true
		}
	}

	return false
}

type Async struct {
	Protocol  string
	URL       string
	Subdomain string
	Secret    string
}

type TargetParty struct {
	Party    Entity
	Favorite bool
	Selected bool
}

type Entity struct {
	ID   string
	Name string
}

type InstID string

type User struct {
	UserID         string
	Token          string
	Name           string
	LastName       string
	Password       string
	Email          string
	Phone          string
	Sender         Entity
	Party          Entity
	BannedParties  []Entity
	TargetParties  []TargetParty
	Subscriptions  []InstID
	IsMiddleman    bool
	IsSuperUser    bool
	IsBackOffice   bool
	IsTrader       bool
	IsService      bool
	IsPartyAdmin   bool
	IsViewer       bool
	IsTesting      bool
	AllowTokenAuth bool
	Rating         decimal.Decimal
}

type UserDataProvider interface {
	GetUserByEmail(email string) User
}

//go:generate go-enum -f=$GOFILE --lower --marshal

// Service Permissions
// ENUM(
// ReadOnly
// ReadWrite
// FullAccess
// Undefined
// )
type ServicePermission int //nolint:recvcheck // The methods of this are autogenerated
