# -*- coding: utf-8 -*-
from __future__ import annotations

from bthlabs_jsonrpc_core import register_method
from django.http import HttpRequest
from django.urls import reverse

from hotpocket_backend.apps.core.rpc import wrap_soa_errors
from hotpocket_backend.apps.ui.dto.rpc import SavesCreateOut
from hotpocket_backend.apps.ui.services.workflows import CreateSaveWorkflow


@register_method(method='saves.create')
@wrap_soa_errors
def create(request: HttpRequest, url: str) -> SavesCreateOut:
    association = CreateSaveWorkflow().run_rpc(
        request=request,
        account=request.user,
        url=url,
    )

    result = SavesCreateOut.model_validate({
        'id': association.pk,
        'target_uuid': association.target_uuid,
        'url': request.build_absolute_uri(reverse(
            'ui.associations.view', args=(association.pk,),
        )),
        'is_netloc_banned': association.target.is_netloc_banned,
    })

    return result
