"""Tests for OpenAPI ingest and SaaSModel."""

import pytest

from foremet.ingest.model import SaaSModel
from foremet.ingest.openapi import load_openapi_to_model, _parse_spec


PETSTORE_YAML = """
openapi: 3.0.0
info:
  title: Petstore
  description: A simple API
paths:
  /pets:
    get:
      summary: List pets
      operationId: listPets
      tags: [pets]
      parameters:
        - name: limit
          in: query
          schema: { type: integer }
    post:
      summary: Create a pet
      tags: [pets]
  /pets/{petId}:
    get:
      summary: Get pet by ID
      tags: [pets]
      parameters:
        - name: petId
          in: path
          required: true
          schema: { type: string }
"""


def test_parse_spec_operations_and_resources() -> None:
    spec = __import__("yaml").safe_load(PETSTORE_YAML)
    model = _parse_spec(spec)
    assert isinstance(model, SaaSModel)
    assert model.title == "Petstore"
    assert len(model.operations) == 4  # get/post /pets, get /pets/{petId}
    paths = {(op.path, op.method) for op in model.operations}
    assert ("/pets", "GET") in paths
    assert ("/pets", "POST") in paths
    assert any(op.path == "/pets/{petId}" for op in model.operations)
    assert len(model.resources) >= 1
    assert any(r.name == "pets" for r in model.resources)


def test_load_openapi_from_dict_style_path(monkeypatch: pytest.MonkeyPatch) -> None:
    """Test load_openapi_to_model with a local path (we pass YAML content via mock)."""
    import foremet.ingest.openapi as openapi_mod
    spec = __import__("yaml").safe_load(PETSTORE_YAML)

    def mock_load(source: str, is_url: bool) -> dict:
        return spec

    monkeypatch.setattr(openapi_mod, "_load_spec_raw", mock_load)
    model = load_openapi_to_model("/fake/path.yaml", is_url=False)
    assert model.title == "Petstore"
    assert len(model.operations) == 4
