app/models/ruby_conversations/concerns/conversation_templates.rb



# frozen_string_literal: true

module RubyConversations
  module Concerns
    # Handles template-related functionality for Conversation
    module ConversationTemplates
      extend ActiveSupport::Concern

      # Fetch template metadata for a given template name
      # @param template_name [String] The name of the template to fetch
      # @return [Hash] Template metadata including fields and UI configuration
      def template_for(template_name)
        begin
          template_data = RubyConversations.client.fetch_conversation_template(template_name)
        rescue RubyConversations::ClientError => e
          raise RubyConversations::TemplateNotFoundError, "Template #{template_name} not found" if e.status_code == 404

          raise e
        end
        extract_template_metadata(template_data)
      end

      # Fetch metadata for all available templates
      # @return [Array<Hash>] Array of template metadata including fields and UI configuration
      def all_templates
        begin
          templates_data = RubyConversations.client.fetch_all_conversation_templates
        rescue RubyConversations::ClientError => e
          raise RubyConversations::TemplateNotFoundError, 'Failed to fetch templates' if e.status_code == 404

          raise e
        end

        return [] unless templates_data.is_a?(Array)

        templates_data.map { |template_data| extract_template_metadata(template_data) }.compact
      end

      private

      def extract_template_metadata(template_data)
        return nil unless template_data

        build_template_metadata(template_data)
      end

      def build_template_metadata(template_data)
        base_metadata(template_data).merge(ui_metadata(template_data))
      end

      def base_metadata(template_data)
        {
          id: template_data['template_name'],
          name: template_display_name(template_data),
          description: template_data['template_description'] || "Template: #{template_data['template_name']}",
          fields: extract_fields_from_template(template_data)
        }
      end

      def ui_metadata(template_data)
        {
          icon: template_data['template_icon'] || 'fa-light fa-comment',
          color: template_data['template_color'] || 'blue',
          response_enabled: template_response_enabled?(template_data),
          cta: template_data['template_call_to_action'] || 'Submit'
        }
      end

      def template_display_name(template_data)
        prompt_name = extract_prompt_name(template_data)
        template_data['template_display_name'] || default_display_name(prompt_name, template_data)
      end

      def extract_prompt_name(template_data)
        return nil unless template_data['ai_messages']
        return nil unless template_data['ai_messages'].first

        prompts = template_data['ai_messages'].first['ai_message_prompts']
        prompts&.first&.dig('name')
      end

      def default_display_name(prompt_name, template_data)
        return prompt_name.titleize if prompt_name

        template_data['template_name'].titleize
      end

      def template_response_enabled?(template_data)
        template_data['template_response_enabled'].nil? ||
          template_data['template_response_enabled']
      end

      def extract_fields_from_template(template_data)
        return [] unless message_inputs?(template_data)

        inputs = get_message_inputs(template_data)
        inputs.map do |input|
          {
            name: input['placeholder_name'],
            type: input['field_type'] || 'text',
            label: input['label'] || input['placeholder_name'].titleize
          }
        end
      end

      def message_inputs?(template_data)
        return false unless template_data['ai_messages']
        return false unless template_data['ai_messages'].first

        prompts = template_data['ai_messages'].first['ai_message_prompts']
        return false unless prompts&.first

        prompts.first['ai_message_inputs'].present?
      end

      def get_message_inputs(template_data)
        return [] unless message_inputs?(template_data)

        template_data['ai_messages'].first['ai_message_prompts'].first['ai_message_inputs']
      end
    end
  end
end