import pytest
from apps.organizations.models import Organization


@pytest.mark.django_db
class TestOrganizationModel:
    def test_create_group(self):
        org = Organization.objects.create(name='示例集团', email='group@example.com')
        assert org.parent is None
        assert org.is_active is True

    def test_create_subsidiary(self):
        parent = Organization.objects.create(name='示例集团', email='group@example.com')
        child = Organization.objects.create(
            name='子公司A', email='a@example.com', parent=parent
        )
        assert child.parent == parent

    def test_list_subsidiaries(self):
        parent = Organization.objects.create(name='集团', email='g@example.com')
        Organization.objects.create(name='子A', email='a@example.com', parent=parent)
        Organization.objects.create(name='子B', email='b@example.com', parent=parent)
        assert parent.children.count() == 2


@pytest.mark.django_db
class TestOrganizationPublicAPI:
    def test_public_list_returns_top_level_orgs(self, client):
        parent = Organization.objects.create(name='集团', email='g@example.com')
        Organization.objects.create(name='子A', email='a@example.com', parent=parent)
        response = client.get('/api/organizations/public/')
        assert response.status_code == 200
        # 只返回顶级组织（集团），不直接返回子公司
        assert response.data['count'] == 1
        assert response.data['results'][0]['name'] == '集团'

    def test_inactive_org_not_in_public_list(self, client):
        Organization.objects.create(name='停用集团', email='inactive@example.com', is_active=False)
        Organization.objects.create(name='启用集团', email='active@example.com', is_active=True)
        response = client.get('/api/organizations/public/')
        assert response.status_code == 200
        assert response.data['count'] == 1

    def test_manage_requires_auth(self, client):
        response = client.get('/api/organizations/manage/')
        assert response.status_code == 401


@pytest.mark.django_db
class TestOrganizationManageAPI:
    def test_superadmin_can_create_org(self, client):
        from django.contrib.auth import get_user_model
        from rest_framework_simplejwt.tokens import RefreshToken
        User = get_user_model()
        superadmin = User.objects.create_user(
            username='super1', password='pass123', role='superadmin'
        )
        refresh = RefreshToken.for_user(superadmin)
        token = str(refresh.access_token)
        response = client.post(
            '/api/organizations/manage/',
            {'name': '新集团', 'email': 'new@example.com'},
            content_type='application/json',
            HTTP_AUTHORIZATION=f'Bearer {token}'
        )
        assert response.status_code == 201

    def test_seeker_cannot_access_manage(self, client):
        from django.contrib.auth import get_user_model
        from rest_framework_simplejwt.tokens import RefreshToken
        User = get_user_model()
        seeker = User.objects.create_user(
            username='seeker1', password='pass123', role='seeker'
        )
        refresh = RefreshToken.for_user(seeker)
        token = str(refresh.access_token)
        response = client.get(
            '/api/organizations/manage/',
            HTTP_AUTHORIZATION=f'Bearer {token}'
        )
        assert response.status_code == 403
