using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.Queries;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions;
using PARR.Core.Common.Helpers;
using PARR.Core.Repositories.Interfaces.Unit;
using PARR.Domain.Common.Pagination;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.Unit;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Управление значениями атрибутов
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class UnitFieldValueController : BaseApiController
    {
        private readonly IMapper mapper;
        private readonly IUnitFieldValueRepository unitFieldValueService;
        private readonly IUnitInValueRepository unitInValueService;

        public UnitFieldValueController(
            IMapper mapper,
            IUnitFieldValueRepository unitFieldValueService,
            IUnitInValueRepository unitInValueService
            )
        {
            this.mapper = mapper;
            this.unitFieldValueService = unitFieldValueService;
            this.unitInValueService = unitInValueService;
        }

        /// <summary>
        /// Список доступных значений аттрибутов
        /// </summary>
        /// <param name="paginationQuery"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.UnitField.GetAllValues)]
        public async Task<IActionResult> GetAll([FromRoute] Guid fieldId, [FromQuery] PaginationQuery paginationQuery, [FromQuery] UnitFieldValueQuery filter)
        {
            var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);

            //IQueryable<UnitFieldValue> query = unitFieldValueService.Get()
            //                        .Where(t => t.UnitInValues.Any(t => t.FieldId == fieldId))
            //                        .OrderBy(t => t.Value);

            IQueryable<UnitFieldValue> query = unitInValueService.Get()
                                                    .Where(t => t.FieldId == fieldId)
                                                    .Select(t => t.Value)
                                                    .Distinct()
                                                    .OrderBy(t => t!.Value);

            if (!string.IsNullOrEmpty(filter.Mask))
                query = query.Where(t => EF.Functions.Like(t.Value!.ToLower(), SqlHelpers.RegexToLike(filter.Mask)));

            var fieldValuesQuery = unitFieldValueService.GetPage(query, paginationFilter);

            var fieldValues = await fieldValuesQuery.ToListAsync();

            if (!fieldValues.Any())
                return NoContent();

            var fielsValuesResponse = mapper.Map<List<UnitFieldValueResponse>>(fieldValues);
            var paginationResponse = new PagedResponse<UnitFieldValueResponse>(fielsValuesResponse, true).GetPaginatedProps(paginationFilter, query);

            return Ok(paginationResponse);
        }

    }
}
