using Lada.Framework.Data.Enums;
using Lada.Framework.DTO.Results.Media;
using Lada.Framework.Infrastructures.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Ladaer.BackEnd.Controllers.Medias
{
    /// <summary>
    /// Controller to handle operations related to pictures.
    /// Updated to use UnifiedMediaService from Framework.
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class PictureController : ControllerBase
    {
        private readonly IUnifiedMediaService _unifiedMediaService;

        /// <summary>
        /// Initializes a new instance of the <see cref="PictureController"/> class.
        /// </summary>
        /// <param name="unifiedMediaService">Unified media service from Framework.</param>
        public PictureController(IUnifiedMediaService unifiedMediaService)
        {
            _unifiedMediaService = unifiedMediaService;
        }



        /// <summary>
        /// Uploads a picture for post content.
        /// </summary>
        /// <param name="file">The picture file to upload.</param>
        /// <returns>The result of the upload operation.</returns>
        [HttpPost("UploadPostContentPicture")]
        [Authorize(Roles = "Picture.UploadPostContentPicture")]
        public async Task<IActionResult> UploadPostContentPicture(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file uploaded.");
            }

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.PostContent);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }


        [HttpPost("UploadPostThumbPicture")]
        [Authorize(Roles = "Picture.UploadPostThumbPicture")]
        public async Task<IActionResult> UploadPostThumbPicture()
        {
            var files = Request.Form.Files;
            if (files.Count <= 0) return BadRequest(new { Message = "Kh�ng c� file n�o truy?n v�o!" });
            var file = files[0];
            if (file.Length <= 0) return BadRequest(new { Message = "File truy?n v�o kh�ng c� d? li?u!" });

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.PostThumb);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }
        [HttpPost("UploadJobPostContentPicture")]
        [Authorize(Roles = "Picture.UploadJobPostContentPicture")]
        public async Task<IActionResult> UploadJobPostContentPicture()
        {
            var files = Request.Form.Files;

            if (files.Count <= 0) return BadRequest(new { Message = "Kh�ng c� file n�o truy?n v�o!" });
            var file = files[0];
            if (file.Length <= 0) return BadRequest(new { Message = "File truy?n v�o kh�ng c� d? li?u!" });

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.JobPostContent);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }
        [HttpPost("UploadJobPostThumbPicture")]
        [Authorize(Roles = "Picture.UploadJobPostThumbPicture")]
        public async Task<IActionResult> UploadJobPostThumbPicture()
        {
            var files = Request.Form.Files;
            if (files.Count <= 0) return BadRequest(new { Message = "Kh�ng c� file n�o truy?n v�o!" });
            var file = files[0];
            if (file.Length <= 0) return BadRequest(new { Message = "File truy?n v�o kh�ng c� d? li?u!" });

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.JobPostThumb);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }
        [HttpPost("UploadOrderPicture")]
        [Authorize(Roles = "Picture.UploadOrderPicture")]
        public async Task<IActionResult> UploadOrderPicture()
        {
            var files = Request.Form.Files;
            if (files.Count <= 0) return BadRequest(new { Message = "Kh�ng c� file n�o truy?n v�o!" });
            var file = files[0];
            if (file.Length <= 0) return BadRequest(new { Message = "File truy?n v�o kh�ng c� d? li?u!" });

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.Order);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }


        [HttpPost("UploadProductThumbPicture")]
        [Authorize(Roles = "Picture.UploadProductThumbPicture")]
        public async Task<IActionResult> UploadProductThumbPicture()
        {
            var files = Request.Form.Files;
            if (files.Count <= 0) return BadRequest(new { Message = "Kh�ng c� file n�o truy?n v�o!" });
            var file = files[0];
            if (file.Length <= 0) return BadRequest(new { Message = "File truy?n v�o kh�ng c� d? li?u!" });

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.ProductThumb);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }

        [HttpPost("UploadProductContentPicture")]
        [Authorize(Roles = "Picture.UploadProductContentPicture")]
        public async Task<IActionResult> UploadProductContentPicture(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file uploaded.");
            }

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.ProductContent);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }
        [HttpPost("UploadTopicContentPicture")]
        [Authorize(Roles = "Picture.UploadTopicContentPicture")]
        public async Task<IActionResult> UploadTopicContentPicture(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file uploaded.");
            }

            var result = await _unifiedMediaService.ProcessImageAsync(file, ImageType.TopicContent);
            return Ok(new UploadPictureResult
            {
                Id = result.MediaId ?? Guid.Empty,
                Url = result.Url,
                Result = result.Result,
                Message = result.Message
            });
        }


    }
}
