package bot

import (
	"strings"
	"sync"

	storagemodel "github.com/tech/sendico/gateway/chsettle/storage/model"
)

type DialogState string

const (
	DialogStateWaitingAmount       DialogState = "waiting_amount"
	DialogStateWaitingConfirmation DialogState = "waiting_confirmation"
)

type DialogSession struct {
	State           DialogState
	OperationType   storagemodel.TreasuryOperationType
	LedgerAccountID string
	RequestID       string
}

type Dialogs struct {
	mu       sync.Mutex
	sessions map[string]DialogSession
}

func NewDialogs() *Dialogs {
	return &Dialogs{
		sessions: map[string]DialogSession{},
	}
}

func (d *Dialogs) Get(telegramUserID string) (DialogSession, bool) {
	if d == nil {
		return DialogSession{}, false
	}
	telegramUserID = strings.TrimSpace(telegramUserID)
	if telegramUserID == "" {
		return DialogSession{}, false
	}
	d.mu.Lock()
	defer d.mu.Unlock()
	session, ok := d.sessions[telegramUserID]
	return session, ok
}

func (d *Dialogs) Set(telegramUserID string, session DialogSession) {
	if d == nil {
		return
	}
	telegramUserID = strings.TrimSpace(telegramUserID)
	if telegramUserID == "" {
		return
	}
	d.mu.Lock()
	defer d.mu.Unlock()
	d.sessions[telegramUserID] = session
}

func (d *Dialogs) Clear(telegramUserID string) {
	if d == nil {
		return
	}
	telegramUserID = strings.TrimSpace(telegramUserID)
	if telegramUserID == "" {
		return
	}
	d.mu.Lock()
	defer d.mu.Unlock()
	delete(d.sessions, telegramUserID)
}
