results = $results ?? new ResultStore(); $this->decisions = $decisions ?? new DecisionStore(); $this->checks = $checks ?? new Checks(); } /** * @param string $resultKey Stored result's result_key. * @param string $conflictId SourceConflict.id to decide. * @param array $params {@type string $action 'keep_preferred'|'use_value'; @type string $ref Required when action is 'use_value'.} * @param int $userId Acting user id. * @return array|WP_Error The updated GenerationResult, or an error. */ public function decide( string $resultKey, string $conflictId, array $params, int $userId ) { $row = $this->results->get( $resultKey ); if ( null === $row ) { return new WP_Error( 'aipt_blueprint_result_not_found', __( 'This result no longer exists.', 'ai-product-tools' ), array( 'status' => 404 ) ); } if ( (int) $row['createdBy'] !== $userId && ! current_user_can( self::CAP_MANAGE ) ) { return new WP_Error( 'aipt_blueprint_forbidden', __( 'You do not have permission to decide a conflict on this result.', 'ai-product-tools' ), array( 'status' => 403 ) ); } $payload = is_array( $row['payload'] ) ? $row['payload'] : array(); $conflicts = is_array( $payload['conflicts'] ?? null ) ? $payload['conflicts'] : array(); $index = $this->findConflictIndex( $conflicts, $conflictId ); if ( null === $index ) { return new WP_Error( 'aipt_blueprint_conflict_not_found', __( 'This conflict no longer exists on this result.', 'ai-product-tools' ), array( 'status' => 404 ) ); } $target = $conflicts[ $index ]; $action = in_array( $params['action'] ?? '', array( 'keep_preferred', 'use_value' ), true ) ? (string) $params['action'] : ''; if ( '' === $action ) { return new WP_Error( 'aipt_blueprint_invalid_request', __( 'Choose a valid decision action.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $chosen_ref = 'keep_preferred' === $action ? (string) $target['preferredRef'] : (string) ( $params['ref'] ?? '' ); $chosen_value = $this->valueForRef( $target, $chosen_ref ); if ( null === $chosen_value ) { return new WP_Error( 'aipt_blueprint_invalid_conflict_ref', __( 'This value is not one of the conflicting values.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $subject_type = (string) ( $payload['subject']['type'] ?? '' ); $subject_id = (int) ( $payload['subject']['id'] ?? 0 ); $this->decisions->decide( array( 'subjectType' => $subject_type, 'subjectId' => $subject_id, 'fieldKey' => (string) $target['field'], 'chosenRef' => $chosen_ref, 'chosenValueHash' => hash( 'sha256', $chosen_value ), 'versionsHash' => (string) $target['versionsHash'], 'decidedBy' => $userId, ) ); $decision_rows = $this->decisions->listForSubject( $subject_type, $subject_id ); foreach ( $conflicts as $i => $conflict ) { $conflicts[ $i ]['outcome'] = $this->recomputeOutcome( $conflict, $decision_rows ); } $payload['conflicts'] = $conflicts; $payload['eligibility'] = $this->checks->eligibility( is_array( $payload['findings'] ?? null ) ? $payload['findings'] : array(), $conflicts ); $payload['resultKey'] = $resultKey; if ( ! $this->results->updatePayload( $resultKey, $payload ) ) { return new WP_Error( 'aipt_blueprint_result_save_failed', __( 'The decision could not be saved. Please try again.', 'ai-product-tools' ), array( 'status' => 500 ) ); } return $payload; } /** * @param array $conflict One SourceConflict. * @param array $decision_rows Current decisions for this subject. * @return string SourceConflict.outcome. */ private function recomputeOutcome( array $conflict, array $decision_rows ): string { foreach ( $decision_rows as $decision ) { if ( (string) ( $decision['fieldKey'] ?? '' ) === (string) ( $conflict['field'] ?? '' ) && (string) ( $decision['versionsHash'] ?? '' ) === (string) ( $conflict['versionsHash'] ?? '' ) ) { return 'accepted'; } } switch ( (string) ( $conflict['policy'] ?? 'review' ) ) { case 'prefer': return 'used_preferred'; case 'skip': return 'omitted'; default: return 'needs_review'; } } /** * @param array[] $conflicts SourceConflict list. * @param string $conflict_id Conflict id to find. * @return int|null */ private function findConflictIndex( array $conflicts, string $conflict_id ): ?int { foreach ( $conflicts as $index => $conflict ) { if ( (string) ( $conflict['id'] ?? '' ) === $conflict_id ) { return $index; } } return null; } /** * @param array $conflict One SourceConflict. * @param string $ref Candidate ref. * @return string|null The matching value's text, or null when $ref is not one of the conflict's values. */ private function valueForRef( array $conflict, string $ref ): ?string { foreach ( (array) ( $conflict['values'] ?? array() ) as $value ) { if ( (string) ( $value['ref'] ?? '' ) === $ref ) { return (string) ( $value['value'] ?? '' ); } } return null; } }