# tests/unit/models/database/test_team.py
"""Unit tests for StoreUser and Role database models."""

import pytest

from app.modules.tenancy.models import Role, StoreUser


@pytest.mark.unit
@pytest.mark.database
class TestRoleModel:
    """Test Role model."""

    def test_role_creation(self, db, test_store):
        """Test Role model creation."""
        role = Role(
            store_id=test_store.id,
            name="Manager",
            permissions=["products.create", "orders.view"],
        )
        db.add(role)
        db.commit()
        db.refresh(role)

        assert role.id is not None
        assert role.store_id == test_store.id
        assert role.name == "Manager"
        assert "products.create" in role.permissions
        assert "orders.view" in role.permissions

    def test_role_default_permissions(self, db, test_store):
        """Test Role model with default empty permissions."""
        role = Role(
            store_id=test_store.id,
            name="Viewer",
        )
        db.add(role)
        db.commit()
        db.refresh(role)

        assert role.permissions == [] or role.permissions is None

    def test_role_store_relationship(self, db, test_store):
        """Test Role-Store relationship."""
        role = Role(
            store_id=test_store.id,
            name="Admin",
            permissions=["*"],
        )
        db.add(role)
        db.commit()
        db.refresh(role)

        assert role.store is not None
        assert role.store.id == test_store.id


@pytest.mark.unit
@pytest.mark.database
class TestStoreUserModel:
    """Test StoreUser model."""

    def test_store_user_creation(self, db, test_store, test_user):
        """Test StoreUser model for team management."""
        # Create a role
        role = Role(
            store_id=test_store.id,
            name="Manager",
            permissions=["products.create", "orders.view"],
        )
        db.add(role)
        db.commit()

        # Create store user
        store_user = StoreUser(
            store_id=test_store.id,
            user_id=test_user.id,
            role_id=role.id,
            is_active=True,
        )
        db.add(store_user)
        db.commit()
        db.refresh(store_user)

        assert store_user.id is not None
        assert store_user.store_id == test_store.id
        assert store_user.user_id == test_user.id
        assert store_user.role.name == "Manager"
        assert "products.create" in store_user.role.permissions

    def test_store_user_relationships(self, db, test_store, test_user):
        """Test StoreUser relationships."""
        role = Role(
            store_id=test_store.id,
            name="Staff",
            permissions=["orders.view"],
        )
        db.add(role)
        db.commit()

        store_user = StoreUser(
            store_id=test_store.id,
            user_id=test_user.id,
            role_id=role.id,
            is_active=True,
        )
        db.add(store_user)
        db.commit()
        db.refresh(store_user)

        assert store_user.store is not None
        assert store_user.user is not None
        assert store_user.role is not None
        assert store_user.store.store_code == test_store.store_code
        assert store_user.user.email == test_user.email

    def test_store_user_with_active_flag(self, db, test_store, test_user):
        """Test StoreUser is_active field."""
        role = Role(
            store_id=test_store.id,
            name="Default",
            permissions=[],
        )
        db.add(role)
        db.commit()

        # Create with explicit is_active=True
        store_user = StoreUser(
            store_id=test_store.id,
            user_id=test_user.id,
            role_id=role.id,
            is_active=True,
        )
        db.add(store_user)
        db.commit()
        db.refresh(store_user)

        assert store_user.is_active is True
