from django.contrib.auth import get_user_model
from django.test import TestCase

from apps.accounts.models import Organization, Role
from apps.onboarding.models import AgentConfig, AgentInteractionLog, OnboardingFlow, OnboardingSession

User = get_user_model()

class OnboardingModelTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            email_address='onboard@example.com',
            password='pass1234',
            first_name='Onboard',
            last_name='User',
            date_of_birth='1994-04-04',
            is_manager=True,
        )
        self.org = Organization.objects.create(name='Onboard Org', owner=self.user)
        self.org.members.add(self.user)
        self.role = Role.objects.create(name='Operator', organization=self.org)

    def test_agent_config_fields(self):
        config = AgentConfig.objects.create(
            organization=self.org,
            name='Operator Knowledge Agent',
            agent_type='knowledge',
            llm_config={'model_id': 'x'},
            system_prompt='Assist user',
            tool_permissions=['search'],
        )

        self.assertEqual(config.organization, self.org)
        self.assertEqual(config.name, 'Operator Knowledge Agent')
        self.assertEqual(config.agent_type, 'knowledge')
        self.assertEqual(config.llm_config, {'model_id': 'x'})
        self.assertEqual(config.system_prompt, 'Assist user')
        self.assertEqual(config.tool_permissions, ['search'])
        self.assertIsNotNone(config.id)
        self.assertIsNotNone(config.uuid)
        self.assertIsNotNone(config.created_at)
        self.assertIsNotNone(config.updated_at)

    def test_onboarding_session_fields(self):
        session = OnboardingSession.objects.create(
            user=self.user,
            role=self.role,
            status='active',
            state={'progress': 25},
            active_configs={'knowledge': 'enabled'},
        )

        self.assertEqual(session.user, self.user)
        self.assertEqual(session.role, self.role)
        self.assertEqual(session.status, 'active')
        self.assertEqual(session.state, {'progress': 25})
        self.assertEqual(session.active_configs, {'knowledge': 'enabled'})
        self.assertIsNone(session.completed_at)
        self.assertIsNotNone(session.id)
        self.assertIsNotNone(session.uuid)
        self.assertIsNotNone(session.created_at)
        self.assertIsNotNone(session.updated_at)

    def test_agent_interaction_log_fields(self):
        config = AgentConfig.objects.create(
            organization=self.org,
            name='Operator Assistant',
            agent_type='assessment',
        )
        session = OnboardingSession.objects.create(
            user=self.user,
            role=self.role,
            state={},
            active_configs={},
        )
        log = AgentInteractionLog.objects.create(
            session=session,
            agent_config=config,
            sender_type='user',
            content='Hello',
            tool_call_metadata={'page_uuid': 'abc'},
        )

        self.assertEqual(log.session, session)
        self.assertEqual(log.agent_config, config)
        self.assertEqual(log.sender_type, 'user')
        self.assertEqual(log.content, 'Hello')
        self.assertEqual(log.tool_call_metadata, {'page_uuid': 'abc'})
        self.assertIsNotNone(log.id)
        self.assertIsNotNone(log.uuid)
        self.assertIsNotNone(log.created_at)
        self.assertIsNotNone(log.updated_at)

    def test_onboarding_flow_fields(self):
        flow = OnboardingFlow.objects.create(
            title='New Hire Flow',
            role=self.role,
            structure=[{'uuid': 'p1', 'title': 'Intro'}],
            is_active=True,
        )

        self.assertEqual(flow.title, 'New Hire Flow')
        self.assertEqual(flow.role, self.role)
        self.assertEqual(flow.structure, [{'uuid': 'p1', 'title': 'Intro'}])
        self.assertTrue(flow.is_active)
        self.assertIsNotNone(flow.id)
        self.assertIsNotNone(flow.uuid)
        self.assertIsNotNone(flow.created_at)
        self.assertIsNotNone(flow.updated_at)
