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.Domain.Models.Filter;
using adas_core.Logging;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;

namespace adas_core.Controllers;

[Route("medicines")]
public class MedicinesController : ControllerBase
{
    private readonly IAdminPanelService _adminPanelService;

    private readonly IMedicineService _medicineService;
    //private readonly ILogger<AdminPanelService> _loggerADMPanel;


    public MedicinesController(IMedicineService medicineService,
        IAdminPanelService adminPanelService) //, ILogger<AdminPanelService> loggerADMPanel)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _medicineService = medicineService;
        _adminPanelService = adminPanelService;
        //this._loggerADMPanel = loggerADMPanel;
    }

    [HttpGet]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetAllMedicines()
    {
        var activeMedicines = await _medicineService.GetAll();
        return Ok(activeMedicines);
    }


    [HttpGet("{patientId}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetActiveMedicines(string patientId)
    {
        if (!ObjectId.TryParse(patientId, out var objectId))
            throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        var activeMedicines = await _medicineService.GetActiveMedicinesByPatient(objectId);
        return Ok(activeMedicines);
    }


    #region ADMPanel

    [HttpPost("paginated")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetPaginatedMedicines([FromBody] PaginationFilter request)
    {
        if (request == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var result = await _medicineService.GetPaginatedMedicines(request);
        return Ok(result);
    }

    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> PostMedicine([FromBody] Medicine medicine)
    {
        if (medicine == null) throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestMissingParameters);
        var insertedMedicine = await _adminPanelService.PostMedicine(medicine);
        return Ok(insertedMedicine);
    }

    [HttpPut("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> UpdateMedicine(string id, [FromBody] Medicine medicine)
    {
        if (!ObjectId.TryParse(id, out var objectId))
            throw new BadRequestException(HttpEnum.ErrorMessage.BadRequestInvalidFormat);
        var med = await _adminPanelService.GetMedicineById(objectId);
        medicine.Id = med!.Id;
        return Ok(await _adminPanelService.UpdateMedicine(medicine));
    }

    [HttpDelete("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> DeleteMedicine(string id)
    {
        await _adminPanelService.DeleteMedicineById(id);
        return Ok();
    }

    [HttpGet("groups")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetAllGroups()
    {
        return Ok(await _medicineService.GetAllGroups());
    }

    [HttpGet("types")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetTypes()
    {
        return Ok(await _medicineService.GetAllTypes());
    }

    [HttpGet("names")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetNames()
    {
        return Ok(await _medicineService.GetAllNames());
    }

    [HttpGet("codes")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public async Task<IActionResult> GetCodes()
    {
        return Ok(await _medicineService.GetAllCodes());
    }

    #endregion
}