# typed: false
# frozen_string_literal: true

require "test_helper"

class YettoControllerTest < ActionDispatch::IntegrationTest
  include API::TestHelpers

  include Webmocks::YettoWebmock

  def setup
    super
    @after_create_plug_installation = {
      plug_installation: {
        id: @plug_installation_id,
        settings: {
          from_email: "new@from.company",
          reply_to_email: "new@from.company",
        },
      },
      organization: {
        id: @organization_id,
      },
      plug: {
        id: @plug_id,
      },
      inbox: {
        id: @inbox_id,
      },
    }

    @after_create_message = {
      plug_installation: {
        id: @plug_installation_id,
        settings: {},
        credentials: {},
      },
      message: {
        id: @message_id,
        text_content: "blern",
        html_content: "<p>blern</p>",
        metadata: {},
        conversation: {
          id: @conversation_id,
          title: "Hey there?",
          metadata: {
            "in-reply-to" => "123",
          },
        },
      },
      inbox: {
        id: @inbox_id,
        organization: {
          id: @organization_id,
          status: "active",
        },
      },
    }
  end

  def headers(body)
    {
      Headers::Yetto::HEADER_EVENT => "after_create",
      Headers::Yetto::HEADER_RECORD_TYPE => "plug_installation",
      Headers::Yetto::HEADER_SIGNATURE => yetto_auth_header(body),
    }
  end

  test "it handles null headers" do
    api(:post, "/after_create/plug_installation", headers: nil)

    assert_response :bad_request
  end

  test "it handles missing headers" do
    api(:post, "/after_create/plug_installation", headers: {})

    assert_response :bad_request
  end

  test "it handles incorrect headers" do
    api(:post, "/after_create/plug_installation", headers: { "X-Yetto-Signature" => "Basic jabroni:lies" })

    assert_response :bad_request

    api(:post, "/after_create/plug_installation", headers: { "X-Yetto-Signature" => "sha256=123456" })

    assert_response :bad_request
  end

  test "it handles missing body" do
    body = {}
    api(:post, "/after_create/plug_installation", headers: headers(body), body: body)

    assert_response :bad_request
  end

  test "it handles wrong event" do
    body = @after_create_plug_installation
    api(:post, "/bloop/plug_installation", headers: headers(body), body: body)

    assert_response :not_found
  end

  test "it handles wrong record type" do
    body = @after_create_plug_installation

    api(:post, "/after_create/plug_instunk", headers: headers(body), body: body)

    assert_response :not_found
  end

  test "it handles wrong signature type" do
    body = @after_create_plug_installation
    headers_with_one_body = headers(body)
    # body different than headers
    body_two = {
      plug: {
        id: "plg_#{Faker::Alphanumeric.alphanumeric(number: 26).upcase}",
      },
      inbox: {
        id: "inbx_#{Faker::Alphanumeric.alphanumeric(number: 26).upcase}",
      },
    }
    api(:post, "/after_create/plug_installation", headers: headers_with_one_body, body: body_two)

    assert_response :bad_request
  end
end