using Back.Services.Warehouse;
using Back.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs.Warehouse;
using Shared.Enums;
using Back.Validations.Warehouse.Remittance;

namespace Back.Controllers.Warehouse
{
    [Route("api/[controller]")]
    [ApiController]
    public class RemittanceController : ControllerBase
    {
        private readonly RemittanceService _treanceService;
        private readonly servUser _servUser;

        private readonly ADDValidation _addvalidation;
        private readonly UpdateValidation _updatevalidation;
        private readonly DeleteValidation _deletevalidation;

        public RemittanceController(RemittanceService treanceService, servUser servUser, ADDValidation addvalidation, UpdateValidation updatevalidation, DeleteValidation deletevalidation)
        {
            _treanceService = treanceService;
            _servUser = servUser;
            _addvalidation = addvalidation;
            _updatevalidation = updatevalidation;
            _deletevalidation = deletevalidation;
        }

        [HttpGet("{ItemID}")]
        public async Task<IActionResult> Get(int ItemID)
        {
            var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
            var UserID = claim.Value;
            var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
            var CompanyID = user.RolUsers.First().CompanyID;
            return Ok(await _treanceService.Get(ItemID, CompanyID));
        }
        [HttpGet("AllRemittance")]
        public async Task<IActionResult> GetAll(TypeRemittance? Type = null, int CODID = 0)
        {
            var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
            var UserID = claim.Value;
            var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
            var CompanyID = user.RolUsers.First().CompanyID;
            return Ok(await _treanceService.GetAll(CompanyID, Type, CODID));
        }
        [HttpPost("ADD")]
        public async Task<IActionResult> Add(RemittanceDto item)
        {
            var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
            var UserID = claim.Value;
            var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
            var CompanyID = user.RolUsers.First().CompanyID;

            //-----Validaton
            var resultValidationmodel = await _addvalidation.ValidateAsync(Tuple.Create(item, CompanyID));
            if (!resultValidationmodel.IsValid)
                return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());


            var newmodel = await _treanceService.ADD(item);
            if (newmodel != null) return Ok(newmodel);
            else return BadRequest(new List<string> { "خطا در صدور" });
        }

        [HttpPut("Update")]
        public async Task<IActionResult> Update(RemittanceDto item)
        {
            var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
            var UserID = claim.Value;
            var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
            var CompanyID = user.RolUsers.First().CompanyID;

            //-----Validaton
            var resultValidationmodel = await _updatevalidation.ValidateAsync(Tuple.Create(item, CompanyID));
            if (!resultValidationmodel.IsValid)
                return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());

            var Updatemodel = await _treanceService.Update(item);
            if (Updatemodel != null) return Ok(Updatemodel);
            else return BadRequest(new List<string> { "خطا در ویرایش" });
        }
        [HttpDelete("{ItemID}")]
        public async Task<IActionResult> Delete(int ItemID)
        {
            // validate Delete
            var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
            var UserID = claim.Value;
            var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
            var CompanyID = user.RolUsers.First().CompanyID;

            //-----Validaton
            var resultValidationmodel = await _deletevalidation.ValidateAsync(Tuple.Create(ItemID, CompanyID));
            if (!resultValidationmodel.IsValid)
                return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());

            var Deletemodel = await _treanceService.Delete(ItemID, CompanyID);
            if (Deletemodel) return Ok();
            else return BadRequest(new List<string> { "خطا در حذف" });
        }
    }
}
