using AutoMapper;
using ConstructorApp.BusinessLayer.Abstract;
using ConstructorApp.DtoLayer.ContactUsDto;
using ConstructorApp.EntityLayer.Entities;
using Microsoft.AspNetCore.Mvc;

namespace ConstructorAppApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ContactUsController : ControllerBase
    {
        private readonly IContactUsService _contactUsService;
        private readonly IMapper _mapper;

        public ContactUsController(IContactUsService contactUsService, IMapper mapper)
        {
            _contactUsService = contactUsService;
            _mapper = mapper;
        }

        [HttpGet]
        public IActionResult ContactUsList()
        {
            var value = _contactUsService.TGetListAll();
            var result = _mapper.Map<List<ResultContactUsDto>>(value);
            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> CreateContactUsAsync(CreateContactUsDto createContactUsDto)
        {
            if (ModelState.IsValid)
            {
                ContactUs contactus = new ContactUs()
                {
                    NameSurname = createContactUsDto.NameSurname,
                    Mail = createContactUsDto.Mail,
                    Phone = createContactUsDto.Phone,
                    MessageContent = createContactUsDto.MessageContent,
                    Date = DateTime.Now,
                    Status = false
                };
                _contactUsService.TAdd(contactus);

                return Ok(new { message = "İletişim Bilgisi Başarılı Bir Şekilde Eklendi" });
            }
            return BadRequest(ModelState);
        }

        [HttpDelete("{id}")]
        public IActionResult DeleteContactUs(int id)
        {
            var value = _contactUsService.TGetByID(id);
            if (value != null)
            {
                _contactUsService.TDelete(value);
                return Ok("İletişim Bilgisi Silindi");
            }
            return NotFound("İletişim Bilgisi Bulunamadı");
        }

        [HttpPut]
        public IActionResult UpdateContactUs(UpdateContactUsDto updateContactUsDto)
        {
            var value = _mapper.Map<ContactUs>(updateContactUsDto);
            _contactUsService.TUpdate(value);
            return Ok("İletişim Alanı Güncellendi");
        }

        [HttpGet("{id}")]
        public IActionResult GetContactUs(int id)
        {
            var value = _contactUsService.TGetByID(id);
            if (value != null)
            {
                var result = _mapper.Map<ResultContactUsDto>(value);
                return Ok(result);
            }
            return NotFound("İletişim Bilgisi Bulunamadı");
        }

        [HttpGet("CountAll")]
        public IActionResult CountAll()
        {
            return Ok(_contactUsService.TCountAll());
        }

        [HttpGet("CountByStatusPending")]
        public IActionResult CountByStatusPending()
        {
            return Ok(_contactUsService.TCountByStatusPending());
        }

        [HttpGet("MarkAsRead/{id}")]
        public IActionResult MarkAsRead(int id)
        {
            _contactUsService.TMarkAsRead(id);
            return Ok("Gelen Mesaj Okundu");
        }
    }
}
