package domain

import (
	"context"
	"time"
)

// Repository — members + multi-platform identities + session helpers.
type Repository interface {
	NextUID(ctx context.Context) (int64, error)

	// Member profile
	CreateMember(ctx context.Context, m *Member) error
	FindByUID(ctx context.Context, uid int64) (*Member, error)
	FindByEmail(ctx context.Context, email string) (*Member, error)
	FindByPhone(ctx context.Context, phone string) (*Member, error)
	FindByInviteCode(ctx context.Context, code string) (*Member, error)
	UpdateMember(ctx context.Context, m *Member) error
	ListPage(ctx context.Context, page, pageSize int, query string, status string) (list []*Member, total int64, err error)
	// ListAllMembers — 邀請樹用（全量；小租戶）
	ListAllMembers(ctx context.Context) ([]*Member, error)
	CountActiveAdmins(ctx context.Context) (int64, error)

	// Identities (login channels)
	CreateIdentity(ctx context.Context, id *Identity) error
	FindIdentity(ctx context.Context, loginID, platform string) (*Identity, error)
	ListIdentitiesByUID(ctx context.Context, uid int64) ([]*Identity, error)
	DeleteIdentity(ctx context.Context, loginID, platform string) error
	UpdateIdentityPassword(ctx context.Context, loginID, platform, passwordHash string) error

	// Codes / refresh (Mongo durable)
	// SetCode returns false when a recent code exists or persistence fails.
	SetCode(kind, key, code string, ttl time.Duration) bool
	// 驗證碼共用語意（信箱驗證 / 重設密碼相同）：
	// CheckCode：碼正確才刪（成功消費）；錯誤不刪；過期才刪
	// PeekCode：只檢查不消費（業務前先 Peek，成功後再 Check）
	CheckCode(kind, key, code string) bool
	PeekCode(kind, key, code string) bool
	SaveRefresh(jti string, uid int64, exp time.Time)
	ConsumeRefresh(jti string) (uid int64, ok bool)
	RevokeRefresh(jti string)
	RevokeAllRefreshByUID(ctx context.Context, uid int64) error

	GetSettings(ctx context.Context, uid int64) (*UserSettings, error)
	SaveSettings(ctx context.Context, uid int64, s *UserSettings) error
}
