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

import datetime
import uuid

from hotpocket_soa.dto.associations import AssociationOut
from hotpocket_soa.dto.celery import AsyncResultOut
from hotpocket_soa.dto.saves import ImportedSaveIn, SaveIn, SaveOut
from hotpocket_soa.services import (
    AssociationsService,
    BotService,
    SaveProcessorService,
    SavesService,
)


class SaveWorkflow:
    def __init__(self):
        self.saves_service = SavesService()
        self.bot_service = BotService()
        self.save_processor_service = SaveProcessorService()
        self.associations_service = AssociationsService()

    def create(self,
               account_uuid: uuid.UUID,
               save: ImportedSaveIn | SaveIn,
               ) -> SaveOut:
        if save.is_netloc_banned is None:
            save.is_netloc_banned = self.bot_service.is_netloc_banned(
                url=save.url,
            )

        return self.saves_service.create(
            account_uuid=account_uuid,
            save=save,
        )

    def associate(self,
                  account_uuid: uuid.UUID,
                  target: SaveOut,
                  pk: uuid.UUID | None = None,
                  created_at: datetime.datetime | None = None,
                  ) -> AssociationOut:
        return self.associations_service.create(
            account_uuid=account_uuid,
            target=target,
            pk=pk,
            created_at=created_at,
        )

    def schedule_processing(self, save: SaveOut) -> AsyncResultOut:
        return self.save_processor_service.schedule_process_save(  # noqa: F841
            save=save,
        )
