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

Requires a running ECM server at localhost:4000.

Object hierarchy used in every test:
    folder (Unittest_DMS)
      register (Unittest_DMS_Register)
        subregister (Unittest_DMS_Subregister)
          doc (Unittest_DMS_Document)  ← target for files()
"""

import os
import unittest
from uuid import UUID

from ecmind_blue_client.ecm import ECM
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 TestEcmDmsFilesAsync(unittest.IsolatedAsyncioTestCase):
    """Integration tests for ECMDmsAsync.files() — 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"FilesTest_{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"FilesReg_{uuid}"),
            folder_id=folder_id,
        )
        subregister = await self.ecm.dms.insert_and_get(
            Unittest_DMS_Subregister(Name=f"FilesSub_{uuid}"),
            folder_id=folder_id,
            register_id=register.system.id,
        )
        self.doc = await self.ecm.dms.insert_and_get(
            Unittest_DMS_Document(Name=f"FilesDoc_{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_files_returns_list(self):
        """files() returns a list for a document with an attachment."""
        result = await self.ecm.dms.files(self.doc)

        self.assertIsInstance(result, list)

    async def test_files_not_empty_for_document_with_attachment(self):
        """A document with one attached file yields exactly one file."""
        result = await self.ecm.dms.files(self.doc)

        self.assertEqual(len(result), 1)

    async def test_files_accepts_plain_integer_id(self):
        """files() can be called with a plain integer object ID instead of a model."""
        result = await self.ecm.dms.files(self.doc.system.id)

        self.assertIsInstance(result, list)

    async def test_files_raises_for_model_without_id(self):
        """files() raises ValueError when the model instance has no server ID."""
        doc = Unittest_DMS_Document(Name="no_id")

        with self.assertRaises(ValueError):
            await self.ecm.dms.files(doc)
