using System;
using System.Collections.Generic;
using System.IO;
using FitConnect.Models;
using FluentAssertions;
using Newtonsoft.Json;
using NUnit.Framework;

namespace IntegrationTests.Sender;

[TestFixture]
public class SenderTestHappyPath : SenderTestBase {
    [Test]
    public void CheckIfSecretsAreValid() {
        _clientId.Should().NotBe("00000000-0000-0000-0000-000000000000");
        _clientSecret.Should().NotBe("0000000000000000000000000000000000000000000");
    }

    [Test]
    public void WithDestination_PrepareSubmissionToServer() {
        // Arrange

        // Act
        Sender.WithDestination(desitnationId);

        // Assert
        Sender.PublicKey.Should().NotBeNullOrEmpty();
    }

    [Test]
    public void WithAttachments_IntroduceSubmission_ShouldGetIdFromServer() {
        // Arrange
        var dut = Sender
            .WithDestination(desitnationId)
            .WithServiceType("ServiceName", "urn:de:fim:leika:leistung:99400048079000");

        var attachments = new List<Attachment>();
        var attachment = new Attachment("Test.pdf", "Just an attachment");

        attachments.Add(attachment);

        // {'attachmentId': 'c96cce5b-f35d-4c38-ac19-9bc47721c0f9', 'hash': {'type': 'sha512', 'content': '8b1042900c2039f65fe6c4cb1bca31e2a7a04b61d3ca7d9ae9fc4077068b82ad5512fa298385b025db70551113b762064444b87737e45e657a71be5b88b06e59'}, 'mimeType': 'application/pdf', 'purpose': 'attachment'}


        // Act
        dut.WithAttachments(attachments);


        // Assert
        attachment.Hash.Should()
            .Be(
                "8b1042900c2039f65fe6c4cb1bca31e2a7a04b61d3ca7d9ae9fc4077068b82ad5512fa298385b025db70551113b762064444b87737e45e657a71be5b88b06e59");
        var submission = GetSubmissionInfo(Sender);
        submission.Attachments.Should().HaveCount(attachments.Count);

        submission.Id.Should().NotBeNullOrWhiteSpace();
        Console.WriteLine(submission.CaseId);
    }


    [TestCase("0b8e6fbd-62e2-4b6f-b333-5308d82e0a00")]
    [TestCase("d2be2027-9368-4c0c-a265-2fdbf7ecd4d9")]
    public void GetSubmissionStatus(string caseId) {
        var status = Sender.GetStatusForSubmission(caseId);
        status.ForEach(s => Console.WriteLine($"{s.EventTime} - {s.EventType}"));
        status.Count.Should().BeGreaterThan(0);
    }

    [Test]
    public void Submit_FinishSubmission_ShouldGetIdFromServer() {
        // Arrange

        var attachments = new List<Attachment>();

        var attachment = new Attachment("Test.pdf", "Just an attachment");
        attachments.Add(attachment);
        var dut = Sender
            .WithDestination(desitnationId)
            .WithServiceType("ServiceName", "urn:de:fim:leika:leistung:99400048079000")
            .WithAttachments(attachments);

        // Act
        dut.Submit();

        // Assert
        var submission = GetSubmissionInfo(Sender);
        Console.WriteLine($"Case ID: {submission.CaseId}");
        submission.Should().NotBeNull();
    }

    [Test]
    public void Submit_FinishSubmissionWithData_ShouldGetIdFromServer() {
        // Arrange

        var attachments = new List<Attachment>();

        var content = File.ReadAllBytes("Test.pdf");
        var attachment = new Attachment("Test.pdf", "Just an attachment") {
            Filename = "RandomBytes",
            MimeType = "application/pdf",
            Description = "Just a test",
            Purpose = "attachment"
        };
        attachments.Add(attachment);
        var dut = Sender
            .WithDestination(desitnationId)
            .WithServiceType("ServiceName", "urn:de:fim:leika:leistung:99400048079000")
            .WithAttachments(attachments)
            .WithData(JsonConvert.SerializeObject(new {
                FirstName = "John",
                LastName = "Doe",
                Age = 42,
                Birthday = DateTime.Today.AddYears(-42)
            }));

        // Act
        dut.Submit();

        // Assert
        var submission = GetSubmissionInfo(Sender);
        var statusForSubmission = Sender.GetStatusForSubmission(submission.CaseId);
        foreach (var securityEventToken in statusForSubmission)
            Console.WriteLine(securityEventToken.Token.Subject);

        statusForSubmission.Should().HaveCountGreaterThan(0);
        Console.WriteLine($"Case ID: {submission.CaseId}");
        Console.WriteLine($"Submission ID: {submission.Id}");
        submission.Should().NotBeNull();
    }

    [Ignore("Destination can not be found")]
    [Test]
    public void Submit_FinishSubmissionWithDataFindDestination_ShouldGetIdFromServer() {
        // Arrange

        var attachments = new List<Attachment>();

        var content = File.ReadAllBytes("Test.pdf");
        var attachment = new Attachment("Test.pdf", "Just an attachment") {
            Filename = "RandomBytes",
            MimeType = "application/pdf",
            Description = "Just a test",
            Purpose = "attachment"
        };
        attachments.Add(attachment);
        var dut = Sender
            .FindDestinationId("99400048079000", ars: "09372126")
            .WithServiceType("ServiceName", "urn:de:fim:leika:leistung:99400048079000")
            .WithAttachments(attachments)
            .WithData(JsonConvert.SerializeObject(new {
                FirstName = "John",
                LastName = "Doe",
                Age = 42,
                Birthday = DateTime.Today.AddYears(-42)
            }));

        // Act
        dut.Submit();

        // Assert
        var submission = GetSubmissionInfo(Sender);
        var statusForSubmission = Sender.GetStatusForSubmission(submission.CaseId);
        foreach (var securityEventToken in statusForSubmission)
            Console.WriteLine(securityEventToken.Token.Subject);

        statusForSubmission.Should().HaveCountGreaterThan(0);
        Console.WriteLine($"Case ID: {submission.CaseId}");
        Console.WriteLine($"Submission ID: {submission.Id}");
        submission.Should().NotBeNull();
    }


    [Test]
    public void GetDestinations_ShouldGetDestinationsFromServer() {
        // Arrange
        var destinations = Sender.FindDestinationId("99123456760610",
            ars: "064350014014");
        Sender.PublicKey.Should().NotBeNull(); //destinations.Should().HaveCountGreaterThan(0));

        // foreach (var destination in destinations) {
        //     destination.DestinationParameters.PublicKeys.Keys.ForEach(k =>
        //         Console.WriteLine(k.ToString()));
        //     destination.DestinationParameters.Status.Should().Be("active");
        // }
    }
}