# 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 typing import Any, ClassVar, Literal, overload
from collections.abc import Callable, Generator

from openstack import proxy
from openstack import resource
from openstack.workflow.v2 import cron_trigger as _cron_trigger
from openstack.workflow.v2 import execution as _execution
from openstack.workflow.v2 import workflow as _workflow


class Proxy(proxy.Proxy):
    api_version: ClassVar[Literal['2']] = '2'

    _resource_registry = {
        "execution": _execution.Execution,
        "workflow": _workflow.Workflow,
    }

    def create_workflow(self, **attrs: Any) -> _workflow.Workflow:
        """Create a new workflow from attributes

        :param attrs: Keyword arguments which will be used to create
            a :class:`~openstack.workflow.v2.workflow.Workflow`,
            comprised of the properties on the Workflow class.

        :returns: The results of workflow creation
        """
        return self._create(_workflow.Workflow, **attrs)

    def update_workflow(
        self, workflow: str | _workflow.Workflow, **attrs: Any
    ) -> _workflow.Workflow:
        """Update workflow from attributes

        :param workflow: The value can be either the name of a workflow or a
            :class:`~openstack.workflow.v2.workflow.Workflow`
            instance.
        :param attrs: Keyword arguments which will be used to update
            a :class:`~openstack.workflow.v2.workflow.Workflow`,
            comprised of the properties on the Workflow class.

        :returns: The results of workflow update
        """
        return self._update(_workflow.Workflow, workflow, **attrs)

    def get_workflow(
        self, workflow: str | _workflow.Workflow
    ) -> _workflow.Workflow:
        """Get a workflow

        :param workflow: The value can be the name of a workflow or
            :class:`~openstack.workflow.v2.workflow.Workflow` instance.

        :returns: One :class:`~openstack.workflow.v2.workflow.Workflow`
        :raises: :class:`~openstack.exceptions.NotFoundException` when no
            workflow matching the name could be found.
        """
        return self._get(_workflow.Workflow, workflow)

    def workflows(
        self,
        **query: Any,
    ) -> Generator[_workflow.Workflow, None, None]:
        """Retrieve a generator of workflows

        :param query: Optional query parameters to be sent to
            restrict the workflows to be returned. Available parameters
            include:

            * limit: Requests at most the specified number of items be
              returned from the query.
            * marker: Specifies the ID of the last-seen workflow. Use the
              limit parameter to make an initial limited request and use
              the ID of the last-seen workflow from the response as the
              marker parameter value in a subsequent limited request.

        :returns: A generator of workflow instances.
        """
        return self._list(_workflow.Workflow, **query)

    # TODO(stephenfin): This method should return None
    def delete_workflow(
        self, value: str | _workflow.Workflow, ignore_missing: bool = True
    ) -> _workflow.Workflow | None:
        """Delete a workflow

        :param value: The value can be either the name of a workflow or a
            :class:`~openstack.workflow.v2.workflow.Workflow`
            instance.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will
            be raised when the workflow does not exist.
            When set to ``True``, no exception will be set when
            attempting to delete a nonexistent workflow.

        :returns: The deleted workflow.
        """
        return self._delete(
            _workflow.Workflow, value, ignore_missing=ignore_missing
        )

    @overload
    def find_workflow(
        self,
        name_or_id: str,
        ignore_missing: Literal[False],
    ) -> _workflow.Workflow: ...

    @overload
    def find_workflow(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
    ) -> _workflow.Workflow | None: ...

    def find_workflow(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
    ) -> _workflow.Workflow | None:
        """Find a single workflow

        :param name_or_id: The name or ID of an workflow.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will be
            raised when the resource does not exist.
            When set to ``True``, None will be returned when
            attempting to find a nonexistent resource.
        :returns: One :class:`~openstack.compute.v2.workflow.Extension` or
            None
        """
        return self._find(
            _workflow.Workflow, name_or_id, ignore_missing=ignore_missing
        )

    def create_execution(self, **attrs: Any) -> _execution.Execution:
        """Create a new execution from attributes

        :param workflow_name: The name of target workflow to execute.
        :param attrs: Keyword arguments which will be used to create
            a :class:`~openstack.workflow.v2.execution.Execution`,
            comprised of the properties on the Execution class.

        :returns: The results of execution creation
        """
        return self._create(_execution.Execution, **attrs)

    def get_execution(
        self, execution: str | _execution.Execution
    ) -> _execution.Execution:
        """Get a execution

        :param execution: The value can be either the ID of a execution or a
            :class:`~openstack.workflow.v2.execution.Execution` instance.

        :returns: One :class:`~openstack.workflow.v2.execution.Execution`
        :raises: :class:`~openstack.exceptions.NotFoundException` when no
            execution matching the criteria could be found.
        """
        return self._get(_execution.Execution, execution)

    def executions(
        self,
        **query: Any,
    ) -> Generator[_execution.Execution, None, None]:
        """Retrieve a generator of executions

        :param query: Optional query parameters to be sent to
            restrict the executions to be returned. Available parameters
            include:

            * limit: Requests at most the specified number of items be
              returned from the query.
            * marker: Specifies the ID of the last-seen execution. Use the
              limit parameter to make an initial limited request and use
              the ID of the last-seen execution from the response as the
              marker parameter value in a subsequent limited request.

        :returns: A generator of execution instances.
        """
        return self._list(_execution.Execution, **query)

    # TODO(stephenfin): This method should return None
    def delete_execution(
        self, value: str | _execution.Execution, ignore_missing: bool = True
    ) -> _execution.Execution | None:
        """Delete an execution

        :param value: The value can be either the name of a execution or a
            :class:`~openstack.workflow.v2.execute.Execution`
            instance.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will be
            raised when the execution does not exist.
            When set to ``True``, no exception will be set when
            attempting to delete a nonexistent execution.

        :returns: The deleted execution.
        """
        return self._delete(
            _execution.Execution, value, ignore_missing=ignore_missing
        )

    @overload
    def find_execution(
        self,
        name_or_id: str,
        ignore_missing: Literal[False],
    ) -> _execution.Execution: ...

    @overload
    def find_execution(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
    ) -> _execution.Execution | None: ...

    def find_execution(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
    ) -> _execution.Execution | None:
        """Find a single execution

        :param name_or_id: The name or ID of an execution.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will be
            raised when the resource does not exist.
            When set to ``True``, None will be returned when
            attempting to find a nonexistent resource.
        :returns: One :class:`~openstack.compute.v2.execution.Execution` or
            None
        """
        return self._find(
            _execution.Execution, name_or_id, ignore_missing=ignore_missing
        )

    def create_cron_trigger(self, **attrs: Any) -> _cron_trigger.CronTrigger:
        """Create a new cron trigger from attributes

        :param attrs: Keyword arguments which will be used to create
            a :class:`~openstack.workflow.v2.cron_trigger.CronTrigger`,
            comprised of the properties on the CronTrigger class.

        :returns: The results of cron trigger creation
        """
        return self._create(_cron_trigger.CronTrigger, **attrs)

    def get_cron_trigger(
        self, cron_trigger: str | _cron_trigger.CronTrigger
    ) -> _cron_trigger.CronTrigger:
        """Get a cron trigger

        :param cron_trigger: The value can be the name of a cron_trigger or
            :class:`~openstack.workflow.v2.cron_trigger.CronTrigger` instance.

        :returns: One :class:`~openstack.workflow.v2.cron_trigger.CronTrigger`
        :raises: :class:`~openstack.exceptions.NotFoundException` when no
            cron triggers matching the criteria could be found.
        """
        return self._get(_cron_trigger.CronTrigger, cron_trigger)

    def cron_triggers(
        self,
        *,
        all_projects: bool = False,
        **query: Any,
    ) -> Generator[_cron_trigger.CronTrigger, None, None]:
        """Retrieve a generator of cron triggers

        :param all_projects: When set to ``True``, list cron triggers from
            all projects. Admin-only by default.
        :param query: Optional query parameters to be sent to
            restrict the cron triggers to be returned. Available parameters
            include:

            * limit: Requests at most the specified number of items be
              returned from the query.
            * marker: Specifies the ID of the last-seen cron trigger. Use the
              limit parameter to make an initial limited request and use
              the ID of the last-seen cron trigger from the response as the
              marker parameter value in a subsequent limited request.

        :returns: A generator of CronTrigger instances.
        """
        if all_projects:
            query['all_projects'] = True
        return self._list(_cron_trigger.CronTrigger, **query)

    # TODO(stephenfin): This method should return None
    def delete_cron_trigger(
        self,
        value: str | _cron_trigger.CronTrigger,
        ignore_missing: bool = True,
    ) -> _cron_trigger.CronTrigger | None:
        """Delete a cron trigger

        :param value: The value can be either the name of a cron trigger or a
            :class:`~openstack.workflow.v2.cron_trigger.CronTrigger`
            instance.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will be
            raised when the cron trigger does not exist.
            When set to ``True``, no exception will be set when
            attempting to delete a nonexistent cron trigger.

        :returns: The deleted cron trigger.
        """
        return self._delete(
            _cron_trigger.CronTrigger, value, ignore_missing=ignore_missing
        )

    # TODO(stephenfin): Drop 'query' parameter or apply it consistently
    @overload
    def find_cron_trigger(
        self,
        name_or_id: str,
        ignore_missing: Literal[False],
        *,
        all_projects: bool = False,
        **query: Any,
    ) -> _cron_trigger.CronTrigger: ...

    @overload
    def find_cron_trigger(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
        *,
        all_projects: bool = False,
        **query: Any,
    ) -> _cron_trigger.CronTrigger | None: ...

    def find_cron_trigger(
        self,
        name_or_id: str,
        ignore_missing: bool = True,
        *,
        all_projects: bool = False,
        **query: Any,
    ) -> _cron_trigger.CronTrigger | None:
        """Find a single cron trigger

        :param name_or_id: The name or ID of a cron trigger.
        :param ignore_missing: When set to ``False``
            :class:`~openstack.exceptions.NotFoundException` will be raised
            when the resource does not exist. When set to ``True``, None will
            be returned when attempting to find a nonexistent resource.
        :param all_projects: When set to ``True``, search for cron
            triggers by name across all projects. Note that this will likely
            result in a higher chance of duplicates.
        :param query: Optional query parameters to be sent to limit
            the cron triggers being returned.

        :returns: One :class:`~openstack.compute.v2.cron_trigger.CronTrigger`
            or None
        :raises: :class:`~openstack.exceptions.NotFoundException` when no
            resource can be found.
        :raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
            resources are found.
        """
        return self._find(
            _cron_trigger.CronTrigger,
            name_or_id,
            ignore_missing=ignore_missing,
            all_projects=all_projects,
            **query,
        )

    # ========== Utilities ==========

    def wait_for_status(
        self,
        res: resource.ResourceT,
        status: str,
        failures: list[str] | None = None,
        interval: int | float | None = 2,
        wait: int | None = None,
        attribute: str = 'status',
        callback: Callable[[int], None] | None = None,
    ) -> resource.ResourceT:
        """Wait for the resource to be in a particular status.

        :param session: The session to use for making this request.
        :param resource: The resource to wait on to reach the status. The
            resource must have a status attribute specified via ``attribute``.
        :param status: Desired status of the resource.
        :param failures: Statuses that would indicate the transition
            failed such as 'ERROR'. Defaults to ['ERROR'].
        :param interval: Number of seconds to wait between checks.
        :param wait: Maximum number of seconds to wait for transition.
            Set to ``None`` to wait forever.
        :param attribute: Name of the resource attribute that contains the
            status.
        :param callback: A callback function. This will be called with a single
            value, progress. This is API specific but is generally a percentage
            value from 0-100.

        :returns: The updated resource.
        :raises: :class:`~openstack.exceptions.ResourceTimeout` if the
            transition to status failed to occur in ``wait`` seconds.
        :raises: :class:`~openstack.exceptions.ResourceFailure` if the resource
            transitioned to one of the states in ``failures``.
        :raises: :class:`~AttributeError` if the resource does not have a
            ``status`` attribute
        """
        return resource.wait_for_status(
            self, res, status, failures, interval, wait, attribute, callback
        )

    def wait_for_delete(
        self,
        res: resource.ResourceT,
        interval: int | float | None = 2,
        wait: int | None = 120,
        callback: Callable[[int], None] | None = None,
    ) -> resource.ResourceT:
        """Wait for a resource to be deleted.

        :param res: The resource to wait on to be deleted.
        :param interval: Number of seconds to wait before to consecutive
            checks.
        :param wait: Maximum number of seconds to wait before the change.
        :param callback: A callback function. This will be called with a single
            value, progress, which is a percentage value from 0-100.

        :returns: The resource is returned on success.
        :raises: :class:`~openstack.exceptions.ResourceTimeout` if transition
            to delete failed to occur in the specified seconds.
        """
        return resource.wait_for_delete(self, res, interval, wait, callback)
