using Lada.Framework.DTO;
using Lada.Framework.DTO.SmartTable;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Premises;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Premises;
using Ladaer.BackEnd.Infrastructures.DTO.Results.Premises;
using Ladaer.BackEnd.Infrastructures.Services.Premises;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Ladaer.BackEnd.Controllers.Premises
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class PremiseController : ControllerBase
    {
        private readonly IPremiseService _service;

        public PremiseController(IPremiseService service)
        {
            _service = service;
        }

        /// <summary>
        /// Tìm kiếm danh sách mặt bằng
        /// </summary>
        [HttpPost("Search")]
        [Authorize(Roles = "Premise.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var result = await _service.Search(param);
            return Ok(result);
        }

        /// <summary>
        /// Lấy chi tiết mặt bằng theo ID
        /// </summary>
        [HttpGet("GetById/{id}")]
        [Authorize(Roles = "Premise.GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _service.GetById(id);
            return Ok(result);
        }

        /// <summary>
        /// Tạo mới hoặc cập nhật mặt bằng
        /// </summary>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Premise.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromForm] PremiseModel model)
        {
            var result = await _service.CreateOrUpdate(model);
            return Ok(result);
        }

        /// <summary>
        /// Xóa mặt bằng (soft delete)
        /// </summary>
        [HttpDelete("Delete/{id}")]
        [Authorize(Roles = "Premise.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _service.Delete(id);
            return Ok(result);
        }

        /// <summary>
        /// Chuyển trạng thái mặt bằng
        /// </summary>
        [HttpPost("ChangeStatus")]
        [Authorize(Roles = "Premise.ChangeStatus")]
        public async Task<IActionResult> ChangeStatus([FromBody] PremiseStatusModel model)
        {
            var result = await _service.ChangeStatus(model);
            return Ok(result);
        }

        /// <summary>
        /// Cập nhật thông tin tài chính mặt bằng
        /// </summary>
        [HttpPost("UpdateFinancial")]
        [Authorize(Roles = "Premise.UpdateFinancial")]
        public async Task<IActionResult> UpdateFinancial([FromBody] PremiseFinancialModel model)
        {
            var result = await _service.UpdateFinancial(model);
            return Ok(result);
        }

        /// <summary>
        /// Xóa ảnh/media của mặt bằng
        /// </summary>
        [HttpDelete("DeleteMedia/{id}")]
        [Authorize(Roles = "Premise.DeleteMedia")]
        public async Task<IActionResult> DeleteMedia(int id)
        {
            var result = await _service.DeleteMedia(id);
            return Ok(result);
        }
    }
}
