"""Submissions Resource."""

from typing import Any, Dict, Generator, List, Optional

from netorca_sdk.v2.base import BaseResource
from netorca_sdk.v2.models.submission import Submission
from netorca_sdk.v2.types import SubmissionFilters


class SubmissionsResource(BaseResource):
    """Submissions resource."""

    endpoint = "submissions"
    model_class = Submission

    SUPPORTED_FILTERS = {
        "limit": int,
        "ordering": str,
        "offset": int,
    }

    def list(self, limit: Optional[int] = None, ordering: Optional[str] = None, **filters: Any) -> Generator[Submission, None, None]:
        """
        List submissions with optional filtering.

        Args:
            limit: Maximum number of submissions to return
            ordering: Field to order by (e.g., "created", "-created")
            **filters: Additional filter parameters (see SubmissionFilters)
                - offset: int - Pagination offset

        Yields:
            Submission instances

        Example:
            >>> # Get 10 most recent submissions
            >>> for submission in client.submissions.list(limit=10, ordering="-created"):
            ...     print(submission.id, submission.created)
        """
        params = {}
        if limit is not None:
            params['limit'] = limit
        if ordering is not None:
            params['ordering'] = ordering
        params.update(filters)

        return super().list(**params)

    def validate(self, data: Dict[str, Any], partial: bool = False) -> Dict[str, Any]:
        """
        Validate submission data.

        Args:
            data: Submission data to validate
            partial: Whether this is a partial validation

        Returns:
            Validation response
        """
        if partial:
            return self.client.patch(self.endpoint, id=0, data=data, custom_path="submissions/validate")
        else:
            return self.client.post(self.endpoint, data, custom_path="submissions/validate")

    def submit(self, data: Dict[str, Any], partial: bool = False) -> Submission:
        """
        Submit a submission.

        Args:
            data: Submission data
            partial: Whether this is a partial submission

        Returns:
            Submission instance
        """
        if partial:
            response_data = self.client.patch(self.endpoint, id=0, data=data, custom_path="submissions/submit")
        else:
            response_data = self.client.post(self.endpoint, data, custom_path="submissions/submit")

        return self._to_model(response_data)

    def update(self, id: int, data: Dict[str, Any], partial: bool = True) -> Submission:
        """Submissions cannot be updated directly."""
        raise NotImplementedError("Use validate() or submit() methods instead")

    def delete(self, id: int) -> None:
        """Submissions cannot be deleted."""
        raise NotImplementedError("Submissions cannot be deleted")
