syntax = "v1"

type (
	ThreadsAccountData {
		ID               string `json:"id"`
		DisplayName      string `json:"display_name,omitempty"`
		Username         string `json:"username,omitempty"`
		ThreadsUserID    string `json:"threads_user_id,omitempty"`
		PersonaID        string `json:"persona_id,omitempty"` // deprecated: persona is chosen per publish, not bound to account
		BrowserConnected bool   `json:"browser_connected"`
		ApiConnected     bool   `json:"api_connected"`
		Status           string `json:"status"`
		CreateAt         int64  `json:"create_at"`
		UpdateAt         int64  `json:"update_at"`
	}

	ListThreadsAccountsData {
		List            []ThreadsAccountData `json:"list"`
		ActiveAccountID string               `json:"active_account_id"`
	}

	CreateThreadsAccountReq {
		DisplayName string `json:"display_name,optional"`
		Activate    *bool  `json:"activate,optional"`
	}

	UpdateThreadsAccountReq {
		DisplayName *string `json:"display_name,optional"`
		PersonaID   *string `json:"persona_id,optional"` // deprecated: use persona_id in publish payload instead
	}

	ThreadsAccountPath {
		ID string `path:"id" validate:"required"`
	}

	ThreadsAccountConnectionPrefs {
		SearchViaApi     bool   `json:"search_via_api"`
		SearchSourceMode string `json:"search_source_mode"`
		PublishViaApi    bool   `json:"publish_via_api"`
		DevMode          bool   `json:"dev_mode"`
		ScrapeReplies    bool   `json:"scrape_replies"`
		RepliesPerPost   int    `json:"replies_per_post"`
		PublishHeaded    bool   `json:"publish_headed"`
		PlaywrightDebug  bool   `json:"playwright_debug"`
	}

	ThreadsAccountConnectionData {
		AccountID        string                        `json:"account_id"`
		AccountName      string                        `json:"account_name"`
		Username         string                        `json:"username,omitempty"`
		BrowserConnected bool                          `json:"browser_connected"`
		ApiConnected     bool                          `json:"api_connected"`
		Prefs            ThreadsAccountConnectionPrefs `json:"prefs"`
	}

	UpdateThreadsAccountConnectionReq {
		SearchViaApi     *bool   `json:"search_via_api,optional"`
		SearchSourceMode *string `json:"search_source_mode,optional"`
		PublishViaApi    *bool   `json:"publish_via_api,optional"`
		DevMode          *bool   `json:"dev_mode,optional"`
		ScrapeReplies    *bool   `json:"scrape_replies,optional"`
		RepliesPerPost   *int    `json:"replies_per_post,optional"`
		PublishHeaded    *bool   `json:"publish_headed,optional"`
		PlaywrightDebug  *bool   `json:"playwright_debug,optional"`
	}

	ImportThreadsAccountSessionReq {
		StorageState string `json:"storageState" validate:"required"`
	}

	ImportThreadsAccountSessionData {
		Success   bool   `json:"success"`
		Valid     bool   `json:"valid"`
		Synced    bool   `json:"synced"`
		AccountID string `json:"account_id"`
		Username  string `json:"username,omitempty"`
		Message   string `json:"message"`
		UpdateAt  int64  `json:"update_at"`
	}

	ThreadsAccountAiSettingsData {
		AccountID         string            `json:"account_id"`
		Provider          string            `json:"provider"`
		Model             string            `json:"model"`
		ResearchProvider  string            `json:"research_provider,omitempty"`
		ResearchModel     string            `json:"research_model,omitempty"`
		ApiKeys           map[string]string `json:"api_keys"`
		ApiKeysConfigured map[string]interface{} `json:"api_keys_configured"`
	}

	UpdateThreadsAccountAiSettingsReq {
		Provider         *string           `json:"provider,optional"`
		Model            *string           `json:"model,optional"`
		ResearchProvider *string           `json:"research_provider,optional"`
		ResearchModel    *string           `json:"research_model,optional"`
		ApiKeys          map[string]string `json:"api_keys,optional"`
	}

	UpdateThreadsAccountHandlerReq {
		ThreadsAccountPath
		UpdateThreadsAccountReq
	}

	UpdateThreadsAccountConnectionHandlerReq {
		ThreadsAccountPath
		UpdateThreadsAccountConnectionReq
	}

	ImportThreadsAccountSessionHandlerReq {
		ThreadsAccountPath
		ImportThreadsAccountSessionReq
	}

	UpdateThreadsAccountAiSettingsHandlerReq {
		ThreadsAccountPath
		UpdateThreadsAccountAiSettingsReq
	}

	ThreadsAccountAiProviderPath {
		ID       string `path:"id" validate:"required"`
		Provider string `path:"provider" validate:"required,oneof=opencode-go xai"`
	}

	ListThreadsAccountAiProviderModelsReq {
		ApiKey string `json:"api_key,optional"` // 選填；未帶則用已儲存的 key
	}

	ThreadsAccountAiProviderModelsHandlerReq {
		ThreadsAccountAiProviderPath
		ListThreadsAccountAiProviderModelsReq
	}

	ThreadsAccountAiProviderModelsData {
		ID      string   `json:"id"`
		Label   string   `json:"label"`
		Models  []string `json:"models"`
		Streams bool     `json:"streams"`
		Error   string   `json:"error,optional"`
	}
)

@server(
	group:      threads_account
	prefix:     /api/v1/threads-accounts
	middleware: AuthJWT
	tags:       "ThreadsAccount"
	summary:    "Threads operating account endpoints. Requires Bearer JWT."
)
service gateway {
	@handler listThreadsAccounts
	get / returns (ListThreadsAccountsData)

	@handler createThreadsAccount
	post / (CreateThreadsAccountReq) returns (ThreadsAccountData)

	@handler getThreadsAccount
	get /:id (ThreadsAccountPath) returns (ThreadsAccountData)

	@handler updateThreadsAccount
	patch /:id (UpdateThreadsAccountHandlerReq) returns (ThreadsAccountData)

	@handler activateThreadsAccount
	post /:id/activate (ThreadsAccountPath)

	@handler getThreadsAccountConnection
	get /:id/connection (ThreadsAccountPath) returns (ThreadsAccountConnectionData)

	@handler updateThreadsAccountConnection
	patch /:id/connection (UpdateThreadsAccountConnectionHandlerReq) returns (ThreadsAccountConnectionData)

	@handler importThreadsAccountSession
	post /:id/session/import (ImportThreadsAccountSessionHandlerReq) returns (ImportThreadsAccountSessionData)

	@handler getThreadsAccountAiSettings
	get /:id/ai-settings (ThreadsAccountPath) returns (ThreadsAccountAiSettingsData)

	@handler updateThreadsAccountAiSettings
	put /:id/ai-settings (UpdateThreadsAccountAiSettingsHandlerReq) returns (ThreadsAccountAiSettingsData)

	@handler listThreadsAccountAiProviderModels
	post /:id/ai-settings/providers/:provider/models (ThreadsAccountAiProviderModelsHandlerReq) returns (ThreadsAccountAiProviderModelsData)
}