#
# This file is part of Astarte.
#
# Copyright 2019-2023 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.AppEngine.APIWeb.DeviceStatusByGroupControllerTest do
  use Astarte.AppEngine.APIWeb.ConnCase

  alias Astarte.AppEngine.API.DatabaseTestHelper
  alias Astarte.AppEngine.API.JWTTestHelper
  alias Astarte.AppEngine.API.Groups

  @realm "autotestrealm"
  @group_name "mygroup"
  @group_devices [
    "f0VMRgIBAQAAAAAAAAAAAA",
    "olFkumNuZ_J0f_d6-8XCDg",
    "DKxaeZ9LzUZLz7WPTTAEAA",
    "aWag-VlVKC--1S-vfzZ9uQ"
  ]

  @device_not_in_group "4UQbIokuRufdtbVZt9AsLg"

  @expected_introspection %{
    "com.example.PixelsConfiguration" => %{
      "major" => 1,
      "minor" => 0,
      "exchanged_msgs" => 4230,
      "exchanged_bytes" => 2_010_000
    },
    "com.example.ServerOwnedTestObject" => %{
      "major" => 1,
      "minor" => 0,
      "exchanged_msgs" => 100,
      "exchanged_bytes" => 30_000
    },
    "com.example.TestObject" => %{
      "major" => 1,
      "minor" => 5,
      "exchanged_msgs" => 9300,
      "exchanged_bytes" => 2_000_000
    },
    "com.test.LCDMonitor" => %{
      "major" => 1,
      "minor" => 3,
      "exchanged_msgs" => 10,
      "exchanged_bytes" => 3000
    },
    "com.test.SimpleStreamTest" => %{
      "major" => 1,
      "minor" => 0,
      "exchanged_msgs" => 0,
      "exchanged_bytes" => 0
    }
  }

  @expected_previous_interfaces [
    %{
      "name" => "com.test.LCDMonitor",
      "major" => 0,
      "minor" => 1,
      "exchanged_msgs" => 42,
      "exchanged_bytes" => 9000
    }
  ]

  @expected_device_id "f0VMRgIBAQAAAAAAAAAAAA"
  @expected_device_status %{
    "connected" => false,
    "id" => @expected_device_id,
    "aliases" => %{"display_name" => "device_a"},
    "introspection" => @expected_introspection,
    "last_connection" => "2017-09-28T03:45:00.000Z",
    "last_disconnection" => "2017-09-29T18:25:00.000Z",
    "first_registration" => "2016-08-15T09:44:00.000Z",
    "first_credentials_request" => "2016-08-20T09:44:00.000Z",
    "last_credentials_request_ip" => "198.51.100.89",
    "last_seen_ip" => "198.51.100.81",
    "attributes" => %{"attribute_key" => "device_a_attribute"},
    "credentials_inhibited" => false,
    "total_received_bytes" => 4_500_000,
    "total_received_msgs" => 45000,
    "previous_interfaces" => @expected_previous_interfaces,
    "groups" => [@group_name],
    "deletion_in_progress" => false
  }

  setup_all do
    {:ok, _client} = DatabaseTestHelper.create_test_keyspace()

    on_exit(fn ->
      DatabaseTestHelper.destroy_local_test_keyspace()
    end)

    :ok
  end

  setup %{conn: conn} do
    DatabaseTestHelper.seed_data()

    authorized_conn =
      conn
      |> put_req_header("accept", "application/json")
      |> put_req_header("authorization", "bearer #{JWTTestHelper.gen_jwt_all_access_token()}")

    {:ok, conn: authorized_conn}
  end

  describe "index" do
    setup [:populate_group]

    test "returns 404 for non-existing group", %{conn: conn} do
      conn = get(conn, device_status_by_group_path(conn, :index, @realm, "nonexisting"))

      assert json_response(conn, 404)["errors"]["detail"] == "Group not found"
    end

    test "returns device ids for a populated group", %{conn: conn} do
      index_conn = get(conn, device_status_by_group_path(conn, :index, @realm, @group_name))

      assert devices = json_response(index_conn, 200)["data"]

      assert length(devices) == length(@group_devices)

      for device <- @group_devices do
        assert Enum.member?(devices, device)
      end
    end

    test "returns correct details", %{conn: conn} do
      index_conn =
        get(
          conn,
          device_status_by_group_path(conn, :index, @realm, @group_name, details: true)
        )

      assert devices = json_response(index_conn, 200)["data"]

      assert length(devices) == length(@group_devices)

      for device <- devices do
        %{
          "id" => device_id,
          "connected" => _,
          "aliases" => _,
          "introspection" => _,
          "last_connection" => _,
          "last_disconnection" => _,
          "first_registration" => _,
          "first_credentials_request" => _,
          "last_credentials_request_ip" => _,
          "attributes" => _,
          "last_seen_ip" => _,
          "credentials_inhibited" => _,
          "total_received_bytes" => _,
          "total_received_msgs" => _,
          "groups" => [@group_name]
        } = device

        assert Enum.member?(@group_devices, device_id)
      end

      assert Enum.find(devices, &(Map.get(&1, "id") == @expected_device_id)) ==
               @expected_device_status
    end

    test "implements pagination correctly", %{conn: conn} do
      page_size = 3

      first_page_conn =
        get(
          conn,
          device_status_by_group_path(conn, :index, @realm, @group_name, limit: page_size)
        )

      assert first_page_response = json_response(first_page_conn, 200)
      first_page_devices = first_page_response["data"]
      assert length(first_page_devices) == page_size

      next_link = first_page_response["links"]["next"]

      second_page_conn = get(conn, next_link)

      assert second_page_response = json_response(second_page_conn, 200)
      second_page_devices = second_page_response["data"]
      assert length(second_page_devices) == length(@group_devices) - page_size

      assert second_page_response["links"]["self"] == next_link
      assert second_page_response["links"]["next"] == nil

      assert MapSet.disjoint?(MapSet.new(first_page_devices), MapSet.new(second_page_devices))
    end
  end

  describe "show" do
    setup [:populate_group]

    test "returns 404 with unexisting device", %{conn: conn} do
      conn =
        get(
          conn,
          device_status_by_group_path(conn, :show, @realm, @group_name, "7YZeUafPTZyEsKjO1yHIkw")
        )

      assert json_response(conn, 404)["errors"]["detail"] == "Device not found"
    end

    test "returns 404 with device not in group", %{conn: conn} do
      conn =
        get(
          conn,
          device_status_by_group_path(conn, :show, @realm, @group_name, @device_not_in_group)
        )

      assert json_response(conn, 404)["errors"]["detail"] == "Device not found"
    end

    test "returns status with valid device not in group", %{conn: conn} do
      conn =
        get(
          conn,
          device_status_by_group_path(conn, :show, @realm, @group_name, @expected_device_id)
        )

      assert json_response(conn, 200)["data"] == @expected_device_status
    end
  end

  describe "update" do
    setup [:populate_group]

    test "returns 404 with unexisting device", %{conn: conn} do
      set_device_alias_payload = %{
        "data" => %{
          "aliases" => %{
            "test_tag" => "test_alias"
          }
        }
      }

      conn =
        conn
        |> put_req_header("content-type", "application/merge-patch+json")
        |> patch(
          device_status_by_group_path(
            conn,
            :update,
            @realm,
            @group_name,
            "rPYl9S9uShibJV7fB-MphQ"
          ),
          set_device_alias_payload
        )

      assert json_response(conn, 404)["errors"]["detail"] == "Device not found"
    end

    test "returns 404 with device not in group", %{conn: conn} do
      set_device_alias_payload = %{
        "data" => %{
          "aliases" => %{
            "test_tag" => "test_alias"
          }
        }
      }

      conn =
        conn
        |> put_req_header("content-type", "application/merge-patch+json")
        |> patch(
          device_status_by_group_path(conn, :update, @realm, @group_name, @device_not_in_group),
          set_device_alias_payload
        )

      assert json_response(conn, 404)["errors"]["detail"] == "Device not found"
    end

    test "adds alias to device", %{conn: conn} do
      set_device_alias_payload = %{
        "data" => %{
          "aliases" => %{
            "test_tag" => "test_alias"
          }
        }
      }

      conn =
        conn
        |> put_req_header("content-type", "application/merge-patch+json")
        |> patch(
          device_status_by_group_path(conn, :update, @realm, @group_name, @expected_device_id),
          set_device_alias_payload
        )

      assert json_response(conn, 200)["data"] ==
               Map.put(@expected_device_status, "aliases", %{
                 "display_name" => "device_a",
                 "test_tag" => "test_alias"
               })
    end

    test "removes device alias", %{conn: conn} do
      unset_device_alias_payload = %{
        "data" => %{
          "aliases" => %{
            "display_name" => nil
          }
        }
      }

      conn =
        conn
        |> put_req_header("content-type", "application/merge-patch+json")
        |> patch(
          device_status_path(conn, :update, "autotestrealm", @expected_device_id),
          unset_device_alias_payload
        )

      assert json_response(conn, 200)["data"] ==
               Map.put(@expected_device_status, "aliases", %{})
    end
  end

  defp populate_group(_context) do
    params = %{
      "group_name" => @group_name,
      "devices" => @group_devices
    }

    {:ok, _group} = Groups.create_group(@realm, params)

    :ok
  end
end
