using Back.Common;
using Back.Data.Models;
using Back.Services;
using Back.Services.Warehouse;
using Back.Validations.Warehouse.Receipt;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs.Warehouse;
using Shared.Enums;
using System.ComponentModel.Design;

namespace Back.Controllers.Warehouse
{
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class ReceiptController : ControllerBase
    {
        private readonly ReceiptService _receiptService;
        private readonly servUser _servUser;
        private readonly ADDValidation _addvalidation;
        private readonly UpdateValidation _updatevalidation;
        private readonly DeleteValidation _deletevalidation;

        public ReceiptController(ReceiptService receiptService, servUser servUser, ADDValidation addvalidation, UpdateValidation updatevalidation, DeleteValidation deletevalidation)
        {
            _receiptService = receiptService;
            _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 _receiptService.Get(ItemID, CompanyID));
        }
        [HttpGet("AllReceip")]
        public async Task<IActionResult> GetAll(TypeReceipt? 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 _receiptService.GetAll(CompanyID, Type, CODID));
        }
        [HttpPost("ADD")]
        public async Task<IActionResult> AddReceipt(ReceiptDto 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());


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

        [HttpPut("Update")]
        public async Task<IActionResult> UpdateReceipt(ReceiptDto 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 _receiptService.Update(item);
            if (Updatemodel != null) return Ok(Updatemodel);
            else return BadRequest(new List<string> { "خطا در ویرایش" });
        }
        [HttpDelete("{ItemID}")]
        public async Task<IActionResult> DeleteReceipt(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 _receiptService.Delete(ItemID, CompanyID);
            if (Deletemodel) return Ok();
            else return BadRequest(new List<string> { "خطا در حذف" });
        }
    }
}
