using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Modules.Library.WebApi.Models.Views.Dictionary;

namespace Modules.Library.WebApi.Controllers;

[ApiController]
[ApiExplorerSettings(GroupName = "LanguageV1")]
[Route("Dictionaries/Language")]
//[Route("[controller]")]
public class LanguageController : ControllerBase
{
    private readonly IMapper _mapper;
    private readonly IMediator _mediator;
    private readonly ILogger _logger;

    public LanguageController(IMapper mapper, IMediator mediator, ILogger<LanguageController> logger)
    {
        _mapper = mapper;
        _mediator = mediator;
        _logger = logger;
    }

    [HttpGet("List")]
    public async Task<List<Language>> List() => 
        _mapper.Map<List<Language>>(await _mediator.Send(new Application.Queries.Dictionaries.Language.LanguageListQuery()));

    [HttpPost("Create")]
    public async Task<Guid> CreateLanguage([FromQuery] string codeIso3, [FromQuery] string name) =>
        await _mediator.Send(new Application.Commands.Dictionaries.Language.CreateLanguageCommand
        {
            Code = codeIso3,
            Name = name,
        });

    [HttpPost("Edit")]
    public async Task EditLanguage([FromQuery] Guid id, [FromQuery] string? codeIso3, [FromQuery]string? name) =>
        await _mediator.Send(new Application.Commands.Dictionaries.Language.SetLanguageNameCommand
        {
            Id = id,
            Code = codeIso3,
            Name = name,
        });

    [HttpPost("Delete")]
    public async Task DeleteLanguage([FromQuery] Guid id) =>
        await _mediator.Send(new Application.Commands.Dictionaries.Language.DeleteLanguageCommand
        {
            Id = id,
        });
}
