syntax = "v1"

type (
	ClaimWorkerJobReq {
		WorkerType string `json:"worker_type" validate:"required"`
		WorkerID   string `json:"worker_id" validate:"required"`
	}

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

	WorkerJobReq {
		WorkerJobPath
		WorkerID string `json:"worker_id" validate:"required"`
	}

	WorkerHeartbeatReq {
		WorkerJobPath
		WorkerID   string `json:"worker_id" validate:"required"`
		TTLSeconds int    `json:"ttl_seconds,optional"`
	}

	WorkerProgressReq {
		WorkerJobPath
		WorkerID   string                `json:"worker_id" validate:"required"`
		Phase      string                `json:"phase,optional"`
		Summary    string                `json:"summary,optional"`
		Percentage int                   `json:"percentage,optional"`
		Steps      []JobStepProgressData `json:"steps,optional"`
	}

	WorkerCompleteReq {
		WorkerJobPath
		WorkerID string                 `json:"worker_id" validate:"required"`
		Result   map[string]interface{} `json:"result,optional"`
	}

	WorkerFailReq {
		WorkerJobPath
		WorkerID string `json:"worker_id" validate:"required"`
		Error    string `json:"error" validate:"required"`
		Phase    string `json:"phase,optional"`
	}

	WorkerCancelCheckData {
		Cancelled bool `json:"cancelled"`
	}

	WorkerOKData {
		OK bool `json:"ok"`
	}

	StorePersonaStyleProfileReq {
		ID             string `path:"id" validate:"required"`
		TenantID       string `json:"tenant_id" validate:"required"`
		OwnerUID       string `json:"owner_uid" validate:"required"`
		StyleProfile   string `json:"style_profile" validate:"required"`
		StyleBenchmark string `json:"style_benchmark,optional"`
	}

	StorePersonaStyleProfileData {
		ID       string `json:"id"`
		UpdateAt int64  `json:"update_at"`
	}

	WorkerThreadsAccountSessionReq {
		ID       string `path:"id" validate:"required"`
		TenantID string `json:"tenant_id" validate:"required"`
		OwnerUID string `json:"owner_uid" validate:"required"`
	}

	WorkerThreadsAccountSessionData {
		AccountID    string `json:"account_id"`
		StorageState string `json:"storage_state"`
		UpdateAt     int64  `json:"update_at"`
	}

	AnalyzeStyle8DPostReq {
		Text       string `json:"text"`
		Permalink  string `json:"permalink,optional"`
		LikeCount  int    `json:"like_count,optional"`
		ReplyCount int    `json:"reply_count,optional"`
	}

	AnalyzeStyle8DReq {
		ID               string                      `path:"id" validate:"required"`
		WorkerID         string                      `json:"worker_id" validate:"required"`
		TenantID         string                      `json:"tenant_id" validate:"required"`
		OwnerUID         string                      `json:"owner_uid" validate:"required"`
		PersonaID        string                      `json:"persona_id" validate:"required"`
		ThreadsAccountID string                      `json:"threads_account_id" validate:"required"`
		Username         string                      `json:"username" validate:"required"`
		Posts            []AnalyzeStyle8DPostReq     `json:"posts" validate:"required"`
		Steps            []JobStepProgressData       `json:"steps,optional"`
	}

	AnalyzeStyle8DData {
		PersonaID      string `json:"persona_id"`
		PostCount      int    `json:"post_count"`
		StyleProfile   string `json:"style_profile"`
		StyleBenchmark string `json:"style_benchmark"`
	}
)

@server(
	group:      job
	prefix:     /api/v1/internal
	middleware: WorkerSecret
	tags:       "Internal Worker"
	summary:    "Internal worker endpoints protected by X-Worker-Secret when InternalWorker.Secret is configured."
)
service gateway {
	@handler claimWorkerJob
	post /workers/jobs/claim (ClaimWorkerJobReq) returns (JobData)

	@handler refreshWorkerJobLock
	post /workers/jobs/:id/heartbeat (WorkerHeartbeatReq) returns (WorkerOKData)

	@handler checkWorkerJobCancel
	post /workers/jobs/:id/cancel-check (WorkerJobReq) returns (WorkerCancelCheckData)

	@handler ackWorkerJobCancel
	post /workers/jobs/:id/cancel-ack (WorkerJobReq) returns (JobData)

	@handler updateWorkerJobProgress
	post /workers/jobs/:id/progress (WorkerProgressReq) returns (JobData)

	@handler completeWorkerJob
	post /workers/jobs/:id/complete (WorkerCompleteReq) returns (JobData)

	@handler failWorkerJob
	post /workers/jobs/:id/fail (WorkerFailReq) returns (JobData)

	@handler storePersonaStyleProfileFromWorker
	patch /workers/personas/:id/style-profile (StorePersonaStyleProfileReq) returns (StorePersonaStyleProfileData)

	@handler getWorkerThreadsAccountSession
	post /workers/threads-accounts/:id/session (WorkerThreadsAccountSessionReq) returns (WorkerThreadsAccountSessionData)

	@handler analyzeStyle8DFromWorker
	post /workers/jobs/:id/analyze-style8d (AnalyzeStyle8DReq) returns (AnalyzeStyle8DData)
}
