using Marco.Pms.Model.Dtos.Directory;
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 DirectoryController : ControllerBase
    {

        private readonly IDirectoryService _directoryService;
        private readonly UserHelper _userHelper;
        private readonly ILoggingService _logger;
        private readonly ISignalRService _signalR;
        private readonly Guid tenantId;

        public DirectoryController(IDirectoryService directoryHelper, UserHelper userHelper, ILoggingService logger, ISignalRService signalR)
        {
            _directoryService = directoryHelper;
            _userHelper = userHelper;
            _logger = logger;
            tenantId = userHelper.GetTenantId();
            _signalR = signalR;
        }
        #region =================================================================== Contact APIs ===================================================================

        #region =================================================================== Contact Get APIs ===================================================================

        [HttpGet("list")]
        public async Task<IActionResult> GetContactList([FromQuery] string? searchString, [FromQuery] string? filter, [FromQuery] Guid? projectId, [FromQuery] bool active = true,
            [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 20)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetListOfContactsAsync(search: searchString, filter: filter, projectId: projectId, active: active, pageSize: pageSize, pageNumber: pageNumber, tenantId, loggedInEmployee);

            return StatusCode(response.StatusCode, response);

        }

        [HttpGet]
        public async Task<IActionResult> GetContactList([FromQuery] string? search, [FromQuery] List<Guid>? bucketIds, [FromQuery] List<Guid>? categoryIds, [FromQuery] Guid? projectId, [FromQuery] bool active = true)
        {
            ContactFilterDto filterDto = new ContactFilterDto
            {
                BucketIds = bucketIds,
                CategoryIds = categoryIds
            };
            var response = await _directoryService.GetListOfContactsOld(search, active, filterDto, projectId);


            return StatusCode(response.StatusCode, response);
        }

        [HttpGet("contact-bucket/{bucketId}")]
        public async Task<IActionResult> GetContactsListByBucketId(Guid bucketId)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetContactsListByBucketIdAsync(bucketId, tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpGet("profile/{id}")]
        public async Task<IActionResult> GetContactProfile(Guid id)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetContactProfileAsync(id, tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpGet("organization")]
        public async Task<IActionResult> GetOrganizationList()
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetOrganizationListAsync(tenantId, loggedInEmployee);
            return Ok(response);
        }

        [HttpGet("designations")]
        public async Task<IActionResult> GetDesignationList()
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetDesignationListAsync(tenantId, loggedInEmployee);
            return Ok(response);
        }

        [HttpGet("contact/filter")]
        public async Task<IActionResult> GetContactFilterObject()
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetContactFilterObjectAsync(tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        #endregion

        [HttpPost]
        public async Task<IActionResult> CreateContact([FromBody] CreateContactDto createContact)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.CreateContactAsync(createContact, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateContact(Guid id, [FromBody] UpdateContactDto updateContact)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.UpdateContactAsync(id, updateContact, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteContact(Guid id, [FromQuery] bool active = false)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.DeleteContactAsync(id, active, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory", Response = id };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }
        #endregion

        #region =================================================================== Contact Notes APIs ===================================================================

        [HttpGet("notes")]
        public async Task<IActionResult> GetListOFAllNotes([FromQuery] Guid? projectId, [FromQuery] string? searchString, [FromQuery] string? filter, [FromQuery] int pageSize = 20, [FromQuery] int pageNumber = 1)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetListOFAllNotesAsync(projectId, searchString, filter, pageSize, pageNumber, tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpPost("note")]
        public async Task<IActionResult> CreateContactNote([FromBody] CreateContactNoteDto noteDto)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.CreateContactNoteAsync(noteDto, tenantId, loggedInEmployee); ;
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Notes", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpGet("notes/{ContactId}")]
        public async Task<IActionResult> GetNoteListByContactId(Guid contactId, [FromQuery] bool active = true)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetNoteListByContactIdAsync(contactId, active, tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpGet("notes/filter")]
        public async Task<IActionResult> GetContactNotesFilterObject()
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetContactNotesFilterObjectAsync(tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpPut("note/{id}")]
        public async Task<IActionResult> UpdateContactNote(Guid id, [FromBody] UpdateContactNoteDto noteDto)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.UpdateContactNoteAsync(id, noteDto, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Notes", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpDelete("note/{id}")]
        public async Task<IActionResult> DeleteContactNote(Guid id, [FromQuery] bool active = false)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.DeleteContactNoteAsync(id, active, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Notes", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        #endregion

        #region =================================================================== Bucket APIs ===================================================================

        [HttpGet("buckets")]
        public async Task<IActionResult> GetBucketList()
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.GetBucketListAsync(tenantId, loggedInEmployee);
            return StatusCode(response.StatusCode, response);
        }

        [HttpPost("bucket")]
        public async Task<IActionResult> CreateBucket([FromBody] CreateBucketDto bucketDto)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.CreateBucketAsync(bucketDto, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Buckets", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);

        }

        [HttpPut("bucket/{id}")]
        public async Task<IActionResult> UpdateBucket(Guid id, [FromBody] UpdateBucketDto bucketDto)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.UpdateBucketAsync(id, bucketDto, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Buckets", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpPost("assign-bucket/{bucketId}")]
        public async Task<IActionResult> AssignBucket(Guid bucketId, [FromBody] List<AssignBucketDto> assignBuckets)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.AssignBucketAsync(bucketId, assignBuckets, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Buckets", Response = response.Data };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        [HttpDelete("bucket/{id}")]
        public async Task<IActionResult> DeleteBucket(Guid id)
        {
            var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
            var response = await _directoryService.DeleteBucketAsync(id, tenantId, loggedInEmployee);
            if (response.Success)
            {
                var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Directory_Buckets", Response = id };
                await _signalR.SendNotificationAsync(notification);
            }
            return StatusCode(response.StatusCode, response);
        }

        #endregion
    }
}