package domain

import "time"

type TicketingType string

const (
	TicketingJira     TicketingType = "jira"
	TicketingAzure    TicketingType = "azure_devops"
	TicketingYouTrack TicketingType = "youtrack"
	TicketingWekan    TicketingType = "wekan"
	// TicketingDemo fabricates tickets locally so the whole flow works
	// offline (§11.11).
	TicketingDemo TicketingType = "demo"
)

func (t TicketingType) Valid() bool {
	switch t {
	case TicketingJira, TicketingAzure, TicketingYouTrack, TicketingWekan, TicketingDemo:
		return true
	}
	return false
}

type Ticketing struct {
	Type            TicketingType `bson:"type" json:"type"`
	BaseURL         string        `bson:"baseUrl" json:"baseUrl"`
	CredentialsEnc  string        `bson:"credentialsEnc" json:"-"`
	ProjectKey      string        `bson:"projectKey" json:"projectKey"`
	PollIntervalSec int           `bson:"pollIntervalSec" json:"pollIntervalSec"`
	// ConsultantIdentities maps a bounty-board consultant id to that
	// consultant's username in THIS customer's ticketing system. It takes
	// precedence over the consultant's global extra.ticketingIdentities so an
	// admin can map accounts per project (§5.3).
	ConsultantIdentities map[string]string `bson:"consultantIdentities,omitempty" json:"consultantIdentities,omitempty"`
	LastSyncAt           time.Time         `bson:"lastSyncAt" json:"lastSyncAt"`
	LastSyncStatus       string            `bson:"lastSyncStatus" json:"lastSyncStatus"` // "", ok, error
	LastSyncError        string            `bson:"lastSyncError" json:"lastSyncError"`
}

// Customer == "project" in the UI (§4.2).
type Customer struct {
	ID            string    `bson:"_id" json:"id"`
	Name          string    `bson:"name" json:"name"`
	Ticketing     Ticketing `bson:"ticketing" json:"ticketing"`
	ConsultantIDs []string  `bson:"consultantIds" json:"consultantIds"`
	DefaultBudget float64   `bson:"defaultBudget" json:"defaultBudget"`
	Archived      bool      `bson:"archived" json:"archived"`
	CreatedAt     time.Time `bson:"createdAt" json:"createdAt"`
	UpdatedAt     time.Time `bson:"updatedAt" json:"updatedAt"`
	Version       int64     `bson:"version" json:"version"`
}

// HasConsultant reports membership in the customer's consultant set.
func (c *Customer) HasConsultant(userID string) bool {
	for _, id := range c.ConsultantIDs {
		if id == userID {
			return true
		}
	}
	return false
}
