package feed

import (
	"database/sql"
	"time"
)

type (
	EventType  string
	UserRole   string
	Visibility string
)

const (
	// User Roles
	RoleAgent       UserRole = "agent"
	RoleDistributor UserRole = "distributor"

	// Visibility Types
	VisibilityPublic      Visibility = "public"       // Показывается всем агентам(инициатор может быть только дистр)
	VisibilityPrivate     Visibility = "private"      // Показывается только инициатору события
	VisibilityCompanyWide Visibility = "company_wide" // Показывается всем в компании

	// Event Types (Common)
	EventWelcome          EventType = "welcome"
	EventNewCompanyMember EventType = "new_company_member"

	// Users Events
	EventProfileChanged          EventType = "profile_changed"
	EventCompanyCreated          EventType = "company_created"
	EventCompanyChanged          EventType = "company_changed"
	EventVacancyCreated          EventType = "vacancy_created"
	EventVacancyChanged          EventType = "vacancy_changed"
	EventVacancyModerationSent   EventType = "vacancy_moderation_sent"
	EventSubmissionStatusChanged EventType = "submission_status_changed"
	EventTransactionCreated      EventType = "transaction_created"
	EventBankAccountChanged      EventType = "bank_details_changed"
	EventBankAccountCreated      EventType = "bank_account_created"
	EventPostAnketa              EventType = "post_anketa"
)

type Event struct {
	Id                 string       `json:"id" db:"id"`
	OwnerId            string       `json:"owner_id" db:"owner_id"`
	OwnerType          UserRole     `json:"owner_type" db:"owner_type"`
	Message            string       `json:"message" db:"message"`
	Visibility         Visibility   `json:"visibility" db:"visibility"`
	CompanyID          *string      `json:"company_id,omitempty"`
	EventType          EventType    `json:"event_type" db:"event_type"`
	Payload            EventPayload `json:"payload" db:"payload"`
	IsCancelled        bool         `json:"is_cancelled" db:"is_cancelled"`
	CancellationReason *string      `json:"cancellation_reason" db:"cancellation_reason"`
	CreatedAt          time.Time    `json:"created_at" db:"created_at"`
	UpdatedAt          time.Time    `json:"updated_at" db:"updated_at"`
}

func (e EventType) String() string {
	return string(e)
}

func (u UserRole) String() string {
	return string(u)
}

func (e *Event) processNullableFields(companyID, cancellationReason sql.NullString) {
	if companyID.Valid {
		e.CompanyID = &companyID.String
	}

	if cancellationReason.Valid {
		e.CancellationReason = &cancellationReason.String
	}
}

type EventPayload struct {
	AttachmentId       string         `json:"attachment_id" db:"attachment_id"`
	AttachmentType     AttachmentType `json:"attachment_type" db:"attachment_type"`
	AdditionalReceiver string         `json:"additional_receiver,omitempty" db:"additional_receiver"` // may be uid or company id
	CustomData         string         `json:"custom_data,omitempty"`
}

type AttachmentType string

const (
	AttachmentTypeVacancy     AttachmentType = "vacancy"
	AttachmentTypeCV          AttachmentType = "cv"
	AttachmentTypeProfile     AttachmentType = "profile"
	AttachmentTypeCompany     AttachmentType = "company"
	AttachmentTypeBankAccount AttachmentType = "bank_account"
	AttachmentTypeSubmission  AttachmentType = "submission"
)
