using LSFE.Domain.Contracts;
using LSFE.Domain.Contracts.Doctor;
using LSFE.Domain.Services;
using LSFE.Infrastructure.Dto.Doctor;
using LSFE.Infrastructure.Entities.Doctor;
using Microsoft.AspNetCore.Mvc;

namespace LSFE.API.Controllers
{
    public class DoctorMgmtController : BaseController
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IWebHostEnvironment _env;
        public DoctorMgmtController(IUnitOfWork unitOfWork, IWebHostEnvironment env) : base(unitOfWork, env)
        {
            _unitOfWork = unitOfWork;
            _env = env;
        }

        [HttpPost("PostPutDoctor")]
        public async Task<IActionResult> PostPutDoctor([FromBody] DoctorDto dto)
        {
            if (dto == null)
                return BadRequest("Invalid doctor data.");

            var response = await _unitOfWork.Doctors.PostPutDoctor(dto);
            if (response.Success)
            {
                await _unitOfWork.SaveChangesAsync();
                return Ok(response);
            }
            return BadRequest(response);
        }

        [HttpPost("ApproveDoctor")]
        public async Task<IActionResult> ApproveDoctor([FromBody] DoctorDto dto)
        {
            if (dto == null || dto.Status == null)
                return BadRequest("Invalid approval data.");

            var response = await _unitOfWork.Doctors.ApproveDoctor(dto.DoctorId, dto.Status,dto.UpdatedBy);
            if (response.Success)
            {
                await _unitOfWork.SaveChangesAsync();
                return Ok(response);
            }
            return BadRequest(response);
        }

        [HttpGet("GetPendingApprovals")]
        public async Task<IActionResult> GetPendingApprovals([FromQuery] DoctorDto dto)
        {
            var approvals = await _unitOfWork.Doctors.GetPendingApprovals(dto);
            return Ok(approvals);
        }
        [HttpGet("GetAllDoctor")]
        public async Task<IActionResult> GetAllDoctor([FromQuery] DoctorDto dto)
        {
            var doctors = await _unitOfWork.Doctors.GetAllDoctor(dto);
            return Ok(doctors);
        }
        [HttpGet("GetDoctor")]
        public async Task<IActionResult> GetDoctor([FromQuery] DoctorDto dto)
        {
            var doctors = await _unitOfWork.Doctors.GetDoctor(dto);
            return Ok(doctors);
        }

        [HttpGet("GetMyDoctor")]
        public async Task<IActionResult> GetMyDoctor([FromQuery] DoctorDto dto)
        {
            var doctors = await _unitOfWork.Doctors.GetMyDoctor(dto);
            return Ok(doctors);
        }
        [HttpGet("GetDoctorCoverageToday")]
        public async Task<IActionResult> GetDoctorCoverageToday([FromQuery] DoctorDto dto)
        {
            dto.MRPlanDate = DateTime.Now.ToString("yyyy-MM-dd");
            var doctors = await _unitOfWork.Doctors.GetDoctorCoverageToday(dto);
            return Ok(doctors);
        }
        [HttpDelete("{doctorId:int}")]
        public async Task<IActionResult> SoftDeleteAsync(int doctorId)
        {
            var result = await _unitOfWork.Doctors.SoftDeleteAsync(doctorId);
            return result.Success ? Ok(result) : BadRequest(result);
        }
    }

}
