"""Integration tests for ECMDmsAsync.digest().

Requires a running ECM server at localhost:4000.
"""

import os
import unittest
from uuid import UUID

from ecmind_blue_client.ecm import ECM, ECMDocumentDigest
from ecmind_blue_client.pool import AsyncPoolClient
from ecmind_blue_client.rpc._job_request_file import JobRequestFileFromPath
from tests.models.Unittest_DMS import (
    Unittest_DMS,
    Unittest_DMS_Document,
    Unittest_DMS_Register,
    Unittest_DMS_Subregister,
)
from tests._test_settings import ECM_SERVERS, ECM_USERNAME, ECM_PASSWORD


class TestEcmDmsDigestAsync(unittest.IsolatedAsyncioTestCase):
    """Integration tests for ECMDmsAsync.digest() — requires localhost:4000."""

    def setUp(self):
        self.client = AsyncPoolClient(servers=ECM_SERVERS, username=ECM_USERNAME, password=ECM_PASSWORD)
        self.ecm = ECM(self.client)

    def tearDown(self):
        del self.client

    async def asyncSetUp(self):
        uuid = UUID(bytes=os.urandom(16), version=4)

        folder_id, folder_type_id = await self.ecm.dms.insert(Unittest_DMS(Name=f"DigestTest_{uuid}"))
        self.folder_id = folder_id
        self.folder_type_id = folder_type_id

        register = await self.ecm.dms.insert_and_get(
            Unittest_DMS_Register(Name=f"DigestReg_{uuid}"),
            folder_id=folder_id,
        )
        subregister = await self.ecm.dms.insert_and_get(
            Unittest_DMS_Subregister(Name=f"DigestSub_{uuid}"),
            folder_id=folder_id,
            register_id=register.system.id,
        )
        self.doc = await self.ecm.dms.insert_and_get(
            Unittest_DMS_Document(Name=f"DigestDoc_{uuid}"),
            files=[JobRequestFileFromPath("README.md")],
            folder_id=folder_id,
            register_id=subregister.system.id,
        )

    async def asyncTearDown(self):
        folder = await self.ecm.dms.get(Unittest_DMS, self.folder_id, self.folder_type_id)
        await self.ecm.dms.delete(folder, hard_delete=True, delete_cascading=True)

    async def test_digest_returns_ecm_document_digest(self):
        result = await self.ecm.dms.digest(self.doc)

        self.assertIsInstance(result, ECMDocumentDigest)

    async def test_digest_table_digest_is_populated(self):
        result = await self.ecm.dms.digest(self.doc)

        self.assertIsNotNone(result.table_digest)
        self.assertIsInstance(result.table_digest, str)

    async def test_digest_local_digest_is_none_without_calculate(self):
        result = await self.ecm.dms.digest(self.doc)

        self.assertIsNone(result.local_digest)

    async def test_digest_calculate_populates_local_digest(self):
        result = await self.ecm.dms.digest(self.doc, calculate=True)

        self.assertIsNotNone(result.local_digest)
        self.assertIsInstance(result.local_digest, str)

    async def test_digest_calculate_table_matches_local(self):
        result = await self.ecm.dms.digest(self.doc, calculate=True)

        self.assertEqual(result.table_digest, result.local_digest)

    async def test_digest_with_explicit_object_type_id(self):
        object_type_id = await self.ecm.dms.get_object_type_by_id(self.doc.system.id)
        result = await self.ecm.dms.digest(self.doc.system.id, object_type_id, calculate=True)

        self.assertEqual(result.table_digest, result.local_digest)
