#######################################################################################
# Copyright ETSI Contributors and Others.
#
# 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 dataclasses import dataclass

from osm_common.dbbase import DbBase
from osm_common.temporal.activities.base import BaseActivity
from osm_common.temporal.states import VimOperationState, VimState


class UpdateVimState(BaseActivity):
    """
    Changes the state of the VIM itself.  Should be either
    ENABLED or ERROR, however this activity does not validate
    the state as no validation was done in OSM previously.

    Collaborators:
        DB Write:           vim_accounts

    Raises  (Retryable):
        DbException         If the target DB record does not exist or DB is not reachable.

    Activity Lifecycle:
        This activity will not report a heartbeat due to its
        short-running nature.

        As this is a direct DB update, it is not recommended to have
        any specific retry policy
    """

    @dataclass
    class Input:
        """
        Input dataclass for updating VIM state in the DB

        Attributes:
        -----------
        vim_uuid : str
            The UUID of the VIM account as stored in the OSM vim
            collection in Mongo

        operational_state : VimState
            A representation of the operational state (ENABLED or ERROR)
            of the VIM.

        message : str
            Human readable message providing additional details to the
            operational state, such as the error message associated
            with the ERROR operational_state.
        """

        vim_uuid: str
        operational_state: VimState
        message: str

    def __init__(self, db: DbBase):
        super().__init__()
        self.db: DbBase = db

    async def __call__(self, activity_input: Input) -> None:
        raise NotImplementedError()


class UpdateVimOperationState(BaseActivity):
    """
    Changes the state of a VIM operation task.  Should be done to
    indicate progress, or completion of the task itself.

    Collaborators:
        DB Write:           vim_accounts

    Raises  (Retryable):
        DbException         If the target DB record does not exist or DB is not reachable.

    Activity Lifecycle:
        This activity will not report a heartbeat due to its
        short-running nature.

        As this is a direct DB update, it is not recommended to have
        any specific retry policy
    """

    @dataclass
    class Input:
        """
        Input dataclass for updating VIM Operations in the Mongo VIM
        collection.

        Attributes:
        -----------
        vim_uuid : str
            The UUID of the VIM account as stored in the OSM vim
            collection in Mongo

        op_id: str
            The operation (task) id for this workflow.  This is used
            to update the status of the operation in Mongo vim collection.

        op_state : VimOperationState
            A representation of the state of the specified operation id,
            such as COMPLETED, or FAILED.

        message : str
            Human readable message providing additional details to the
            operation state, such as the error message explaining why
            the operation failed.
        """

        vim_uuid: str
        op_id: str
        op_state: VimOperationState
        message: str

    def __init__(self, db: DbBase):
        super().__init__()
        self.db: DbBase = db

    async def __call__(self, activity_input: Input) -> None:
        raise NotImplementedError()


class DeleteVimRecord(BaseActivity):
    """
    Deletes the VIM record from the database.

    Collaborators:
        DB Delete:           vim_accounts

    Raises (Retryable):
        DbException         If the target DB record does not exist or DB is not reachable.

    Activity Lifecycle:
        This activity will not report a heartbeat due to its
        short-running nature.

        As this is a direct DB update, it is not recommended to have
        any specific retry policy
    """

    @dataclass
    class Input:
        """
        Input dataclass for deleting vim record from the database

        Attributes:
        -----------
        vim_uuid : str
            The UUID of the VIM account as stored in the OSM vim
            collection in Mongo

        """

        vim_uuid: str

    def __init__(self, db: DbBase):
        super().__init__()
        self.db: DbBase = db

    async def __call__(self, activity_input: Input) -> None:
        raise NotImplementedError()
