using Fuel.Application.Service;
using Fuel.Core.Nozzle.Dto;
using Fuel.Core.Transactions.Dto;
using FuelServer.Core.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Fuel.PaymentServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class TransactionsController : ControllerBase
    {
        private readonly ITransactionsService _transactionsService;
        public TransactionsController(ITransactionsService transactionsService)
        { 
             _transactionsService = transactionsService;
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="uploadTransactions"></param>
        /// <returns></returns>
        [Route("CreateTransactions")]
        [HttpPost]
        public async Task<IActionResult> CreateTransactionsAsync(UploadTransactions uploadTransactions)
        {
            var serviceResult = await _transactionsService.CreateTransactions(uploadTransactions);
            return Ok(serviceResult);
        }
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Route("GetTransactions")]
        [HttpPost]
        public async Task<IActionResult> GetTransactionsAsync(TransactionsInput input)
        {
            var serviceResult = await _transactionsService.GetTransactionsAsync(input);
            return Ok(serviceResult);
        }
        /// <summary>
        /// 小程序查询未支付订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Route("GetMiniProgramTransactionsUnpaidQuery")]
        [HttpPost]
        public async Task<IActionResult> GetMiniProgramTransactionsUnpaidAsync(TransactionsInput input)
        {
            var serviceResult = await _transactionsService.GetMiniProgramTransactionsUnpaidAsync(input);
            return Ok(serviceResult);
        }
        /// <summary>
        /// 小程序查询已支付订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Route("GetMiniProgramTransactionsPaidQuery")]
        [HttpPost]
        public async Task<IActionResult> GetMiniProgramTransactionsPaidAsync(TransactionsInput input)
        {
            var serviceResult = await _transactionsService.GetMiniProgramTransactionsPaidAsync(input);
            return Ok(serviceResult);
        }


        /// <summary>
        /// 提交支付
        /// </summary>
        /// <param name="trxId"></param>
        /// <param name="AuthCode"></param>
        /// <returns></returns>
        [Route("Payment")]
        [HttpPost]
        public async Task<IActionResult> CommitPayment(int trxId,string AuthCode)
        {
            var serviceResult = await _transactionsService.CommitPayment(trxId, AuthCode);
            return Ok(serviceResult);
        }

        /// <summary>
        /// 退款、退余额
        /// </summary>
        /// <param name="trxId">订单id</param>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        /// <returns></returns>
        [Route("Refund")]
        [HttpPost]
        public async Task<IActionResult> RefundTrx(int trxId, double longitude,double latitude)
        {
            var serviceResult = await _transactionsService.RefundTrx( trxId,  longitude,  latitude);
            return Ok(serviceResult);
        }
        /// <summary>
        /// 核销
        /// </summary>
        /// <param name="trxId"></param>
        /// <param name="OriginalQty">实际加油升数</param>
        /// <returns></returns>
        [Route("Redeem")]
        [HttpPost]
        public async Task<IActionResult> Redeem(int trxId,decimal OriginalQty)
        {
            var serviceResult = await _transactionsService.Redeem(trxId, OriginalQty);
            return Ok(serviceResult);
        }

    }
}
