using adas_core.Application.Exceptions;
using adas_core.Application.Services.Interfaces;
using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace adas_core.Controllers;

[Route("application")]
[ApiController]
public class ApplicationController : ControllerBase
{
    private readonly IClientMessageService _clientMessageService;

    //Logger<ApplicationController> logger
    //private readonly ILogger<ApplicationController> _loggerADMPanel;
    private readonly IFileService _fileService;
    private readonly ICacheService? _redisService;
    private readonly ISubscriberGroupedService _subscriberGroupedService;
    private readonly ISubscribersService _subscribersService;

    public ApplicationController(
        IFileService fileService,
        ISubscribersService subscribersService,
        ISubscriberGroupedService subscriberGroupedService,
        IClientMessageService clientMessageService,
        ICacheService? redisService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _fileService = fileService;
        _subscribersService = subscribersService;
        _subscriberGroupedService = subscriberGroupedService;
        _clientMessageService = clientMessageService;
        _redisService = redisService;
    }

    
    [HttpGet("subscribers")]
    [AllowAnonymous]
    public IActionResult GetSubscribersVersion()
    {
        return Ok(_subscribersService.GetSubscribers());
    }

    [HttpGet("subscribers-group")]
    [AllowAnonymous]
    public IActionResult GetSubscribersGroup()
    {
        return Ok(_subscriberGroupedService.GetGrouped());
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpPost]
    public IActionResult UpdateApplications([FromBody] List<PatientLocation> boxes)
    {
        _clientMessageService.SendUpdateMessageToBoxes(boxes);
        return Ok();
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpPost("upload")]
    [RequestSizeLimit(300_000_000)]
    public async Task<ActionResult> UploadFile(List<IFormFile> files)
    {
        await _fileService.CopyUpdateFiles(files);
        return Ok();
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpPost("upload/{theme}/{category}/asset")]
    [RequestSizeLimit(300_000_000)]
    public async Task<ActionResult> UploadAssetFile(string theme, List<IFormFile> files)
    {
        var isParsed = Enum.TryParse<FileEnum.AssetTheme>(theme, out var themeParsed);
        if (!isParsed) throw new InvalidFormatException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        await _fileService.UploadAssetFiles(files, themeParsed);

        return Ok();
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpGet("{theme}/{category}/asset")]
    public ActionResult UGetAssetFile(string theme)
    {
        var isParsed = Enum.TryParse<FileEnum.AssetTheme>(theme, out var themeParse);
        if (!isParsed) throw new InvalidFormatException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        return Ok(_fileService.GetAllAssetFile(themeParse));
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpGet("cleaning")]
    public ActionResult CleanCache()
    {
        if (_redisService == null) throw new InvalidFormatException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        _redisService.CleanCache();
        return Ok();
    }
}