using System.Net;
using IWS5.Autotest.IWS5.Api.Api;
using IWS5.Autotest.IWS5.Api.Common;
using IWS5.Core.DTO;
using Newtonsoft.Json;

namespace IWS5.Autotest.IWS5.Autotests
{
    public class ButtonsApiTest
    {
        //this test will work only for PDadmin user with permissions to Emark process with Staff Review Queue queue
        private const string QueueName = "Staff Review Queue";
        private const string approved = "Approved";
        private const string issue = "Issue";
        private readonly ButtonsControllerApi _buttonsControllerApi;
        private readonly SessionApi _sessionApi;

        public ButtonsApiTest()
        {
            _sessionApi = new SessionApi();
            _buttonsControllerApi = new ButtonsControllerApi(_sessionApi.SessionStorage);
        }

        [OneTimeSetUp]
        public async Task Init()
        {
            await _sessionApi.SignIn();
        }

        [Test]
        public async Task Get_WorkItemsInboxForUser_ReturnsSuccess()
        {
            var response = await _buttonsControllerApi.GetWorkItemsInboxForUser(QueueName);
            var items = JsonConvert.DeserializeObject<List<ButtonListItemDTO>>(response.Content);

            Assert.Multiple(() =>
            {
                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
                Assert.That(response.Data, Is.Not.Null);
                Assert.That(response.Data.Count, Is.EqualTo(2));

                var approvedItem = items.First(i => i.Index == 0);
                Assert.That(approvedItem.Name, Is.EqualTo(approved));
                Assert.That(approvedItem.AllowedToClick, Is.True);

                var issueItem = items.First(i => i.Index == 1);
                Assert.That(issueItem.Name, Is.EqualTo(issue));
                Assert.That(issueItem.AllowedToClick, Is.True);
            });
        }

        [Test]
        public async Task Get_WorkItemsInboxForUser_WithInvalidQueue_ReturnsEmpty()
        {
            const string invalidQueueName = "Invalid Queue Name";

            var response = await _buttonsControllerApi.GetWorkItemsInboxForUser(invalidQueueName);
            var items = JsonConvert.DeserializeObject<List<ButtonListItemDTO>>(response.Content);

            Assert.Multiple(() =>
            {
                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
                Assert.That(items, Is.Empty, "Expected no buttons for an invalid queue name.");
            });
        }

        //Bug https://greenfieldinvestments.atlassian.net/browse/PDR-915
        [TestCase(null)]
        [TestCase("")]
        [TestCase("' OR 1=1 --")]
        public async Task Get_WorkItemsInboxForUser_WithInvalidInputs_ReturnsEmpty(string? queueName)
        {
            var response = await _buttonsControllerApi.GetWorkItemsInboxForUser(queueName);
            var items = JsonConvert.DeserializeObject<List<ButtonListItemDTO>>(response.Content);

            Assert.Multiple(() =>
            {
                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
                Assert.That(items, Is.Empty, $"Expected no buttons for queue name: {queueName ?? "<null>"}");
            });
        }

    }
}
