"""Services Resource."""

from typing import Any, Dict, List

from netorca_sdk.v2.base import BaseResource
from netorca_sdk.v2.models.service import Service


class ServicesResource(BaseResource):
    """Services resource."""

    endpoint = "services"
    model_class = Service

    def validate(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Validate service data.

        Args:
            data: Service data to validate

        Returns:
            Validation response
        """
        return self.client.post(self.endpoint, data, custom_path="services/validate")

    def submit(self, data: Dict[str, Any]) -> Service:
        """
        Submit a service definition.

        This creates or updates a service in the NetOrca platform.

        Args:
            data: Service definition data

        Returns:
            Service instance
        """
        response_data = self.client.post(self.endpoint, data)
        return self._to_model(response_data)

    def get_docs(self, service_id: int) -> Dict[str, Any]:
        """
        Get service documentation.

        Args:
            service_id: Service ID

        Returns:
            Documentation data
        """
        return self.client.get_one(self.endpoint, service_id, custom_path=f"services/{service_id}/docs")

    def create_docs(self, service_id: int, file_path: str, upload_filename: str) -> Dict[str, Any]:
        """
        Upload service documentation (README/markdown file).

        Args:
            service_id: Service ID
            file_path: Path to markdown file on local filesystem
            upload_filename: Name to use for uploaded file (e.g., "service_name.md")

        Returns:
            Response data from upload

        Example:
            >>> client = NetOrcaClient(fqdn="...", api_key="...")
            >>> client.services.create_docs(
            ...     service_id=123,
            ...     file_path="/path/to/README.md",
            ...     upload_filename="my_service.md"
            ... )
        """
        docs_url = self.client._build_url(self.endpoint, id=service_id, custom_path=f"services/{service_id}/docs")

        response = self.client.auth.upload_file(
            url=docs_url,
            file_path=file_path,
            upload_filename=upload_filename,
            field_name="md_file",
            content_type="text/markdown",
        )

        return self.client._handle_response(response, self.endpoint)

    def get_tags(self) -> List[str]:
        """
        Get available service tags.

        Returns:
            List of tag names
        """
        response = list(self.client.get(self.endpoint, custom_path="services/tags"))
        # Assuming response contains tags list
        return response if response else []

    def get_dependant(self) -> List[Dict[str, Any]]:
        """
        Get dependent services.

        Returns:
            List of dependent service data
        """
        results = []
        for item in self.client.get(self.endpoint, custom_path="services/dependant"):
            results.append(item)
        return results
