using adas_core.Application.Exceptions;
using adas_core.Application.Services.Interfaces;
using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Domain.Models.DTO;
using adas_core.Domain.Models.Filter;
using adas_core.Domain.Models.MongoModels;
using adas_core.Logging;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace adas_core.Controllers;

/// <summary>
/// Serves as an MVC controller that handles HTTP requests related to pumps, mapped to the <c>"pumps"</c> route.
/// Inherits from <see cref="ControllerBase"/> to provide ASP.NET Core Web API capabilities.
/// </summary>
/// <!-- aidoc:v1 sig=e48df22 -->
[Route("pumps")]
public class PumpController : ControllerBase
{
    private readonly IHistoricalConfigChangesService _historicalConfigChangesService;
    private readonly IPumpService _pumpService;


    /// <summary>
    /// Initializes a new instance of <see cref="PumpController"/>, establishing a trace identifier via <see cref="LogExecutionContext.TrySetTraceIdentifier"/> and storing the injected service dependencies required by the controller.
    /// </summary>
    /// <param name="pumpService">The <see cref="IPumpService"/> used by the controller to interact with pump operations.</param>
    /// <param name="historicalConfigChangesService">The <see cref="IHistoricalConfigChangesService"/> used by the controller to record and query historical configuration changes.</param>
    /// <!-- aidoc:v1 sig=fbfa66c body=a0865cc -->
    public PumpController(IPumpService pumpService, IHistoricalConfigChangesService historicalConfigChangesService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _pumpService = pumpService;
        _historicalConfigChangesService = historicalConfigChangesService;
    }

