using Asp.Versioning;
using Google.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OnlineSalesAutoCrop.CoreAPI.Models;
using OnlineSalesAutoCrop.CoreAPI.Models.Global;
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.MobileApp;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.MobileApp;
using System;
using System.Threading.Tasks;

namespace OnlineSalesAutoCrop.CoreAPI.Controllers.Mobile;

/// <summary>
/// 
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <param name="service"></param>
/// <param name="appSettings"></param>
/// <param name="cache"></param>
/// <param name="logger"></param>
[Authorize]
[ApiController]
[ApiVersion("1.0")]
[ValidateAntiForgeryToken]
[Route("api/mobile/v{version:apiVersion}/orders")]
public class OrdersController(IOrderService service, IOptions<AppSettings> appSettings, IEaseCache cache, ILogger<OrdersController> logger) : ControllerBase
{
    private readonly ILogger _logger = logger;
    private readonly IEaseCache _cache = cache;
    private readonly IOrderService _service = service;
    private readonly AppSettings _appSettings = appSettings?.Value;
    private readonly DateTimeOffset _options = Helper.CreateEaseCacheOptions();


    /// <summary>
    /// Login using your credential data retrieve from SqlServer
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <param name="request"></param>
    /// <response code="200">If login successful Return Order List</response>
    [HttpPost("SaveOrder")]
    [IgnoreAntiforgeryToken]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SaveOrderResponse))]
    public async Task<IActionResult> SaveOrders([FromBody] SaveOrderRequest request)
    {
        SaveOrderResponse response = new();
        try
        {
            int userId = HttpContext.User.GetClaimValue<int>(Constants.UserId);
            response = await _service.SaveOrderAsync(request, userId);
            return Ok(MobileResponseBase<SaveOrderResponse>.Success(response));
        }
        catch (Exception ex)
        {
            string msg = $"Exception occur on Save Orders endpoint with Order UUID request - {request?.UUID}";
            _logger.LogError(exception: ex, message: msg);
            return StatusCode(StatusCodes.Status500InternalServerError, MobileResponseBase<AppAuthUserResponse>.Failure(ex.InnerException != null ? ex.InnerException.Message : ex.Message, StatusCodes.Status500InternalServerError));
        }
    }

    /// <summary>
    /// Login using your credential data retrieve from SqlServer
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <param name="request"></param>
    /// <response code="200">If login successful Return Attendance List</response>
    [HttpGet("OrderSummary")]
    [IgnoreAntiforgeryToken]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(GetSalesOrderResponse))]
    public async Task<IActionResult> GetOrderSummaries([FromQuery] GetOrderSummaryRequest request)
    {
        PagedResult<GetSalesOrderResponse> response = new();
        try
        {
            int userId = HttpContext.User.GetClaimValue<int>(Constants.UserId);
            response = await _service.GetSalesOrdersSummary(request, userId);
            return Ok(MobileResponseBase<PagedResult<GetSalesOrderResponse>>.Success(response));
        }
        catch (Exception ex)
        {
            string msg = $"Exception occur on Save Orders endpoint with Order UUID request - {request?.UUID}";
            _logger.LogError(exception: ex, message: msg);
            return StatusCode(StatusCodes.Status500InternalServerError, MobileResponseBase<AppAuthUserResponse>.Failure(ex.InnerException != null ? ex.InnerException.Message : ex.Message, StatusCodes.Status500InternalServerError));
        }
    }
}

