# frozen_string_literal: true

require "json"

module Dispatch
  module Adapter
    class Claude < Base
      module PricingTable
        DATA_PATH = File.expand_path("data/claude_pricing.json", __dir__)

        # Raw Hash loaded once from the bundled JSON file.
        TABLE = begin
          raw = JSON.parse(File.read(DATA_PATH))
          raw.freeze
        end

        module_function

        # Returns a ModelPricing for the given model id, or nil if unknown.
        #
        # @param model_id [String]
        # @return [Dispatch::Adapter::ModelPricing, nil]
        def lookup(model_id)
          entry = TABLE[model_id]
          return nil unless entry

          ModelPricing.new(
            input_per_mtok: entry["input_per_mtok"],
            output_per_mtok: entry["output_per_mtok"],
            cache_read_per_mtok: entry["cache_read_per_mtok"],
            cache_write_per_mtok: entry["cache_write_per_mtok"]
          )
        end

        # Returns the context window size for the given model id, or nil.
        #
        # @param model_id [String]
        # @return [Integer, nil]
        def context_window(model_id)
          TABLE.dig(model_id, "context_window")
        end

        # Returns the max output token count for the given model id, or nil.
        #
        # @param model_id [String]
        # @return [Integer, nil]
        def max_output_tokens(model_id)
          TABLE.dig(model_id, "max_output_tokens")
        end

        # Returns all known model id strings.
        #
        # @return [Array<String>]
        def known_ids
          TABLE.keys
        end
      end
    end
  end
end
