# Copyright 2025 LY Flava
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from abc import ABC
from lib.api_schema.response.flava_services.langfuse.v1_0.langfuse_projects import (
    admin_langfuse_project_list_response_schema,
    admin_langfuse_project_detail_schema,
    error_response_schema
)
from lib.api_schema.response.flava_services.langfuse.v1_0.users import (
    langfuse_user_list_response_schema
)
from lib.services.langfuse.base_client import BaseLangfuseClient
from urllib.parse import urlencode


class FlavaLangfuseAdminClient(BaseLangfuseClient):
    """Flava-specific client for Langfuse Admin operations"""

    def __init__(self, auth_provider, service='langfuse', region=None, **kwargs):
        super(FlavaLangfuseAdminClient, self).__init__(
            auth_provider=auth_provider, service=service, region=region, **kwargs)

    def get_schema(self):
        """Get the response schemas for validation."""
        return {
            'list_all_projects': admin_langfuse_project_list_response_schema,
            'list_projects_by_project': admin_langfuse_project_list_response_schema,
            'show_project_admin': admin_langfuse_project_detail_schema,
            'list_project_users': langfuse_user_list_response_schema,
            'error': error_response_schema
        }

    def list_all_langfuse_projects(self, **kwargs):
        """List all Langfuse projects across all Flava projects (admin only).

        Args:
            **kwargs: Query parameters (projectName, name, createdAtFrom, etc.)

        Returns:
            Response containing list of all Langfuse projects
        """
        uri = "/admin/langfuseProjects"
        if kwargs:
            uri = f"{uri}?{urlencode(kwargs)}"

        resp, body = self.get(uri)
        self.expected_success_codes([200])
        return resp, self.parse_json_response_body(body)

    def list_langfuse_projects_by_project(self, project_name, **kwargs):
        """List Langfuse projects for a specific Flava project (admin only).

        Args:
            project_name: Flava project name
            **kwargs: Query parameters (name, createdAtFrom, etc.)

        Returns:
            Response containing list of Langfuse projects for the project
        """
        uri = f"/admin/projects/{project_name}/langfuseProjects"
        if kwargs:
            uri = f"{uri}?{urlencode(kwargs)}"

        resp, body = self.get(uri)
        self.expected_success_codes([200])
        return resp, self.parse_json_response_body(body)

    def show_langfuse_project_admin(self, project_name, langfuse_project_id):
        """Get details of a Langfuse project (admin only).

        Args:
            project_name: Flava project name
            langfuse_project_id: ID of the Langfuse project

        Returns:
            Response containing detailed Langfuse project information
        """
        uri = f"/admin/projects/{project_name}/langfuseProjects/{langfuse_project_id}"

        resp, body = self.get(uri)
        self.expected_success_codes([200])
        return resp, self.parse_json_response_body(body)

    def list_langfuse_project_users(self, project_name, langfuse_project_id):
        """List users of a Langfuse project (admin only).

        Args:
            project_name: Flava project name
            langfuse_project_id: ID of the Langfuse project

        Returns:
            Response containing list of users
        """
        uri = f"/admin/projects/{project_name}/langfuseProjects/{langfuse_project_id}/users"

        resp, body = self.get(uri)
        self.expected_success_codes([200])
        return resp, self.parse_json_response_body(body)