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

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

import pytest


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

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

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

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

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

    def test_get_import_job_status_not_found(self, client, vendor_user_headers):
        """Test getting non-existent import job returns not found."""
        response = client.get(
            "/api/v1/vendor/marketplace/imports/99999",
            headers=vendor_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/vendor/marketplace/imports")

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

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

        response = client.post(
            "/api/v1/vendor/marketplace/import",
            headers=vendor_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.vendor
class TestVendorMarketplaceImportJobAPI:
    """Test vendor marketplace import job management."""

    def test_get_import_job_success(
        self,
        client,
        vendor_user_headers,
        test_vendor_with_vendor_user,
        test_marketplace_import_job,
        db,
    ):
        """Test getting import job status by ID."""
        # Ensure the import job belongs to the vendor
        test_marketplace_import_job.vendor_id = test_vendor_with_vendor_user.id
        db.commit()

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

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