using Microsoft.AspNetCore.Mvc;
using RR3CommunityServer.Models;
using RR3CommunityServer.Services;

namespace RR3CommunityServer.Controllers;

[ApiController]
[Route("product/api/core")]
public class ProductController : ControllerBase
{
    private readonly ICatalogService _catalogService;
    private readonly ILogger<ProductController> _logger;

    public ProductController(ICatalogService catalogService, ILogger<ProductController> logger)
    {
        _catalogService = catalogService;
        _logger = logger;
    }

    [HttpGet("getAvailableItems")]
    public async Task<ActionResult<SynergyResponse<List<CatalogItem>>>> GetAvailableItems()
    {
        _logger.LogInformation("GetAvailableItems request");

        var items = await _catalogService.GetAvailableItems();

        var response = new SynergyResponse<List<CatalogItem>>
        {
            resultCode = 0,
            message = "Success",
            data = items
        };

        return Ok(response);
    }

    [HttpGet("getMTXGameCategories")]
    public async Task<ActionResult<SynergyResponse<List<CatalogCategory>>>> GetCategories()
    {
        _logger.LogInformation("GetMTXGameCategories request");

        var categories = await _catalogService.GetCategories();

        var response = new SynergyResponse<List<CatalogCategory>>
        {
            resultCode = 0,
            message = "Success",
            data = categories
        };

        return Ok(response);
    }

    [HttpPost("getDownloadItemUrl")]
    public async Task<ActionResult<SynergyResponse<object>>> GetDownloadUrl([FromBody] Dictionary<string, string> request)
    {
        var itemId = request.GetValueOrDefault("itemId", "");
        _logger.LogInformation("GetDownloadItemUrl: {ItemId}", itemId);

        var url = await _catalogService.GetDownloadUrl(itemId);

        var response = new SynergyResponse<object>
        {
            resultCode = 0,
            message = "Success",
            data = new { downloadUrl = url }
        };

        return Ok(response);
    }
}
