package model

import (
	"golang.org/x/oauth2"
	"time"
)

type Amo struct {
	ID           string         `bson:"_id,omitempty" json:"id"`
	UserID       string         `bson:"user_id" json:"user_id"`
	AccountID    string         `bson:"account_id" json:"account_id"` // Account ID in AMO CRM
	Subdomain    string         `bson:"subdomain" json:"subdomain"`   // Subdomain in AMO CRM. Example: [subdomain].amocrm.ru/
	Referer      string         `bson:"referer" json:"referer"`       // Referer in AMO CRM. Example: subdomain.amocrm.ru
	FromWidget   string         `bson:"from_widget" json:"from_widget"`
	AccessToken  string         `bson:"access_token" json:"-"`
	RefreshToken string         `bson:"refresh_token" json:"-"`
	ExpiresIn    time.Time      `bson:"expires_in" json:"-"`
	TokenType    string         `bson:"token_type" json:"-"`
	AccessRules  AmoAccessRules `bson:"access_rules" json:"access_rules"`
	IsDeleted    bool           `bson:"is_deleted" json:"is_deleted"`
	CreatedAt    time.Time      `bson:"created_at" json:"created_at"`
	UpdatedAt    time.Time      `bson:"updated_at" json:"updated_at"`
}

type AmoAccessRules struct {
	Visibility []int64 `bson:"visibility"`
	Creation   []int64 `bson:"creation"`
	Delete     []int64 `bson:"delete"`
}

func (m *Amo) Token() *oauth2.Token {
	token := &oauth2.Token{
		AccessToken:  m.AccessToken,
		TokenType:    m.TokenType,
		RefreshToken: m.RefreshToken,
		Expiry:       m.ExpiresIn,
	}

	return token
}
