using Marco.Pms.Model.Dtos.ServiceProject;
using Marco.Pms.Model.Employees;
using Marco.Pms.Model.Utilities;
using Marco.Pms.Services.Service.ServiceInterfaces;
using MarcoBMS.Services.Helpers;
using MarcoBMS.Services.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Marco.Pms.Services.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class ServiceProjectController : Controller
    {
        private readonly IServiceProject _serviceProject;
        private readonly UserHelper _userHelper;
        private readonly ILoggingService _logger;
        private readonly ISignalRService _signalR;
        private readonly Guid tenantId;
        public ServiceProjectController(IServiceProject serviceProject, UserHelper userHelper, ILoggingService logger, ISignalRService signalR)
        {
            _serviceProject = serviceProject;
            _userHelper = userHelper;
            _logger = logger;
            _signalR = signalR;
            tenantId = userHelper.GetTenantId();

        }


        [HttpPost("create")]
        public async Task<IActionResult> CreateProject([FromBody] ServiceProjectDto serviceProject)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
            }
            Employee loggedInEmploee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _serviceProject.CreateServiceProjectAsync(serviceProject, loggedInEmploee, tenantId);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmploee.Id, Keyword = "Service_Project", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);


        }

        [HttpGet("list")]
        public async Task<IActionResult> GetServiceProjectList([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 20)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
            }
            Employee loggedInEmploee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _serviceProject.GetServiceProjectListAsync(pageNumber, pageSize, loggedInEmploee, tenantId);
            return StatusCode(response.StatusCode, response);

        }

        [HttpGet("details/{id}")]
        public async Task<IActionResult> GetServiceProjectDetails(Guid id)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
            }
            Employee loggedInEmploee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _serviceProject.GetServiceProjectDetailsAsync(id, loggedInEmploee, tenantId);
            return StatusCode(response.StatusCode, response);

        }

        [HttpPut("edit/{id}")]
        public async Task<IActionResult> UpdateServicecProject(Guid id, [FromBody] ServiceProjectDto serviceProject)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
            }
            Employee loggedInEmploee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _serviceProject.UpdateServiceProjectAsync(id, serviceProject, loggedInEmploee, tenantId);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmploee.Id, Keyword = "Service_Project", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);

        }

        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeActivateServiceProject(Guid id, bool isActive = false)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
            }
            Employee loggedInEmploee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _serviceProject.DeActivateServiceProjectAsync(id, isActive, loggedInEmploee, tenantId);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmploee.Id, Keyword = "Service_Project", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

    }
}
