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

import pathlib
from unittest import mock

import pytest
import pytest_mock

from hotpocket_backend.apps.ui import tasks as tasks_module
from hotpocket_backend_testing.dto.ui import PocketImportSaveSpec
from hotpocket_backend_testing.services.saves import (
    AssociationsTestingService,
    SaveProcessorTestingService,
    SavesTestingService,
)


@pytest.fixture
def mock_saves_process_save_task_apply_async(mocker: pytest_mock.MockerFixture,
                                             async_result,
                                             ) -> mock.Mock:
    return SaveProcessorTestingService().mock_process_save_task_apply_async(
        mocker=mocker, async_result=async_result,
    )


@pytest.fixture
def pocket_csv_file_path(tmp_path, account, pocket_csv_content):
    result = tmp_path / f'{account.pk}-part_000000.csv'

    with open(result, 'wb') as result_f:
        result_f.write(pocket_csv_content.encode('utf-8'))

    return result


@pytest.mark.django_db
def test_ok(account,
            pocket_csv_file_path: pathlib.Path,
            pocket_import_created_save_spec: PocketImportSaveSpec,
            save_out,
            association_out,
            other_account_save_out,
            pocket_import_other_account_save_spec: PocketImportSaveSpec,
            pocket_import_banned_netloc_save_spec: PocketImportSaveSpec,
            pocket_import_invalid_url_spec: PocketImportSaveSpec,
            mock_saves_process_save_task_apply_async: mock.Mock,
            ):
    # When
    result = tasks_module.import_from_pocket(
        job='test',
        account_uuid=account.pk,
        csv_path=str(pocket_csv_file_path),
    )

    # Then
    assert len(result) == 4

    created_save_pk, created_save_association_pk = result[0]
    SavesTestingService().assert_created(
        pk=created_save_pk,
        account_uuid=account.pk,
        url=pocket_import_created_save_spec.url,
        is_netloc_banned=False,
        title=pocket_import_created_save_spec.title,
    )
    AssociationsTestingService().assert_imported(
        pk=created_save_association_pk,
        account_uuid=account.pk,
        target_uuid=created_save_pk,
        created_at=pocket_import_created_save_spec.time_added,
    )

    reused_save_and_association_save_pk, reused_save_and_association_association_pk = result[1]
    assert reused_save_and_association_save_pk == save_out.pk
    assert reused_save_and_association_association_pk == association_out.pk
    SavesTestingService().assert_reused(
        pk=reused_save_and_association_save_pk,
        reference=save_out,
    )
    AssociationsTestingService().assert_reused(
        pk=reused_save_and_association_association_pk,
        reference=association_out,
    )

    reused_other_account_save_pk, reused_other_account_save_association_pk = result[2]
    assert reused_other_account_save_pk == other_account_save_out.pk
    SavesTestingService().assert_reused(
        pk=reused_other_account_save_pk,
        reference=other_account_save_out,
    )
    AssociationsTestingService().assert_imported(
        pk=reused_other_account_save_association_pk,
        account_uuid=account.pk,
        target_uuid=reused_other_account_save_pk,
        created_at=pocket_import_other_account_save_spec.time_added,
    )

    banned_netloc_save_pk, banned_netloc_save_association_pk = result[3]
    SavesTestingService().assert_created(
        pk=banned_netloc_save_pk,
        account_uuid=account.pk,
        url=pocket_import_banned_netloc_save_spec.url,
        is_netloc_banned=True,
        title=pocket_import_banned_netloc_save_spec.title,
    )
    AssociationsTestingService().assert_imported(
        pk=banned_netloc_save_association_pk,
        account_uuid=account.pk,
        target_uuid=banned_netloc_save_pk,
        created_at=pocket_import_banned_netloc_save_spec.time_added,
    )

    mock_saves_process_save_task_apply_async.assert_not_called()

    assert pocket_csv_file_path.exists() is False
