# tests/unit/models/database/test_marketplace_import_job.py
"""Unit tests for MarketplaceImportJob database model."""

import pytest

from app.modules.marketplace.models import MarketplaceImportJob


@pytest.mark.unit
@pytest.mark.database
class TestMarketplaceImportJobModel:
    """Test MarketplaceImportJob model."""

    def test_import_job_creation(self, db, test_user, test_store):
        """Test MarketplaceImportJob model with relationships."""
        import_job = MarketplaceImportJob(
            store_id=test_store.id,
            user_id=test_user.id,
            marketplace="Letzshop",
            source_url="https://example.com/feed.csv",
            status="pending",
            imported_count=0,
            updated_count=0,
            error_count=0,
            total_processed=0,
        )

        db.add(import_job)
        db.commit()
        db.refresh(import_job)

        assert import_job.id is not None
        assert import_job.store_id == test_store.id
        assert import_job.user_id == test_user.id
        assert import_job.marketplace == "Letzshop"
        assert import_job.source_url == "https://example.com/feed.csv"
        assert import_job.status == "pending"
        assert import_job.store.store_code == test_store.store_code
        assert import_job.user.username == test_user.username

    def test_import_job_default_values(self, db, test_user, test_store):
        """Test MarketplaceImportJob default values."""
        import_job = MarketplaceImportJob(
            store_id=test_store.id,
            user_id=test_user.id,
            source_url="https://example.com/feed.csv",
        )

        db.add(import_job)
        db.commit()
        db.refresh(import_job)

        assert import_job.marketplace == "Letzshop"  # Default
        assert import_job.status == "pending"  # Default
        assert import_job.imported_count == 0  # Default
        assert import_job.updated_count == 0  # Default
        assert import_job.error_count == 0  # Default
        assert import_job.total_processed == 0  # Default

    def test_import_job_status_values(self, db, test_user, test_store):
        """Test MarketplaceImportJob with different status values."""
        statuses = [
            "pending",
            "processing",
            "completed",
            "failed",
            "completed_with_errors",
        ]

        for i, status in enumerate(statuses):
            import_job = MarketplaceImportJob(
                store_id=test_store.id,
                user_id=test_user.id,
                source_url=f"https://example.com/feed_{i}.csv",
                status=status,
            )
            db.add(import_job)
            db.commit()
            db.refresh(import_job)

            assert import_job.status == status

    def test_import_job_counts(self, db, test_user, test_store):
        """Test MarketplaceImportJob count fields."""
        import_job = MarketplaceImportJob(
            store_id=test_store.id,
            user_id=test_user.id,
            source_url="https://example.com/feed.csv",
            status="completed",
            imported_count=100,
            updated_count=50,
            error_count=5,
            total_processed=155,
        )

        db.add(import_job)
        db.commit()
        db.refresh(import_job)

        assert import_job.imported_count == 100
        assert import_job.updated_count == 50
        assert import_job.error_count == 5
        assert import_job.total_processed == 155

    def test_import_job_error_message(self, db, test_user, test_store):
        """Test MarketplaceImportJob with error message."""
        import_job = MarketplaceImportJob(
            store_id=test_store.id,
            user_id=test_user.id,
            source_url="https://example.com/feed.csv",
            status="failed",
            error_message="Connection timeout while fetching CSV",
        )

        db.add(import_job)
        db.commit()
        db.refresh(import_job)

        assert import_job.error_message == "Connection timeout while fetching CSV"

    def test_import_job_relationships(self, db, test_user, test_store):
        """Test MarketplaceImportJob relationships."""
        import_job = MarketplaceImportJob(
            store_id=test_store.id,
            user_id=test_user.id,
            source_url="https://example.com/feed.csv",
        )

        db.add(import_job)
        db.commit()
        db.refresh(import_job)

        assert import_job.store is not None
        assert import_job.user is not None
        assert import_job.store.id == test_store.id
        assert import_job.user.id == test_user.id
