"""REQ-105: per-environment contract files exist + validate + resolve."""
import json
import sys
from pathlib import Path

import jsonschema
import pytest
import yaml

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from core.contract_resolver import resolve

SCHEMA = json.loads((ROOT / "schemas" / "contract.schema.json").read_text())

PER_ENV_CONTRACTS = [
    "contracts/static-assets.dev.yml",
    "contracts/static-assets.qa.yml",
    "contracts/static-assets.prod.yml",
    "contracts/static-assets.dr.yml",
    "contracts/microservice.dev.yml",
    "contracts/microservice.qa.yml",
    "contracts/microservice.prod.yml",
    "contracts/microservice.dr.yml",
]


@pytest.mark.parametrize("rel", PER_ENV_CONTRACTS)
def test_per_env_contract_validates_against_schema(rel):
    contract = yaml.safe_load((ROOT / rel).read_text())
    jsonschema.validate(contract, SCHEMA)


@pytest.mark.parametrize("rel", PER_ENV_CONTRACTS)
def test_per_env_contract_resolves(rel):
    stack = resolve(str(ROOT / rel))
    assert stack["stack"]["name"] in ("assets", "msvc")


def test_static_assets_dev_has_dev_environment():
    c = yaml.safe_load((ROOT / "contracts/static-assets.dev.yml").read_text())
    assert c["environment"] == "dev"


def test_static_assets_qa_has_qa_environment():
    c = yaml.safe_load((ROOT / "contracts/static-assets.qa.yml").read_text())
    assert c["environment"] == "qa"


def test_static_assets_prod_has_prod_environment():
    c = yaml.safe_load((ROOT / "contracts/static-assets.prod.yml").read_text())
    assert c["environment"] == "prod"


def test_static_assets_dr_has_dr_environment():
    c = yaml.safe_load((ROOT / "contracts/static-assets.dr.yml").read_text())
    assert c["environment"] == "dr"


def test_per_env_contracts_use_interpolation():
    """Each per-env contract uses ${env.*} interpolation for the bucket name."""
    for rel in PER_ENV_CONTRACTS:
        text = (ROOT / rel).read_text()
        assert "${env.environment}" in text
        assert "${env.account_id}" in text


def test_per_env_qa_resolves_to_qa_bucket_name():
    stack = resolve(str(ROOT / "contracts/static-assets.qa.yml"))
    s3 = [r for r in stack["resources"] if r["type"] == "aws:s3:bucket"][0]
    assert s3["inputs"]["bucket_name"] == "acdl-qa-assets-000000000000-us-east-1"


def test_per_env_prod_resolves_to_prod_bucket_name():
    stack = resolve(str(ROOT / "contracts/static-assets.prod.yml"))
    s3 = [r for r in stack["resources"] if r["type"] == "aws:s3:bucket"][0]
    assert s3["inputs"]["bucket_name"] == "acdl-prod-assets-000000000000-us-east-1"


def test_default_dev_contract_still_works():
    """The existing contracts/static-assets.yml remains the dev default."""
    c = yaml.safe_load((ROOT / "contracts/static-assets.yml").read_text())
    assert c["environment"] == "dev"
    stack = resolve(str(ROOT / "contracts/static-assets.yml"))
    s3 = [r for r in stack["resources"] if r["type"] == "aws:s3:bucket"][0]
    assert s3["inputs"]["bucket_name"] == "acdl-dev-assets-000000000000-us-east-1"