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

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

        public ProjectController(IProjectService projectService, IMapper mapper)
        {
            _projectService = projectService;
            _mapper = mapper;
        }

        [HttpGet]
        public IActionResult ProjectList()
        {
            var value = _projectService.TGetListAll();
            var result = _mapper.Map<List<ResultProjectDto>>(value);
            return Ok(result);
        }

        [HttpPost]
        public IActionResult CreateProject(CreateProjectDto createProjectDto)
        {
            var value = _mapper.Map<Project>(createProjectDto);
            _projectService.TAdd(value);
            return Ok("Proje Bilgisi Eklendi");
        }

        [HttpDelete("{id}")]
        public IActionResult DeleteProject(int id)
        {
            var value = _projectService.TGetByID(id);
            if (value != null)
            {
                _projectService.TDelete(value);
                return Ok("Proje Bilgisi Silindi");
            }
            return NotFound("Proje Bilgisi Bulunamadı");
        }

        [HttpPut]
        public IActionResult UpdateProject(UpdateProjectDto updateProjectDto)
        {
            var value = _mapper.Map<Project>(updateProjectDto);
            _projectService.TUpdate(value);
            return Ok("Proje Alanı Güncellendi");
        }

        [HttpGet("{id}")]
        public IActionResult GetProject(int id)
        {
            var value = _projectService.TGetByID(id);
            if (value != null)
            {
                var result = _mapper.Map<ResultProjectDto>(value);
                return Ok(result);
            }
            return NotFound("Proe Bilgisi Bulunamadı");
        }

        [HttpGet("ProjectStatusActive/{id}")]
        public IActionResult ProjectStatusActive(int id)
        {
            _projectService.TProjectStatusActive(id);
            return Ok("Proje Aktif Hale Getirildi");
        }

        [HttpGet("ProjectStatusPassive/{id}")]
        public IActionResult ProjectStatusPassive(int id)
        {
            _projectService.TProjectStatusPassive(id);
            return Ok("Proje Pasif Hale Getirildi");
        }

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

        [HttpGet("GetBySlug/{slug}")]
        public IActionResult GetBySlug(string slug)
        {
            var project = _projectService.TGetProjectWithImagesBySlug(slug);
            if (project == null)
                return NotFound();

            var dto = new ResultProjectDto
            {
                ProjectID = project.ProjectID,
                Title = project.Title,
                CoverUrl = project.CoverUrl,
                Date = project.Date,
                Location = project.Location,
                ShortDescription = project.ShortDescription,
                LongDescription = project.LongDescription,
                GoogleMapIFrame = project.GoogleMapIFrame,
                VideoUrl = project.VideoUrl,
                FloorPlanUrl = project.FloorPlanUrl,
                IsActive = project.IsActive,
                Slug = project.Slug,
                ImageUrls = project.ProjectGallery?.Select(g => g.ImageUrl!).ToList()
            };

            return Ok(dto);
        }

    }
}
