using CrazyStudio.Core.Common.Eitities.Page;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XPrint.Image.Tools;
using XPrintServer.Business.Services;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Api.Controllers.Common
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ModelController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ModelService _modelService;
        private readonly ActionLogService _actionLogService;

        public ModelController(IHttpContextAccessor httpContextAccessor, ModelService modelService, ActionLogService actionLogService)
        {
            _httpContextAccessor = httpContextAccessor;
            _modelService = modelService;
            _actionLogService = actionLogService;
        }

        [HttpPost]
        public async Task<ActionResult> AddOrUpdateModel([FromBody] Model model)
        {
            try
            {
                await _modelService.AddOrUpdateModel(model);
                return Ok(model);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "AddOrUpdateModel Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        [HttpPost]
        public async Task<ActionResult> ModelList([FromBody] PageData pageData)
        {
            try
            {
                var modelList = await _modelService.GetModelList(pageData);
                return Ok(modelList);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "ModelList Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        [HttpPost]
        public async Task<ActionResult> ModelById([FromBody] Guid Id)
        {
            try
            {
                var model = await _modelService.GetModel(Id);
                return Ok(model);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "ModelById Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }
    }
}
