using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SIGCM.Domain.Entities;
using SIGCM.Domain.Interfaces;

namespace SIGCM.API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class AttributeDefinitionsController : ControllerBase
{
    private readonly IAttributeDefinitionRepository _repository;

    public AttributeDefinitionsController(IAttributeDefinitionRepository repository)
    {
        _repository = repository;
    }

    [HttpGet("category/{categoryId}")]
    [AllowAnonymous]
    public async Task<IActionResult> GetByCategoryId(int categoryId)
    {
        var attributes = await _repository.GetByCategoryIdAsync(categoryId);
        return Ok(attributes);
    }

    [HttpPost]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> Create(AttributeDefinition attribute)
    {
        var id = await _repository.AddAsync(attribute);
        attribute.Id = id;
        return Ok(attribute);
    }

    [HttpDelete("{id}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> Delete(int id)
    {
        await _repository.DeleteAsync(id);
        return NoContent();
    }
}
