# coding: utf-8

"""
CUA API

Documentation of API endpoints of CUA

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""  # noqa: E501


import os
import unittest
from pprint import pprint

import robotnikai
from dotenv import load_dotenv
from robotnikai.api.applications_api import ApplicationsApi
from robotnikai.rest import ApiException
from robotnikai.wrapper import API


class TestApplicationsApi(unittest.TestCase):
    """ApplicationsApi unit test stubs"""

    def setUp(self) -> None:
        load_dotenv()

        # Create API client with configuration
        self.api = API()

        # Store test app ID for use across tests
        self.test_app_id = None
        self.test_app_name = "Test Application for API Tests"

    def tearDown(self) -> None:
        # Clean up any test application that was created
        if self.test_app_id:
            try:
                self.api.apps.delete_app(self.test_app_id)
                print(f"Cleaned up test application with ID: {self.test_app_id}")
                self.test_app_id = None
            except ApiException:
                print(
                    f"Warning: Could not delete test application with ID: {self.test_app_id}"
                )
                pass

    def test_apps_create(self) -> None:
        """Test case for apps_create

        Create Application
        """
        # try:
        # Create application object with test data
        application = robotnikai.Application(
            name=self.test_app_name,
        )

        # Call the create API
        api_response = self.api.apps.create_app(application)

        # Store the app ID for use in other tests
        self.test_app_id = api_response.id

        # Assertions to verify the response
        self.assertIsNotNone(api_response)
        self.assertEqual(api_response.name, self.test_app_name)
        self.assertIsNotNone(api_response.id)

        print("Test application created successfully:")
        pprint(api_response)

        # except ApiException as e:
        #     self.fail(f"Exception when calling ApplicationsApi->create_app: {e}")

        return api_response

    def test_apps_retrieve(self) -> None:
        """Test case for apps_retrieve

        Retrieve Application
        """
        # First create a test application if we don't have one
        if not self.test_app_id:
            app = self.test_apps_create()
            self.test_app_id = app.id

        try:
            # Retrieve the application by ID
            api_response = self.api.apps.get_app(self.test_app_id)

            # Verify the response
            self.assertIsNotNone(api_response)
            self.assertEqual(api_response.id, self.test_app_id)
            self.assertEqual(api_response.name, self.test_app_name)

            print("Test application retrieved successfully:")
            pprint(api_response)

        except ApiException as e:
            self.fail(f"Exception when calling ApplicationsApi->get_app: {e}")

    def test_apps_list(self) -> None:
        """Test case for apps_list

        List Applications
        """
        # First create a test application if we don't have one
        if not self.test_app_id:
            app = self.test_apps_create()
            self.test_app_id = app.id

        try:
            # Get list of applications
            api_response = self.api.apps.get_apps()

            # Verify the response
            self.assertIsNotNone(api_response)
            self.assertIsNotNone(api_response.results)
            self.assertIsInstance(api_response.results, list)

            print(
                f"Applications list retrieved successfully, found {len(api_response.results)} applications"
            )

        except ApiException as e:
            self.fail(f"Exception when calling ApplicationsApi->get_apps: {e}")

    def test_apps_update(self) -> None:
        """Test case for apps_update

        Update Application
        """
        # First create a test application if we don't have one
        if not self.test_app_id:
            app = self.test_apps_create()
            self.test_app_id = app.id

        try:
            # Create updated application object
            updated_name = f"{self.test_app_name} - Updated"
            application = robotnikai.Application(
                name=updated_name,
            )

            # Update the application
            api_response = self.api.apps.update_app(self.test_app_id, application)

            # Verify the response
            self.assertIsNotNone(api_response)
            self.assertEqual(api_response.id, self.test_app_id)
            self.assertEqual(api_response.name, updated_name)

            # Update test app name to match the updated value
            self.test_app_name = updated_name

            print("Test application updated successfully:")
            pprint(api_response)

        except ApiException as e:
            self.fail(f"Exception when calling ApplicationsApi->update_app: {e}")

    def test_apps_destroy(self) -> None:
        """Test case for apps_destroy

        Delete Application
        """
        # First create a test application if we don't have one
        if not self.test_app_id:
            app = self.test_apps_create()
            self.test_app_id = app.id

        try:
            # Delete the application
            self.api.apps.delete_app(self.test_app_id)

            # Verify deletion by trying to retrieve it (should raise ApiException)
            try:
                self.api.apps.get_app(self.test_app_id)
                self.fail("Application was not deleted successfully")
            except ApiException as e:
                # Expected 404 Not Found
                self.assertEqual(e.status, 404)

            print(f"Test application deleted successfully: {self.test_app_id}")

            # Clear the test app ID since it's been deleted
            self.test_app_id = None

        except ApiException as e:
            self.fail(f"Exception when calling ApplicationsApi->delete_app: {e}")


if __name__ == "__main__":
    unittest.main()
