getInputs(); $title = isset( $inputs['title'] ) ? "\"{$inputs['title']}\"" : '(untitled)'; $summary = "Create service {$title}"; if ( ! empty( $inputs['duration_minutes'] ) ) { $summary .= " ({$inputs['duration_minutes']} min)"; } if ( isset( $inputs['price'] ) && '' !== $inputs['price'] ) { $summary .= ' priced at ' . $inputs['price']; } return $summary; } /** * Human-readable inputs shown in the approval card table. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool after human approval. * * @param string $title Service name. * @param string|null $description Service description. * @param int|null $duration_minutes Duration in minutes. * @param float|null $price Charge amount. * @param string|null $status active|deactive. * @param int|null $before_buffer_time Buffer before, minutes. * @param int|null $after_buffer_time Buffer after, minutes. * @param array|null $team_member_ids Assigned team-member IDs. * @param array|null $category_ids Category term IDs. * @return array */ public function __invoke( string $title, ?string $description = null, ?int $duration_minutes = null, ?float $price = null, ?string $status = null, ?int $before_buffer_time = null, ?int $after_buffer_time = null, ?array $team_member_ids = null, ?array $category_ids = null ): array { $check = $this->guard_admin(); if ( $check ) { return $check; } if ( empty( trim( $title ) ) ) { return [ 'error' => 'title is required.' ]; } if ( null !== $status && ! in_array( $status, self::ALLOWED_STATUSES, true ) ) { return [ 'error' => "Invalid status '{$status}'. Accepted values: " . implode( ', ', self::ALLOWED_STATUSES ) . '.' ]; } try { $body = [ 'title' => sanitize_text_field( $title ), 'status' => $status ?? 'active', ]; if ( null !== $description ) { $body['description'] = sanitize_text_field( $description ); } if ( null !== $before_buffer_time ) { $body['before_buffer_time'] = max( 0, $before_buffer_time ); } if ( null !== $after_buffer_time ) { $body['after_buffer_time'] = max( 0, $after_buffer_time ); } if ( null !== $team_member_ids ) { $body['team_member'] = array_values( array_unique( array_map( 'intval', $team_member_ids ) ) ); } if ( null !== $category_ids ) { $body['categories'] = array_values( array_unique( array_map( 'intval', $category_ids ) ) ); } if ( null !== $duration_minutes || null !== $price ) { $body['additional_durations'] = [ $this->default_duration_row( (int) ( $duration_minutes ?? 30 ), (float) ( $price ?? 0 ) ), ]; } $request = $this->build_json_request( 'POST', '/booktics/v1/services', $body ); if ( is_array( $request ) ) { return $request; } $controller = new \Booktics\Service\Controllers\Service_Controller(); $data = $this->extract_response( $controller->create_item( $request ), 'Failed to create service.' ); if ( isset( $data['error'] ) ) { return $data; } $id = (int) ( $data['id'] ?? 0 ); if ( $id <= 0 ) { Log::error( 'Booktics_Create_Service_Tool got a success response without an ID', [ 'data_keys' => array_keys( $data ) ] ); return [ 'error' => 'The service may not have been created; Booktics returned no ID. Please verify in Booktics before retrying.' ]; } return [ 'success' => true, 'service_id' => $id, 'title' => $title, 'message' => "Service \"{$title}\" created successfully (ID: {$id}).", ]; } catch ( \Throwable $e ) { Log::error( 'Booktics_Create_Service_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to create service: ' . $e->getMessage() ]; } } }