package usecase

import (
	"context"

	"haixun-backend/internal/library/placement"
)

type AccountSummary struct {
	ID               string
	DisplayName      string
	Username         string
	ThreadsUserID    string
	PersonaID        string
	BrowserConnected bool
	ApiConnected     bool
	Status           string
	CreateAt         int64
	UpdateAt         int64
}

type ConnectionPrefs struct {
	SearchViaApi     bool
	SearchSourceMode string
	PublishViaApi    bool
	DevMode          bool
	ScrapeReplies    bool
	RepliesPerPost   int
	PublishHeaded    bool
	PlaywrightDebug  bool
}

type ConnectionData struct {
	AccountID        string
	AccountName      string
	Username         string
	BrowserConnected bool
	ApiConnected     bool
	Prefs            ConnectionPrefs
}

type CreateRequest struct {
	TenantID    string
	OwnerUID    string
	DisplayName string
	Activate    bool
}

type UpdateAccountRequest struct {
	TenantID    string
	OwnerUID    string
	AccountID   string
	DisplayName *string
	PersonaID   *string
}

type UpdateConnectionRequest struct {
	TenantID  string
	OwnerUID  string
	AccountID string
	Prefs     ConnectionPrefsPatch
}

type ImportBrowserSessionRequest struct {
	TenantID     string
	OwnerUID     string
	AccountID    string
	StorageState string
}

type ImportBrowserSessionResult struct {
	AccountID string
	Username  string
	Synced    bool
	Valid     bool
	Message   string
	UpdateAt  int64
}

type BrowserSessionData struct {
	AccountID    string
	StorageState string
	UpdateAt     int64
}

type AiSettings struct {
	AccountID         string
	Provider          string
	Model             string
	ResearchProvider  string
	ResearchModel     string
	ApiKeys           map[string]string
	ApiKeysConfigured map[string]bool
}

type AiSettingsPatch struct {
	Provider         *string
	Model            *string
	ResearchProvider *string
	ResearchModel    *string
	ApiKeys          map[string]string
}

// WorkerAiCredential carries raw provider settings for internal workers (never expose to clients).
type WorkerAiCredential struct {
	Provider string
	Model    string
	APIKey   string
}

type ThreadsPublishCredential struct {
	AccountID     string
	ThreadsUserID string
	AccessToken   string
}

type ConnectionPrefsPatch struct {
	SearchViaApi     *bool
	SearchSourceMode *string
	PublishViaApi    *bool
	DevMode          *bool
	ScrapeReplies    *bool
	RepliesPerPost   *int
	PublishHeaded    *bool
	PlaywrightDebug  *bool
}

type ListResult struct {
	List            []AccountSummary
	ActiveAccountID string
}

type UseCase interface {
	List(ctx context.Context, tenantID, ownerUID string) (*ListResult, error)
	Create(ctx context.Context, req CreateRequest) (*AccountSummary, error)
	Get(ctx context.Context, tenantID, ownerUID, accountID string) (*AccountSummary, error)
	Update(ctx context.Context, req UpdateAccountRequest) (*AccountSummary, error)
	Activate(ctx context.Context, tenantID, ownerUID, accountID string) error
	GetConnection(ctx context.Context, tenantID, ownerUID, accountID string) (*ConnectionData, error)
	UpdateConnection(ctx context.Context, req UpdateConnectionRequest) (*ConnectionData, error)
	ImportBrowserSession(ctx context.Context, req ImportBrowserSessionRequest) (*ImportBrowserSessionResult, error)
	GetBrowserSession(ctx context.Context, tenantID, ownerUID, accountID string) (*BrowserSessionData, error)
	GetAiSettings(ctx context.Context, tenantID, ownerUID, accountID string) (*AiSettings, error)
	UpdateAiSettings(ctx context.Context, tenantID, ownerUID, accountID string, patch AiSettingsPatch) (*AiSettings, error)
	ResolveWorkerAiCredential(ctx context.Context, tenantID, ownerUID, accountID string) (*WorkerAiCredential, error)
	ResolveMemberAiCredential(ctx context.Context, tenantID, ownerUID string) (*WorkerAiCredential, error)
	ResolveMemberPlacementContext(ctx context.Context, tenantID, ownerUID string, research placement.ResearchSettings) (placement.MemberContext, error)
	ResolveMemberThreadsPublishCredential(ctx context.Context, tenantID, ownerUID string) (*ThreadsPublishCredential, error)
}
