using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


/// <summary>
/// Medication administration recording and lookup.
/// </summary>
[ApiController]
[Produces("application/json")]
[Authorize]
public class MedicationsController : ControllerBase
{
    private readonly IMedicationService _medications;

    public MedicationsController(IMedicationService medications) => _medications = medications;

    /// <summary>
    /// Records a medication administration for an encounter.
    /// </summary>
    /// <param name="encounterId">Encounter id.</param>
    /// <param name="req">Medication administration details.</param>
    /// <returns>The created medication administration record.</returns>
    [HttpPost("api/v1/encounters/{encounterId:guid}/medications")]
    [AuthorizePermission(ClinicalPermissions.MedicationsWrite)]
    [ProducesResponseType(typeof(ApiResponse<MedicationAdministration>), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status409Conflict)]
    public async Task<IActionResult> Create(Guid encounterId, [FromBody] CreateMedicationAdministrationRequest req)
    {
        var med = await _medications.CreateAsync(encounterId, req);
        return StatusCode(201, ApiResponse<MedicationAdministration>.Created(med));
    }

    /// <summary>
    /// Lists medication administrations for an encounter with optional time filter.
    /// </summary>
    /// <param name="encounterId">Encounter id.</param>
    /// <param name="since">Optional ISO 8601 cutoff — only returns administrations at or after this time.</param>
    /// <param name="page">Page number (1-based).</param>
    /// <param name="pageSize">Results per page.</param>
    /// <returns>A paginated list of medication administrations.</returns>
    [HttpGet("api/v1/encounters/{encounterId:guid}/medications")]
    [AuthorizePermission(ClinicalPermissions.EncountersRead)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    public async Task<IActionResult> ListByEncounter(
        Guid encounterId,
        [FromQuery] DateTimeOffset? since,
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20)
    {
        var result = await _medications.ListByEncounterAsync(encounterId, since, page, pageSize);
        return Ok(ApiResponse<object>.Ok(new
        {
            items = result.Items,
            page = result.Page,
            pageSize = result.PageSize,
            totalCount = result.TotalCount,
            totalPages = result.TotalPages
        }));
    }

    /// <summary>
    /// Gets a single medication administration by id.
    /// </summary>
    /// <param name="id">Medication administration id.</param>
    /// <returns>The medication administration record.</returns>
    [HttpGet("api/v1/medications/{id:guid}")]
    [AuthorizePermission(ClinicalPermissions.EncountersRead)]
    [ProducesResponseType(typeof(ApiResponse<MedicationAdministration>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Get(Guid id)
    {
        var med = await _medications.GetByIdAsync(id);
        return Ok(ApiResponse<MedicationAdministration>.Ok(med));
    }
}
