"""Publishing, SEO and PWA API tests — Phase 11.7."""
import pytest

from app.tests.conftest import TENANT_A, TENANT_B, tenant_headers


async def _foundation(client):
    ws = await client.post("/api/v1/workspaces", json={"code": "publish", "name": "Publishing", "status": "active"}, headers=tenant_headers(TENANT_A))
    assert ws.status_code == 201, ws.text
    site = await client.post("/api/v1/sites", json={"workspace_id": ws.json()["id"], "code": "main", "name": "Main"}, headers=tenant_headers(TENANT_A))
    assert site.status_code == 201, site.text
    domain = await client.post("/api/v1/site-domains", json={"workspace_id": ws.json()["id"], "site_id": site.json()["id"], "host": "publish.example.test", "domain_kind": "custom"}, headers=tenant_headers(TENANT_A))
    assert domain.status_code == 201, domain.text
    return ws.json(), site.json(), domain.json()


@pytest.mark.asyncio
async def test_phase_117_publishing_seo_pwa(client):
    caps = (await client.get("/capabilities")).json()
    assert caps["phase"] == "11.10"
    assert caps["features"]["seo_engine"] is True
    ws, site, domain = await _foundation(client)
    headers = tenant_headers(TENANT_A)

    release = await client.post("/api/v1/publish-releases", json={
        "workspace_id": ws["id"], "target_type": "site", "target_id": site["id"], "metadata_json": {"channel": "web"}
    }, headers=headers)
    assert release.status_code == 201, release.text
    assert release.json()["status"] == "published"

    edge = await client.post("/api/v1/domain-edge-bindings", json={
        "workspace_id": ws["id"], "site_id": site["id"], "site_domain_id": domain["id"], "dns_status_ref": "dns-1"
    }, headers=headers)
    assert edge.status_code == 201, edge.text

    requests = [
        ("/api/v1/seo-metadata", {"workspace_id": ws["id"], "target_type": "site", "target_id": site["id"], "title": "Main"}),
        ("/api/v1/sitemap-shells", {"workspace_id": ws["id"], "site_id": site["id"], "entries_json": {"urls": []}}),
        ("/api/v1/robots-shells", {"workspace_id": ws["id"], "site_id": site["id"], "content_text": "User-agent: *"}),
        ("/api/v1/pwa-manifests", {"workspace_id": ws["id"], "site_id": site["id"], "name": "Main", "manifest_json": {"display": "standalone"}}),
    ]
    for path, payload in requests:
        response = await client.post(path, json=payload, headers=headers)
        assert response.status_code == 201, response.text
        isolated = await client.get(f"{path}/{response.json()['id']}", headers=tenant_headers(TENANT_B))
        assert isolated.status_code == 404


@pytest.mark.asyncio
async def test_rejects_edge_secrets_in_metadata(client):
    ws, site, _domain = await _foundation(client)
    response = await client.post("/api/v1/seo-metadata", json={
        "workspace_id": ws["id"], "target_type": "site", "target_id": site["id"],
        "metadata_json": {"ssl_private_key": "-----BEGIN PRIVATE KEY-----"}
    }, headers=tenant_headers(TENANT_A))
    assert response.status_code == 422
