# tests/integration/api/v1/store/test_marketplace.py
"""Integration tests for store marketplace import endpoints.

Tests the /api/v1/store/marketplace/* endpoints.
All endpoints require store JWT authentication.
"""

import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.store
class TestStoreMarketplaceAPI:
    """Test store marketplace import endpoints at /api/v1/store/marketplace/*."""

    def test_get_import_jobs_success(self, client, store_user_headers):
        """Test getting marketplace import jobs list."""
        response = client.get(
            "/api/v1/store/marketplace/imports",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)

    def test_get_import_jobs_with_filter(self, client, store_user_headers):
        """Test getting marketplace import jobs with marketplace filter."""
        response = client.get(
            "/api/v1/store/marketplace/imports?marketplace=amazon",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)

    def test_get_import_job_status_not_found(self, client, store_user_headers):
        """Test getting non-existent import job returns not found."""
        response = client.get(
            "/api/v1/store/marketplace/imports/99999",
            headers=store_user_headers,
        )

        assert response.status_code == 404
        data = response.json()
        assert data["error_code"] == "IMPORT_JOB_NOT_FOUND"

    def test_get_import_jobs_without_auth_returns_unauthorized(self, client):
        """Test getting import jobs without authentication returns unauthorized."""
        response = client.get("/api/v1/store/marketplace/imports")

        assert response.status_code == 401
        data = response.json()
        assert data["error_code"] == "INVALID_TOKEN"

    def test_import_products_validation_error(self, client, store_user_headers):
        """Test importing products with invalid data returns validation error."""
        import_data = {
            "marketplace": "",  # Invalid empty marketplace
        }

        response = client.post(
            "/api/v1/store/marketplace/import",
            headers=store_user_headers,
            json=import_data,
        )

        assert response.status_code == 422
        data = response.json()
        assert data["error_code"] == "VALIDATION_ERROR"


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.store
class TestStoreMarketplaceImportJobAPI:
    """Test store marketplace import job management."""

    def test_get_import_job_success(
        self,
        client,
        store_user_headers,
        test_store_with_store_user,
        test_marketplace_import_job,
        db,
    ):
        """Test getting import job status by ID."""
        # Ensure the import job belongs to the store
        test_marketplace_import_job.store_id = test_store_with_store_user.id
        db.commit()

        response = client.get(
            f"/api/v1/store/marketplace/imports/{test_marketplace_import_job.id}",
            headers=store_user_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert data["job_id"] == test_marketplace_import_job.id
