using System.Net.Http.Headers;
using IntegryApiClient.Core.Domain.RestClient.Contacts;
using SteUp.Shared.Core.Dto;
using SteUp.Shared.Core.Interface.IntegryApi;

namespace SteUp.Shared.Core.Services;

public class IntegrySteupService(IIntegryApiRestClient integryApiRestClient) : IIntegrySteupService
{
    private const string BaseRequest = "steup";

    #region Retrieve

    public Task<List<PuntoVenditaDto>> RetrievePuntiVendita() =>
        integryApiRestClient.AuthorizedGet<List<PuntoVenditaDto>>($"{BaseRequest}/retrievePuntiVendita")!;

    public Task<List<JtbFasiDto>> RetrieveReparti() =>
        integryApiRestClient.AuthorizedGet<List<JtbFasiDto>>($"{BaseRequest}/retrieveReparti")!;

    public Task<List<ActivityTypeDto>> RetrieveActivityType() =>
        integryApiRestClient.AuthorizedGet<List<ActivityTypeDto>>($"{BaseRequest}/retrieveActivityType")!;

    public Task<List<ArticoliInGrigliaDto>?> RetrieveGrigliaPlu(RetrieveGrigliaPluRequestDto request) =>
        integryApiRestClient.AuthorizedPost<List<ArticoliInGrigliaDto>?>($"{BaseRequest}/retrieveGrigliaPlu", request);

    public Task<ArticoliInGrigliaDto?> RetrieveArtFromBarcode(string barcode) =>
        integryApiRestClient.AuthorizedGet<ArticoliInGrigliaDto?>(
            $"{BaseRequest}/retrieveArtFromBarcode",
            new Dictionary<string, object?>
            {
                { "barcode", barcode }
            }
        );

    public Task<List<StbActivityDto>?> RetrieveStatoIspezioni(RetrieveStatoIspezioniRequestDto request) =>
        integryApiRestClient.AuthorizedPost<List<StbActivityDto>?>($"{BaseRequest}/retrieveStatoIspezioni", request);

    #endregion

    #region Save

    public Task<SaveSchedaResponseDto?> SaveScheda(SaveRequestDto request) =>
        integryApiRestClient.AuthorizedPost<SaveSchedaResponseDto?>($"{BaseRequest}/saveScheda", request);

    public Task<SaveSchedaResponseDto?> SaveMultipleSchede(List<SaveRequestDto> request) =>
        integryApiRestClient.AuthorizedPost<SaveSchedaResponseDto?>($"{BaseRequest}/saveMultipleSchede", request);

    public Task CompleteInspection(string activityId) =>
        integryApiRestClient.AuthorizedPost<object>(
            $"{BaseRequest}/complete",
            new object(),
            new Dictionary<string, object>
            {
                { "activityId", activityId }
            }!
        );

    #endregion

    public async Task UploadFile(string activityId, byte[] file, string fileName)
    {
        var queryParams = new Dictionary<string, object> { { "activityId", activityId } };

        var content = new MultipartFormDataContent();
        var fileContent = new ByteArrayContent(file);
        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
        content.Add(fileContent, "file", fileName);

        await integryApiRestClient.Post<object>($"{BaseRequest}/uploadAttachment", content, queryParams!);
        content.Dispose();
    }

    public Task DeleteScheda(string activityId) =>
        integryApiRestClient.AuthorizedGet<object>(
            $"{BaseRequest}/deleteScheda",
            new Dictionary<string, object?>
            {
                { "activityId", activityId }
            }
        );
}