from django.contrib.auth import authenticate
from rest_framework.authtoken.models import Token
from rest_framework_simplejwt.tokens import RefreshToken


class Admin:
    def get_status_action(self):
        # Tu lógica de status que ya tenías
        return {"status": "ok", "service": "Admin Infrastructure"}

    
    def obtener_token_action(self, params):
        """
        Capa Action: Valida credenciales y genera un par de tokens JWT.
        """
        username = params.get('username')
        password = params.get('password')

        # 1. Autenticación
        user = authenticate(username=username, password=password)
        
        if user is not None:
            # 2. Generación de JWT (Access & Refresh)
            refresh = RefreshToken.for_user(user)
            
            return {
                'refresh': str(refresh),
                'access': str(refresh.access_token),
                'user': user.username,
                'status': 'success'
            }
        
        return None
    