using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Transactions;
using UnivateProperties_API.Containers.Campaigns;
using UnivateProperties_API.Model.Campaigns;
using UnivateProperties_API.Repository;
using UnivateProperties_API.Repository.Campaigns;

namespace UnivateProperties_API.Controllers.Campaigns
{
    [Route("api/[controller]")]
    [ApiController]
    public class CampaignController : ControllerBase
    {

        private readonly ICampaignRepository _Repo;

        public CampaignController(ICampaignRepository _repo)
        {
            _Repo = _repo;
        }

        [HttpGet]
        public IActionResult Get()
        {
            return new OkObjectResult(_Repo.GetAll());
        }
        
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
        }

        [HttpGet("GetDTO/{id}")]
        public IActionResult GetDTO(int id)
        {
            return new OkObjectResult(_Repo.GetDTO(x => x.Id == id));
        }

        [HttpGet("GetLandingPage/{id}")]
        public IActionResult GetLandingPage(int id)
        {
            return new OkObjectResult(_Repo.GetCampaignHTML(id));
        }

        [HttpGet("GetCampaignItems/{id}")]
        public IActionResult GetCampaignItems(int id)
        {
            return new OkObjectResult(_Repo.GetCampaignItems(id));
        }

        [HttpGet("GetCampaignPlaceHolders/{id}")]
        public IActionResult GetCampaignPlaceHolders(int id)
        {
            return new OkObjectResult(_Repo.GetCampaignPlaceHolders(id));
        }

        [HttpPost]
        public IActionResult Post([FromBody] Campaign campaign)
        {
            using (var scope = new TransactionScope())
            {
                _Repo.Insert(campaign);
                scope.Complete();
                return CreatedAtAction(nameof(Get), new { id = campaign.Id }, campaign);
            }
        }
        
        [HttpPut]
        public IActionResult Put(Campaign campaign)
        {
            if (campaign != null)
            {
                using (var scope = new TransactionScope())
                {
                    _Repo.Update(campaign);
                    scope.Complete();
                    return new OkResult();
                }
            }
            return new NoContentResult();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            _Repo.RemoveAtId(id);
            return new OkResult();
        }
    }
}