package repository

import (
	"context"

	"haixun-backend/internal/model/threads_account/domain/entity"
)

type Repository interface {
	EnsureIndexes(ctx context.Context) error
	Create(ctx context.Context, account *entity.Account) (*entity.Account, error)
	FindByID(ctx context.Context, tenantID, ownerUID, accountID string) (*entity.Account, error)
	FindByIDGlobal(ctx context.Context, accountID string) (*entity.Account, error)
	FindByThreadsUserID(ctx context.Context, tenantID, ownerUID, threadsUserID string) (*entity.Account, error)
	ListByOwner(ctx context.Context, tenantID, ownerUID string) ([]*entity.Account, error)
	UpdateShell(ctx context.Context, tenantID, ownerUID, accountID string, displayName, username, personaID *string) (*entity.Account, error)
	UpdateAPIProfile(ctx context.Context, tenantID, ownerUID, accountID, username, threadsUserID, displayName string) (*entity.Account, error)
	ClearAPIProfile(ctx context.Context, tenantID, ownerUID, accountID string) (*entity.Account, error)
	SoftDelete(ctx context.Context, tenantID, ownerUID, accountID string) error
	Delete(ctx context.Context, tenantID, ownerUID, accountID string) error
	SetKnownAccounts(ctx context.Context, tenantID, ownerUID, accountID string, known map[string]entity.KnownAccountProfile) error
}

type SecretsRepository interface {
	EnsureIndexes(ctx context.Context) error
	FindByAccountID(ctx context.Context, accountID string) (*entity.Secrets, error)
	SaveBrowserStorageState(ctx context.Context, accountID, storageState string) (*entity.Secrets, error)
	SaveAPIAccessToken(ctx context.Context, accountID, accessToken string, expiresAt int64) (*entity.Secrets, error)
	ClearAPIAccessToken(ctx context.Context, accountID string) error
	DeleteByAccountID(ctx context.Context, accountID string) error
	ListAPIAccessTokensExpiringBefore(ctx context.Context, beforeNs int64) ([]*entity.Secrets, error)
}

type OAuthStateStore interface {
	Save(ctx context.Context, state string, payload OAuthStatePayload) error
	Consume(ctx context.Context, state string) (*OAuthStatePayload, error)
}

type OAuthStatePayload struct {
	TenantID  string `json:"tenant_id"`
	OwnerUID  string `json:"owner_uid"`
	AccountID string `json:"account_id,omitempty"`
}