    /// <summary>
    /// Processes an incoming pump request by constructing an <see cref="ApiRequest"/> from the provided DTO and persisting it,
    /// using a synchronous save when the request indicates a wait-for-result, and an asynchronous save otherwise.
    /// </summary>
    /// <param name="pumpDto">The pump data transfer object received in the request body, containing the patient number, location, pump observations, message time, and wait result flag.</param>
    /// <returns>An <see cref="IActionResult"/> representing an HTTP 200 OK response.</returns>
    /// <!-- aidoc:v1 sig=e6d26d1 body=f8ed0cd -->
    [HttpPost("request")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public IActionResult ProcessRequest([FromBody] PumpDto pumpDto)
    {
        var request = new ApiRequest
        {
            TraceId = Guid.NewGuid().ToString(),
            Type = "AlarisPump",
            PatientNumber = pumpDto.PatientNumber,
            Location = pumpDto.Location,
            PumpObservations = pumpDto.PumpObservations,
            MessageTime = pumpDto.MessageTime,
            WaitResult = pumpDto.WaitResult
        };


        if (request.WaitResult) _pumpService.SaveRequest(request);

        else _pumpService.SaveRequestAsync(request);
        return Ok();
    }

    /// <summary>
    /// Retrieves all pump configuration records as an HTTP 200 OK response.
    /// Requires the caller to have the "AuthSome" role and "DisplayId" permission on the "Source" resource.
    /// </summary>
    /// <returns>An <see cref="IActionResult"/> containing the result of <c>_pumpService.GetAllPumpConfig()</c>.</returns>
    /// <!-- aidoc:v1 sig=f0b0fe2 body=ef4e339 -->
    [HttpGet]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetAllPumpConfig()
    {
        return Ok(await _pumpService.GetAllPumpConfig());
    }

    /// <summary>
    /// Retrieves the pump configuration identified by the specified identifier, restricted to callers with the AuthSome role and DisplayId permission on the Source resource.
    /// </summary>
    /// <param name="id">The unique identifier of the pump configuration to retrieve.</param>
    /// <returns>An HTTP 200 response containing the pump configuration that matches the specified identifier.</returns>
    /// <!-- aidoc:v1 sig=93c59a0 body=de31f9a -->
    [HttpGet("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetPumpConfigById(string id)
    {
        return Ok(await _pumpService.GetPumpConfigsById(id));
    }

    /// <summary>
    /// Updates an existing pump configuration and records the change in the historical log.
    /// Validates the incoming configuration payload, retrieving the previous configuration to enable historical tracking of changes.
    /// </summary>
    /// <param name="configPumps">The pump configuration data to update, provided in the request body.</param>
    /// <returns>An <see cref="IActionResult"/> containing the updated pump configuration wrapped in an Ok response.</returns>
    /// <exception cref="BadRequestException">Thrown when the <paramref name="configPumps"/> parameter is null.</exception>
    /// <!-- aidoc:v1 sig=793e330 body=06d12b7 -->
    [HttpPut]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> UpdatePumpConfig([FromBody] ConfigPumps configPumps)
    {
        if (configPumps == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var oldConfig = await _pumpService.GetPumpConfigsById(configPumps.Id);

        var result = await _pumpService.UpdatePumpConfig(configPumps);
        await LogHistoricalConfigChanged(result, oldConfig);
        return Ok(result);
    }

    /// <summary>
    /// Inserts a new pump configuration record after validating the request payload.
    /// Requires the caller to have the appropriate role and source-related permission.
    /// </summary>
    /// <param name="config">The pump configuration data supplied in the request body to be inserted.</param>
    /// <returns>An <see cref="IActionResult"/> containing the outcome of the pump configuration insertion.</returns>
    /// <exception cref="BadRequestException">Thrown when <paramref name="config"/> is null, indicating that required parameters are missing.</exception>
    /// <!-- aidoc:v1 sig=5c5e235 body=a33665f -->
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> InsertPumpConfig([FromBody] ConfigPumps config)
    {
        if (config == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var result = await _pumpService.InsertPumpConfig(config);
        await LogHistoricalConfigChanged(config, null);
        return Ok(result);
    }

    /// <summary>
    /// Deletes a pump configuration identified by the specified id, retrieving it first, removing it, and logging the historical change after the deletion.
    /// </summary>
    /// <param name="id">The identifier of the pump configuration to delete.</param>
    /// <returns>An <see cref="IActionResult"/> containing the result of the deletion operation.</returns>
    /// <!-- aidoc:v1 sig=af97ca3 body=61abe4f -->
    [HttpDelete("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> DeletePumpConfig(string id)
    {
        var config = await _pumpService.GetPumpConfigsById(id);
        var result = await _pumpService.DeletePumpConfig(config!);

        await LogHistoricalConfigChanged(null, config);
        return Ok(result);
    }

    /// <summary>
    /// Retrieves a paginated list of pumps based on the supplied filter configuration.
    /// </summary>
    /// <param name="config">The pagination filter that defines the paging parameters for the pump query.</param>
    /// <returns>An <see cref="IActionResult"/> containing the paginated pump result.</returns>
    /// <exception cref="BadRequestException">Thrown when the <paramref name="config"/> parameter is null.</exception>
    /// <!-- aidoc:v1 sig=43f409f body=d32ce39 -->
    [HttpPost("paginated")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> PaginatedPump([FromBody] PaginationFilter config)
    {
        if (config == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var result = await _pumpService.GetPaginatedPump(config);
        return Ok(result);
    }


    /// <summary>
    /// Logs a historical change record for pump configuration updates by capturing the current user (or remote IP as fallback) and serializing the new and previous pump configurations to JSON.
    /// </summary>
    /// <param name="newConfigO">The new pump configuration to be logged, or <see langword="null"/> if not applicable.</param>
    /// <param name="oldConfig">The previous pump configuration to be logged, or <see langword="null"/> if not applicable.</param>
    /// <!-- aidoc:v1 sig=88e20b9 body=f2a9b56 -->
    private async Task LogHistoricalConfigChanged(ConfigPumps? newConfigO, ConfigPumps? oldConfig)
    {
        var user = HttpContext.User.Identity?.Name ?? HttpContext.Connection.RemoteIpAddress?.ToString();
        await _historicalConfigChangesService.LogConfigChanged(user!, DisplayConfigEnums.ConfigTypes.PumpCfg,
            newConfigO?.ToJson() ?? string.Empty, oldConfig?.ToJson() ?? string.Empty);
    }
}