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

Tests the /api/v1/admin/marketplace-import-jobs/* endpoints.
"""

import pytest


@pytest.mark.integration
@pytest.mark.api
@pytest.mark.admin
class TestAdminMarketplaceAPI:
    """Test admin marketplace import job endpoints at /api/v1/admin/marketplace-import-jobs/*."""

    def test_get_marketplace_import_jobs_admin(
        self, client, admin_headers, test_marketplace_import_job
    ):
        """Test admin getting marketplace import jobs."""
        response = client.get(
            "/api/v1/admin/marketplace-import-jobs", headers=admin_headers
        )

        assert response.status_code == 200
        data = response.json()
        assert "items" in data
        assert "total" in data
        assert "page" in data
        assert "limit" in data
        assert len(data["items"]) >= 1

        # Check that test_marketplace_import_job is in the response
        job_ids = [job["job_id"] for job in data["items"] if "job_id" in job]
        assert test_marketplace_import_job.id in job_ids

    def test_get_marketplace_import_jobs_with_filters(
        self, client, admin_headers, test_marketplace_import_job
    ):
        """Test admin getting marketplace import jobs with filters."""
        response = client.get(
            "/api/v1/admin/marketplace-import-jobs",
            params={"marketplace": test_marketplace_import_job.marketplace},
            headers=admin_headers,
        )

        assert response.status_code == 200
        data = response.json()
        assert "items" in data
        assert len(data["items"]) >= 1
        assert all(
            job["marketplace"] == test_marketplace_import_job.marketplace
            for job in data["items"]
        )

    def test_get_marketplace_import_jobs_non_admin(self, client, auth_headers):
        """Test non-admin trying to access marketplace import jobs."""
        response = client.get(
            "/api/v1/admin/marketplace-import-jobs", headers=auth_headers
        )

        assert response.status_code == 403
        data = response.json()
        assert data["error_code"] == "ADMIN_REQUIRED"
