from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from scienith_openapi_contract_tooling.profiles import build_from_config


OPENAPI_YAML = """\
openapi: 3.1.0
info:
  title: Profile Fixture
  version: 1.0.0
paths:
  /items:
    get:
      operationId: listItems
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ListItemsResponse"
components:
  schemas:
    Item:
      type: object
      required: [id]
      properties:
        id:
          type: string
    ListItemsResponse:
      type: object
      required: [items]
      properties:
        items:
          type: array
          items:
            $ref: "#/components/schemas/Item"
"""


class ProfileTests(unittest.TestCase):
    def test_monorepo_adapter_profile_writes_skills_management_artifacts(self) -> None:
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            (root / "contracts").mkdir()
            (root / "contracts" / "openapi.yaml").write_text(OPENAPI_YAML, encoding="utf-8")
            config = root / "contract-tooling.yaml"
            config.write_text(
                """\
profiles:
  fixture:
    type: monorepo-adapter
    root: .
    openapi: contracts/openapi.yaml
    outputs:
      openapi_bundle: generated/openapi.bundle.json
      contract_summary: generated/summary.json
      operation_maps_ts: generated/openapi-types.ts
      python_validator: generated/openapi_contract.py
      python_validator_init: generated/__init__.py
""",
                encoding="utf-8",
            )
            build_from_config(config_path=config, profile_name="fixture")
            self.assertTrue((root / "generated" / "openapi.bundle.json").exists())
            operation_maps = (root / "generated" / "openapi-types.ts").read_text()
            self.assertIn("// Generated by scienith-contract. Do not edit by hand.", operation_maps)
            self.assertNotIn("scripts/build_contract_artifacts.py", operation_maps)
            self.assertIn("export type OperationMap", operation_maps)
            self.assertIn("validate_response", (root / "generated" / "openapi_contract.py").read_text())

    def test_contract_package_profile_dry_run_validates_full_output_shape(self) -> None:
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            (root / "openapi").mkdir()
            (root / "openapi" / "openapi.yaml").write_text(OPENAPI_YAML, encoding="utf-8")
            config = root / "contract-tooling.yaml"
            config.write_text(
                """\
profiles:
  fixture:
    type: contract-package
    root: .
    openapi: openapi/openapi.yaml
    ts_package_dir: packages/ts
    outputs:
      python_models: out/python/models_generated.py
      python_openapi: out/python/openapi/openapi.yaml
      ts_types: out/ts/openapi.types.ts
      zod_ts: out/ts/openapi.zod.ts
      msw_handlers: out/ts/handlers.generated.ts
      ts_openapi: out/ts/openapi/openapi.yaml
      redoc_html: out/docs/openapi.redoc.html
      plantuml: out/docs/openapi.puml
""",
                encoding="utf-8",
            )
            build_from_config(config_path=config, profile_name="fixture", dry_run=True)


if __name__ == "__main__":
    unittest.main()
