# Copyright (C) 2024 Manuel Bustillo

module Groups
  class SummaryQuery
    def call
      ActiveRecord::Base.connection.execute(query).to_a
    end

    private

    def query
      <<~SQL.squish
        SELECT
          groups.id,
          groups.name,
          groups.icon,
          groups.parent_id,
          groups.color,
          count(*) filter (where status IS NOT NULL) as total,
          count(*) filter (where status = 0) as considered,
          count(*) filter (where status = 10) as invited,
          count(*) filter (where status = 20) as confirmed,
          count(*) filter (where status = 30) as declined,
          count(*) filter (where status = 40) as tentative
        FROM groups
        LEFT JOIN guests on groups.id = guests.group_id
        GROUP BY groups.id
      SQL
    end
  end
end