#######################################################################################
# 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 typing import List


from osm_common.dbbase import DbBase
from osm_common.temporal.activities.base import BaseActivity
from osm_common.temporal.states import NsState


class GetVnfDetails(BaseActivity):
    """
    Gets the list of VNF record IDs, VNF member-index-refs for a given NS record ID.

    Collaborators:
        DB Read:            vnfrs

    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.

        Since this activity only reads from the DB, it is safe to retry, although
        you may wish to have some back-off policy.
    """

    @dataclass
    class Input:
        """
        Attributes:
        -----------
        ns_uuid : str
            The UUID of the NS from which to retrieve the VNF records.
        """

        ns_uuid: str

    @dataclass
    class Output:
        """
        Attributes:
        -----------
        vnf_details: list[(vnfr_ids: str, vnf_member_index_ref: str), .. ]
            List of tuples including VNF details associated with the NS.
            Tuple(VNF record IDs, vnf_member_index_ref)
        """

        vnf_details: List[tuple]

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

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


class GetNsRecord(BaseActivity):
    """Gets the NS record from Database.

    Collaborators:
        DB Read:     nsrs

    Raises (retryable):
        DbException: If DB read operations fail, the collection or DB record ID does not exist.

    Activity Lifecycle:
        This activity should complete relatively quickly (less than 10
        second).

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

        This is an idempotent activity.

    """

    @dataclass
    class Input:
        """
        Input dataclass for getting NS record activity.

        Attributes:
        -----------
        nsr_uuid :
            The UUID of the NS record which is stored in the OSM nsrs
            collection in Mongo.

        """

        nsr_uuid: str

    @dataclass
    class Output:
        """
        Output dataclass for getting NS record activity.

        Attributes:
        -----------
        nsr : dict
            NS record retrieved from Database..

        """

        nsr: dict

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

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


class UpdateNsState(BaseActivity):
    """
    Changes the state of the NS itself.

    Collaborators:
        DB Write:           nsrs

    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 NS state in the DB

        Attributes:
        -----------
        ns_uuid : str
            The UUID of the NS as stored in the OSM ns
            collection in Mongo

        operational_state : NsState
            A representation of the operational state (ENABLED or ERROR)
            of the NS.

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

        ns_uuid: str
        state: NsState
        message: str

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

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


class DeleteNsRecord(BaseActivity):
    """Delete a NS record from DB.

    Collaborators:
        DB Write:           nsrs

    Raises (retryable):
        DbException: If DB access fails, the collection does not exist.

    Activity Lifecycle:
        This activity should complete relatively quickly (less than a
        second). However, it would be reasonable to wait up to 10
        seconds.

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

        This operation is idempotent.

    """

    @dataclass
    class Input:
        """
        Input dataclass for activity that deletes a NS record.

        Attributes:
        -----------
        ns_uuid : str
            The UUID of the NS to be deleted from the nsrs
            collection in Mongo.
        """

        ns_uuid: str

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

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