"""Tests for lws.terraform.override -- override file generation."""

from __future__ import annotations

from pathlib import Path

import pytest

from lws.terraform.override import MARKER_COMMENT, OVERRIDE_FILENAME, generate_override


class TestGenerateOverride:
    """Tests for the generate_override function."""

    def test_generates_override_file(self, tmp_path: Path) -> None:
        """Generate override file with correct content and endpoint URLs."""
        # Arrange
        port = 3000
        expected_apigw_port = port + 8

        # Act
        override_path = generate_override(port, tmp_path)

        # Assert
        assert override_path.exists()
        assert override_path.name == OVERRIDE_FILENAME

        actual_content = override_path.read_text()
        assert MARKER_COMMENT in actual_content

        # Verify endpoint URLs (base port + 1 through 13)
        assert f"http://localhost:{port + 1}" in actual_content  # dynamodb
        assert f"http://localhost:{port + 2}" in actual_content  # sqs
        assert f"http://localhost:{port + 3}" in actual_content  # s3
        assert f"http://localhost:{port + 4}" in actual_content  # sns
        assert f"http://localhost:{port + 5}" in actual_content  # cloudwatchevents
        assert f"http://localhost:{port + 6}" in actual_content  # stepfunctions
        assert f"http://localhost:{port + 7}" in actual_content  # cognitoidp
        assert f"http://localhost:{expected_apigw_port}" in actual_content  # apigateway
        assert f"http://localhost:{port + 9}" in actual_content  # lambda
        assert f"http://localhost:{port + 10}" in actual_content  # iam
        assert f"http://localhost:{port + 11}" in actual_content  # sts
        assert f"http://localhost:{port + 12}" in actual_content  # ssm
        assert f"http://localhost:{port + 13}" in actual_content  # secretsmanager

        # Verify both V1 and V2 API Gateway endpoints point to same port
        assert f'apigateway       = "http://localhost:{expected_apigw_port}"' in actual_content
        assert f'apigatewayv2     = "http://localhost:{expected_apigw_port}"' in actual_content

    def test_overwrites_lws_generated_file(self, tmp_path: Path) -> None:
        """Overwrite existing LWS-generated override file."""
        # Arrange
        override_path = tmp_path / OVERRIDE_FILENAME
        override_path.write_text(f"{MARKER_COMMENT}\nOld content")
        port = 4000

        # Act
        actual_path = generate_override(port, tmp_path)

        # Assert
        assert actual_path == override_path
        actual_content = override_path.read_text()
        assert "Old content" not in actual_content
        assert f"http://localhost:{port + 1}" in actual_content

    def test_refuses_to_overwrite_user_file(self, tmp_path: Path) -> None:
        """Raise FileExistsError when trying to overwrite user-created file."""
        # Arrange
        override_path = tmp_path / OVERRIDE_FILENAME
        override_path.write_text('# User-created file\nprovider "aws" {}')
        expected_error_fragment = "not generated by LWS"

        # Act / Assert
        with pytest.raises(FileExistsError) as exc_info:
            generate_override(3000, tmp_path)

        actual_error_message = str(exc_info.value)
        assert OVERRIDE_FILENAME in actual_error_message
        assert expected_error_fragment in actual_error_message
