#-*- coding: utf-8 -*-
#
# Système de gestion des adhésions de Nos oignons
# Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'json'
require 'rest-client'

module NosOignons
  module Mailman
    class MailmanMember < Struct.new(:member_id, :email, keyword_init: true)
      def unsubscribe!
        Mailman::request(:delete, "/3.0/members/#{URI::encode_www_form_component(member_id)}")
      end
    end

    class << self
      def list_members(list)
        s = request(:get, "/3.0/lists/#{URI::encode_www_form_component(list)}/roster/member")
        r = JSON.parse(s)
        r["entries"].collect { |entry|
            MailmanMember.new(member_id: entry["member_id"], email: entry["email"])
          }
      end

      def susbcribe_email(list, email)
        list_id = list.gsub(/@/, '.')
        request(:post, "/3.0/members", payload: {
              list_id: list_id,
              subscriber: email,
              pre_verified: true,
              pre_confirmed: true,
              pre_approved: true,
            }.to_json,
            headers: { content_type: :json },
          )
      end

      def request(method, path, **args)
        RestClient::Request.execute(
          **args,
          method: method,
          url: "#{rest_url}#{path}"
        )
      end

      private

      def rest_url
        @rest_url if defined?(@rest_url)

        @rest_url = ENV["NOS_OIGNONS_MAILMAN_REST_API_URL"]
        if @rest_url.nil?
          cred_path = File.join("#{ENV["CREDENTIALS_DIRECTORY"]}", "mailman-rest-url")
          begin
            @rest_url = File.open(cred_path).read
          rescue Errno::ENOENT
            raise ArgumentError.new("Environment variable `NOS_OIGNONS_MAILMAN_REST_API_URL` not defined, and unable to read the file `$CREDENTIALS_DIRECTORY/mailman-rest-url` either")
          end
        end
        @rest_url
      end
    end
  end
end
