# backend/formulas/views/formula.py
from rest_framework.viewsets import ModelViewSet
from formulas.models.formula import Formula
from formulas.serializers.formula import FormulaSerializer
from formulas.permissions import IsAdminOrEditor
from rest_framework.decorators import action
from rest_framework.response import Response
from formulas.serializers import SymbolSerializer

class FormulaViewSet(ModelViewSet):
    queryset = Formula.objects.all()
    serializer_class = FormulaSerializer
    permission_classes = [IsAdminOrEditor]
    lookup_field = "code"
    
    @action(detail=True, methods=['get'], url_path='symbols')
    def get_symbols(self, request, code=None):
        formula = self.get_object()
        symbols = formula.symbols.all()
        serializer = SymbolSerializer(symbols, many=True)
        return Response(serializer.data)            