# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import re
import pytest
from pathlib import Path
from test_base import TestBase, servicePreparer
from devtools_testutils.aio import recorded_by_proxy_async
from devtools_testutils import RecordedTransport


class TestFilesAsync(TestBase):

    # To run this test, use the following command in the \sdk\ai\azure-ai-projects folder:
    # cls & pytest tests\test_files_async.py::TestFilesAsync::test_files_async -s
    @servicePreparer()
    @recorded_by_proxy_async(RecordedTransport.HTTPX)
    async def test_files_async(self, **kwargs):

        file_purpose = self.test_files_params["file_purpose"]
        test_file_name = self.test_files_params["test_file_name"]

        test_data_dir = Path(__file__).parent.parent / "test_data" / "files"
        test_file_path = test_data_dir / test_file_name

        assert test_file_path.exists(), f"Test file not found: {test_file_path}"

        project_client = self.create_async_client(**kwargs)
        openai_client = project_client.get_openai_client()

        async with project_client:

            print(f"[test_files_async] Create (upload) a file with purpose '{file_purpose}'")
            with open(test_file_path, "rb") as f:
                uploaded_file = await openai_client.files.create(file=f, purpose=file_purpose)
            TestBase.validate_file(uploaded_file, expected_purpose=file_purpose)
            file_id = uploaded_file.id
            print(f"[test_files_async] Uploaded file with ID: {file_id}")

            processed_file = await openai_client.files.wait_for_processing(file_id)
            TestBase.assert_equal_or_not_none(processed_file.status, "processed")
            print(f"[test_files_async] File processed successfully with ID: {file_id}")

            print(f"[test_files_async] Retrieve file metadata by ID: {file_id}")
            retrieved_file = await openai_client.files.retrieve(file_id)
            TestBase.validate_file(retrieved_file, expected_file_id=file_id, expected_purpose=file_purpose)
            print(f"[test_files_async] Retrieved file: {retrieved_file.filename}")

            print(f"[test_files_async] Retrieve file content for ID: {file_id}")
            file_content = await openai_client.files.content(file_id)
            assert file_content is not None
            assert file_content.content is not None
            assert len(file_content.content) > 0
            print(f"[test_files_async] File content retrieved ({len(file_content.content)} bytes)")

            print("[test_files_async] List all files")
            found_uploaded_file = False
            async for file in openai_client.files.list():
                if file.id == file_id:
                    found_uploaded_file = True
                    TestBase.validate_file(file)
                    print(f"[test_files_async] Found uploaded file in list: {file.id}")
                    break
            assert found_uploaded_file, "Uploaded file not found in list"

            print(f"[test_files_async] Delete file with ID: {file_id}")
            deleted_file = await openai_client.files.delete(file_id)
            TestBase.assert_equal_or_not_none(deleted_file.id, file_id)
            assert deleted_file.deleted is True
            print(f"[test_files_async] Successfully deleted file: {deleted_file.id}")
