# Copyright (C) 2026 NPO NT
# SPDX-License-Identifier: AGPL-3.0-only

from __future__ import annotations

import csv
import hashlib
import json
from pathlib import Path

from mika_deid.dataset_validator import validate_dataset


def test_minimal_dataset_contract(tmp_path: Path) -> None:
    documents = tmp_path / "documents"
    annotations = tmp_path / "annotations"
    documents.mkdir()
    annotations.mkdir()
    document = documents / "doc-000001.pdf"
    document.write_bytes(b"synthetic-placeholder")
    digest = hashlib.sha256(document.read_bytes()).hexdigest()
    manifest = tmp_path / "manifest.csv"
    with manifest.open("w", encoding="utf-8", newline="") as stream:
        writer = csv.DictWriter(
            stream,
            fieldnames=[
                "document_id",
                "file_path",
                "sha256",
                "document_type",
                "language",
                "page_count",
                "source_kind",
                "quality_tier",
                "contains_handwriting",
                "contains_barcode",
                "split_group",
            ],
        )
        writer.writeheader()
        writer.writerow(
            {
                "document_id": "doc-000001",
                "file_path": "documents/doc-000001.pdf",
                "sha256": digest,
                "document_type": "discharge_summary",
                "language": "ru",
                "page_count": "1",
                "source_kind": "native_pdf",
                "quality_tier": "good",
                "contains_handwriting": "false",
                "contains_barcode": "false",
                "split_group": "g-000001",
            }
        )
    annotation = {
        "schema": "mika-deid-annotation-v1",
        "document_id": "doc-000001",
        "pages": [
            {
                "page": 1,
                "width": 1.0,
                "height": 1.0,
                "regions": [
                    {
                        "region_id": "r-0001",
                        "category": "person_name",
                        "box": {"x0": 0.1, "y0": 0.1, "x1": 0.5, "y1": 0.2},
                        "required_action": "redact",
                        "legibility": "clear",
                        "notes": "synthetic label only",
                    }
                ],
            }
        ],
    }
    (annotations / "doc-000001.json").write_text(json.dumps(annotation), encoding="utf-8")
    result = validate_dataset(tmp_path)
    assert result.passed
    assert result.document_count == 1
    assert result.annotation_count == 1
    assert result.warnings


def test_identifier_in_filename_is_rejected(tmp_path: Path) -> None:
    (tmp_path / "manifest.csv").write_text(
        "document_id,file_path,sha256,document_type,language,page_count,source_kind,quality_tier,contains_handwriting,contains_barcode,split_group\n"
        "doc-000001,documents/пациент-12.04.1978.pdf,00,x,ru,1,native_pdf,good,false,false,g-1\n",
        encoding="utf-8",
    )
    result = validate_dataset(tmp_path)
    assert not result.passed
    assert any("имя файла" in issue for issue in result.issues)
