# LocalStack Resource Provider Scaffolding {{ scaffolding_version }}

from localstack.services.cloudformation.resource_provider import (
    OperationStatus,
    ProgressEvent,
    ResourceRequest,
)
{%- if pro %}
{%- set root_module = "localstack.pro.core" %}
{%- else %}
{%- set root_module = "localstack" %}
{%- endif %}
from {{ root_module }}.services.{{ service }}.resource_providers.generated.aws_{{ service }}_{{ lower_resource }}_base import (
    {{ resource }}ProviderBase,
    {{ resource }}Properties,
    REPEATED_INVOCATION
)

class {{ resource }}Provider({{ resource }}ProviderBase):

    def create(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Create a new resource.

        {% if primary_identifier -%}
        Primary identifier fields:
        {%- for property in primary_identifier %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if required_properties -%}
        Required properties:
        {%- for property in required_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if create_only_properties -%}
        Create-only properties:
        {%- for property in create_only_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if read_only_properties -%}
        Read-only properties:
        {%- for property in read_only_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if create_permissions -%}
        IAM permissions required:
        {%- for permission in create_permissions %}
          - {{ permission }}
        {%- endfor -%}
        {%- endif %}
        """
        model = request.desired_state

        # TODO: validations

        if not request.custom_context.get(REPEATED_INVOCATION):
            # this is the first time this callback is invoked
            # TODO: defaults
            # TODO: idempotency
            # TODO: actually create the resource
            request.custom_context[REPEATED_INVOCATION] = True
            return ProgressEvent(
                status=OperationStatus.IN_PROGRESS,
                resource_model=model,
                custom_context=request.custom_context,
            )

        # TODO: check the status of the resource
        # - if finished, update the model with all fields and return success event:
        #   return ProgressEvent(status=OperationStatus.SUCCESS, resource_model=model)
        # - else
        #   return ProgressEvent(status=OperationStatus.IN_PROGRESS, resource_model=model)

        raise NotImplementedError

    def read(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Fetch resource information

        {% if read_permissions -%}
        IAM permissions required:
        {%- for permission in read_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError

    def delete(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Delete a resource

        {% if delete_permissions -%}
        IAM permissions required:
        {%- for permission in delete_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError

    def update(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Update a resource

        {% if update_permissions -%}
        IAM permissions required:
        {%- for permission in update_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError

    def list(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        List available resources of this type
        {% if list_permissions -%}
        IAM permissions required:
        {%- for permission in list_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError
