package model

import (
	"time"

	"github.com/tech/sendico/pkg/db/storable"
	"github.com/tech/sendico/pkg/mservice"
	"go.mongodb.org/mongo-driver/v2/bson"
)

type InvitationStatus string

const (
	InvitationCreated  InvitationStatus = "created"
	InvitationSent     InvitationStatus = "sent"
	InvitationAccepted InvitationStatus = "accepted"
	InvitationDeclined InvitationStatus = "declined"
	InvitationRevoked  InvitationStatus = "revoked"
)

type invitationDesc struct {
	Email    string `bson:"email" json:"email"`
	Name     string `bson:"name" json:"name"`
	LastName string `bson:"lastName" json:"lastName"`
	Comment  string `bson:"comment" json:"comment"`
}

func (id *invitationDesc) Collection() string {
	return mservice.Invitations
}

type Invitation struct {
	PermissionBound `bson:",inline" json:",inline"`
	OrganizationRef bson.ObjectID    `bson:"organizationRef" json:"organizationRef"`
	RoleRef         bson.ObjectID    `bson:"roleRef" json:"roleRef"`
	InviterRef      bson.ObjectID    `bson:"inviterRef" json:"inviterRef"`
	Status          InvitationStatus `bson:"status" json:"status"`
	ExpiresAt       time.Time        `bson:"expiresAt"  json:"expiresAt"`
	Content         invitationDesc   `bson:"description" json:"description"`
}

func (*Invitation) Collection() string {
	return mservice.Invitations
}

type employeeDesc struct {
	Description Describable `bson:"description" json:"description"`
	AvatarURL   *string     `bson:"avatarUrl,omitempty" json:"avatarUrl,omitempty"`
}

type organizationDesc struct {
	Description Describable `bson:"description" json:"description"`
	LogoURL     *string     `bson:"logoUrl,omitempty" json:"logoUrl,omitempty"`
}

type invitationStorable struct {
	storable.Base `bson:",inline" json:",inline"`
	ExpiresAt     time.Time `bson:"expiresAt"  json:"expiresAt"`
}

type PublicInvitation struct {
	Storable             invitationStorable `bson:"storable" json:"storable"`
	Employee             employeeDesc       `bson:"employee" json:"employee"`
	Organization         organizationDesc   `bson:"organization" json:"organization"`
	Role                 Describable        `bson:"role" json:"role"`
	Invitation           invitationDesc     `bson:"invitation" json:"invitation"`
	RequiresRegistration bool               `bson:"registrationRequired" json:"registrationRequired"`
}

func (pi *PublicInvitation) Collection() string {
	return mservice.Invitations
}
