"""Tests for compiler/stages/style.py — all offline via FakeProvider."""

from __future__ import annotations

import json
from pathlib import Path

import pytest
from compiler.stages.style import run_style
from runtime.providers.fake import FakeProvider

FIXTURES = Path(__file__).parent.parent / "fixtures"

# Mini corpus for tests
CORPUS_MD = """\
---
source_id: test-src
title: Test Source
rights: public-domain
fetch_type: gutenberg
---

# Test Source

You have power over your mind, not outside events. Realize this, and you will find strength.

The impediment to action advances action. What stands in the way becomes the way.

Very little is needed to make a happy life; it is all within yourself in your way of thinking.

Confine yourself to the present and you will live a peaceful life.

The soul becomes dyed with the color of its thoughts.
"""


@pytest.fixture
def corpus_dir(tmp_path):
    d = tmp_path / "corpus"
    d.mkdir()
    (d / "test-src.md").write_text(CORPUS_MD)
    return d


@pytest.fixture
def style_provider():
    return FakeProvider.from_jsonl(FIXTURES / "style_extraction.jsonl")


# ── run_style ─────────────────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_run_style_creates_profile(corpus_dir, style_provider, tmp_path):
    style_dir = tmp_path / "style"
    profile_path, exemplars_path = await run_style(
        {"test-src": corpus_dir / "test-src.md"},
        style_dir,
        style_provider,
    )
    assert profile_path.exists()
    assert exemplars_path.exists()


@pytest.mark.asyncio
async def test_run_style_profile_has_sections(corpus_dir, style_provider, tmp_path):
    style_dir = tmp_path / "style"
    profile_path, _ = await run_style(
        {"test-src": corpus_dir / "test-src.md"},
        style_dir,
        style_provider,
    )
    content = profile_path.read_text()
    # The golden fixture contains section headings
    assert "#" in content  # at least one heading


@pytest.mark.asyncio
async def test_run_style_exemplars_are_valid_jsonl(
    corpus_dir, style_provider, tmp_path
):
    style_dir = tmp_path / "style"
    _, exemplars_path = await run_style(
        {"test-src": corpus_dir / "test-src.md"},
        style_dir,
        style_provider,
    )
    lines = [ln for ln in exemplars_path.read_text().splitlines() if ln.strip()]
    assert len(lines) > 0
    for line in lines:
        record = json.loads(line)
        assert "text" in record
        assert isinstance(record["text"], str)


@pytest.mark.asyncio
async def test_run_style_golden_output(corpus_dir, tmp_path):
    """Recorded golden response — deterministic replay."""
    style_dir = tmp_path / "style"
    provider = FakeProvider.from_jsonl(FIXTURES / "style_extraction.jsonl")
    profile_path, _ = await run_style(
        {"test-src": corpus_dir / "test-src.md"},
        style_dir,
        provider,
    )
    content = profile_path.read_text()
    assert "Stoic" in content or "austere" in content.lower() or "# Tone" in content


@pytest.mark.asyncio
async def test_run_style_empty_corpus(tmp_path):
    style_dir = tmp_path / "style"
    provider = FakeProvider.from_jsonl(FIXTURES / "style_extraction.jsonl")
    profile_path, exemplars_path = await run_style({}, style_dir, provider)
    assert profile_path.exists()
    assert exemplars_path.exists()
