using LSFE.Domain.Contracts;
using LSFE.Domain.Helpers;
using LSFE.Infrastructure.Dto.Reports;
using Microsoft.AspNetCore.Mvc;

namespace LSFE.API.Controllers
{
    public class ReportsController : BaseController
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IWebHostEnvironment _env;
        public ReportsController(IUnitOfWork unitOfWork, IWebHostEnvironment env) : base(unitOfWork, env)
        {
            _unitOfWork = unitOfWork;
            _env = env;
        }
        #region Get
        [HttpGet("GetCallRateReport")]
        public async Task<IActionResult> GetCallRateReport([FromQuery] ReportsDto dto)
        {
            var report = await _unitOfWork.Reports.GetCallRateReport(dto);
            return Ok(report);
        }
        [HttpGet("GetCallReachReport")]
        public async Task<IActionResult> GetCallReachReport([FromQuery] ReportsDto dto)
        {
            var report = await _unitOfWork.Reports.GetCallReachReport(dto);
            return Ok(report);
        }
        [HttpGet("GetCatchUpCallReport")]
        public async Task<IActionResult> GetCatchUpCallReport([FromQuery] ReportsDto dto)
        {
            var report = await _unitOfWork.Reports.GetCatchUpCallReport(dto);
            return Ok(report);
        }
        [HttpGet("GetActualCoverageReport")]
        public async Task<IActionResult> GetActualCoverageReport([FromQuery] ReportsDto dto)
        {
            var report = await _unitOfWork.Reports.GetActualCoverageReport(dto);
            return Ok(report);
        }
        [HttpGet("GetDoctorSignature/{fileName}")]
        [ResponseCache(Duration = 3600)] 
        public async Task<IActionResult> GetDoctorSignature(string fileName)
        {
            try
            {
                // Validate filename
                if (string.IsNullOrWhiteSpace(fileName) || fileName.Contains(".."))
                {
                    return BadRequest("Invalid file name");
                }

                var uploadsPath = Path.Combine(_env.WebRootPath, "Content/Uploads", "Signature");
                var filePath = Path.Combine(uploadsPath, fileName);

                ContentTypeHelper.ValidateFile(filePath, uploadsPath);

                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                var contentType = ContentTypeHelper.GetContentType(fileName);

                Response.Headers.Append("Content-Disposition", $"inline; filename=\"{fileName}\"");
                return File(fileStream, contentType, enableRangeProcessing: true);
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Error retrieving file");
            }
        }
        #endregion

        #region POST PUT
        [HttpPost("PutMissedCall")]
        public async Task<IActionResult> PutMissedCall([FromBody] ReportsDto dto)
        {
            if (dto == null)
                return BadRequest("Invalid reason data.");

            bool response = await _unitOfWork.Reports.PutMissedCall(dto);
            return response ? Ok(response) : BadRequest(response);
        }
        #endregion
    }
}
