"""
Validate auth from event.
"""
from enum import Enum
from typing import Dict, Optional, List
from optikka_design_data_layer.logger import logger
from optikka_design_data_layer.db.base_postgres_service import BasePostgresService

class Role(Enum):
    """
    Role enum.
    """
    SUPER_ADMIN = "SUPER_ADMIN"
    ADMIN = "ADMIN"
    READ_ONLY = "READ_ONLY"
    READ_WRITE = "READ_WRITE"

class RoleBasedPermissions:
    """
    Role based permissions.
    """
    WRITE_ROUTES: List[str] = []
    READ_ROUTES: List[str] = []
    ADMIN_ROUTES: List[str] = []
    SUPER_ADMIN_ROUTES: List[str] = []
    OPEN_ROUTES: List[str] = []

    def __init__ (
        self,
        WRITE_ROUTES: List[str],
        READ_ROUTES: List[str],
        ADMIN_ROUTES: List[str],
        OPEN_ROUTES: List[str],
        SUPER_ADMIN_ROUTES: List[str]
    ) -> None:
        self.WRITE_ROUTES = WRITE_ROUTES
        self.READ_ROUTES = READ_ROUTES
        self.ADMIN_ROUTES = ADMIN_ROUTES
        self.OPEN_ROUTES = OPEN_ROUTES
        self.SUPER_ADMIN_ROUTES = SUPER_ADMIN_ROUTES

    def is_route_allowed(self, route: str, role: Role) -> bool:
        if route in self.OPEN_ROUTES:
            return True
        elif role == Role.SUPER_ADMIN.value:
            return True
        elif role == Role.ADMIN.value:
            if route in self.ADMIN_ROUTES or route in self.READ_ROUTES or route in self.OPEN_ROUTES:
                return True
            return False
        elif role == Role.READ_WRITE.value:
            if route in self.READ_ROUTES or route in self.OPEN_ROUTES or route in self.WRITE_ROUTES:
                return True
            return False
        elif role == Role.READ_ONLY.value:
            if route in self.READ_ROUTES or route in self.OPEN_ROUTES:
                return True
            return False
        else:
            return False
        

def validate_auth_from_event(event: dict) -> Optional[Dict[str, str]]:
    """
    Validate authentication from event.
    """
    headers = event.get("headers", {})
    user_id = headers.get("x-user-id")
    if user_id is None:
        logger.error("User ID is required")
        return None
    studio_id = headers.get("x-studio-id")
    if studio_id is None:
        logger.error("Studio ID is required")
        return None
    account_id = headers.get("x-account-id")
    if account_id is None:
        logger.error("Account ID is required")
        return None
    role = headers.get("x-role")
    if role is None:
        logger.error("Role ID is required")
        return None

    # Fetch user from database
    username = None
    try:
        user_service = BasePostgresService(table_name="User")
        user_record = user_service.find_by_id(user_id, include_deleted=True)
        if user_record:
            username = user_record.get("username")
        else:
            logger.warning(f"User with id {user_id} not found in database")
    except Exception as e:
        logger.error(f"Error retrieving user data for ID {user_id}: {str(e)}")

    #return user information as a dictionary
    return {
        "user_id": user_id,
        "studio_id": studio_id,
        "account_id": account_id,
        "role": role,
        "user_name": username
    }
