package common

import (
	"fmt"
	"strings"
	"time"

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

type SupplierAccount struct {
	SupplierOrganizationID int64      `json:"supplier_organization_id,string,omitempty" pg:"supplier_organization_id"`
	Email                  string     `json:"email" pg:",pk" validate:"required,email,max=1000"`
	Company                string     `json:"company" validate:"required,max=100"`
	Address                string     `json:"address" validate:"required,max=100"`
	Contact                string     `json:"contact" validate:"required,max=50"`
	Telephone              string     `json:"telephone" validate:"required,max=20"`
	Program                string     `json:"program" validate:"required,max=100"`
	ECUs                   []string   `json:"ecus" pg:"ecus,array" validate:"required,min=1,max=100,dive,min=2,max=10"`
	IP                     string     `json:"ip,omitempty" validate:"max=50" swaggerignore:"true"`
	ActivatedAt            *time.Time `json:"activated,omitempty" swaggerignore:"true"`
	SigninAt               *time.Time `json:"signin_at,omitempty" swaggerignore:"true"`
	KeysAt                 *time.Time `json:"keys_at,omitempty" swaggerignore:"true"`
	dbbasemodel.DBModelBase
}

func (s *SupplierAccount) SanitizeData() {
	s.Email = strings.ToLower(strings.TrimSpace(s.Email))
	s.Company = strings.TrimSpace(s.Company)
	s.Address = strings.TrimSpace(s.Address)
	s.Contact = strings.TrimSpace(s.Contact)
	s.Telephone = strings.TrimSpace(s.Telephone)
	s.Program = strings.TrimSpace(s.Program)
	s.CreatedAt = nil
	s.UpdatedAt = nil
	s.ActivatedAt = nil
	s.SigninAt = nil
	s.KeysAt = nil

	s.SanitizeECUData()
}

func (s *SupplierAccount) SanitizeECUData() {
	for i, item := range s.ECUs {
		s.ECUs[i] = strings.TrimSpace(item)
	}
}

func (s *SupplierAccount) String() string {
	return fmt.Sprintf("SupplierAccount<%s, %s, %s, %s, %s, %s, %v, %v, %v, %v, %v>",
		s.Company,
		s.Address,
		s.Contact,
		s.Telephone,
		s.Email,
		s.Program,
		s.CreatedAt,
		s.UpdatedAt,
		s.ActivatedAt,
		s.SigninAt,
		s.KeysAt,
	)
}
