from pathlib import Path
from unittest.mock import patch

import pytest
from sqlalchemy import select

from app.assets.database.models import Asset, AssetContent, AssetTag, Tag
from app.assets.helpers import to_stored_hash
from app.assets.scanner import (
    SeedAssetSpec,
    clear_pending_verifications,
    pending_recovery_count,
    seed_asset_specs,
)
from app.assets.scanner_changes import recover_missing_content
from app.assets.services.snapshot_hash import snapshot_hash


@pytest.fixture(autouse=True)
def _clear_queues():
    clear_pending_verifications()
    yield
    clear_pending_verifications()


def _missing_content(session, path: Path, hash_value: str) -> tuple[AssetContent, Asset]:
    content = AssetContent(path=str(path), hash=hash_value, is_missing=True)
    session.add(content)
    session.flush()
    record = Asset(content_id=content.id, name=path.name)
    session.add(record)
    session.flush()
    if session.get(Tag, "missing") is None:
        session.add(Tag(name="missing"))
    session.add(AssetTag(asset_id=record.id, tag_name="missing", origin="automatic"))
    session.commit()
    return content, record


def _spec(path: Path) -> SeedAssetSpec:
    stat = path.stat()
    return {
        "abs_path": str(path),
        "size_bytes": stat.st_size,
        "mtime_ns": stat.st_mtime_ns,
        "info_name": path.name,
        "tags": ["input"],
        "fname": path.name,
        "metadata": None,
        "mime_type": None,
        "job_id": None,
    }


def _stored_hash(path: Path) -> str:
    snapshot = snapshot_hash(str(path))
    assert snapshot is not None
    digest, _ = snapshot
    return to_stored_hash(digest)


def test_single_hash_match_recovers(session, temp_dir: Path):
    path = temp_dir / "restored.bin"
    path.write_bytes(b"restored bytes")
    content, record = _missing_content(session, path, _stored_hash(path))

    with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert error is None
    assert created == 0
    assert session.get(AssetContent, content.id).is_missing is False
    assert session.get(AssetTag, {"asset_id": record.id, "tag_name": "missing"}) is None


def test_ambiguous_hash_match_recovers_nothing(session, temp_dir: Path):
    path = temp_dir / "ambiguous.bin"
    path.write_bytes(b"same bytes")
    digest = _stored_hash(path)
    first, _ = _missing_content(session, path, digest)
    second, _ = _missing_content(session, path, digest)

    with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert error is None
    assert created == 1
    assert session.get(AssetContent, first.id).is_missing is True
    assert session.get(AssetContent, second.id).is_missing is True
    assert len(session.scalars(select(AssetContent)).all()) == 3


def test_no_hash_match_creates_fresh_rows(session, temp_dir: Path):
    path = temp_dir / "different.bin"
    path.write_bytes(b"current bytes")
    missing, _ = _missing_content(session, path, "old")

    with patch("app.assets.scanner.mode.hashing_enabled", return_value=True):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert error is None
    assert created == 1
    assert session.get(AssetContent, missing.id).is_missing is True
    assert len(session.scalars(select(Asset)).all()) == 2


def _stat_content(session, path: Path, hash_value: str | None) -> tuple[AssetContent, Asset]:
    content, record = _missing_content(session, path, hash_value)
    stat_result = path.stat()
    content.size_bytes = stat_result.st_size
    content.mtime_ns = stat_result.st_mtime_ns
    session.commit()
    return content, record


def test_off_mode_recovers_by_exact_stat_without_hashing(session, temp_dir: Path):
    path = temp_dir / "off.bin"
    path.write_bytes(b"bytes")
    missing, record = _stat_content(session, path, _stored_hash(path))

    with (
        patch("app.assets.scanner.mode.hashing_enabled", return_value=False),
        patch("app.assets.scanner.snapshot_hash") as hash_mock,
    ):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    hash_mock.assert_not_called()
    assert error is None
    assert created == 0
    recovered = session.get(AssetContent, missing.id)
    assert recovered.is_missing is False
    assert recovered.hash == _stored_hash(path)
    assert session.get(AssetTag, {"asset_id": record.id, "tag_name": "missing"}) is None


@pytest.mark.parametrize("field", ["size_bytes", "mtime_ns"])
def test_off_mode_stat_mismatch_creates_fresh_rows(session, temp_dir: Path, field: str):
    path = temp_dir / "off-changed.bin"
    path.write_bytes(b"bytes")
    missing, _ = _stat_content(session, path, None)
    setattr(missing, field, getattr(missing, field) + 1)
    session.commit()

    with patch("app.assets.scanner.mode.hashing_enabled", return_value=False):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert error is None
    assert created == 1
    assert session.get(AssetContent, missing.id).is_missing is True


def test_unstable_hash_requeues(session, temp_dir: Path):
    path = temp_dir / "unstable.bin"
    path.write_bytes(b"bytes")
    missing, _ = _missing_content(session, path, "old")

    with (
        patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
        patch("app.assets.scanner.snapshot_hash", return_value=None),
    ):
        created, error = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert error is None
    assert created == 0
    assert pending_recovery_count() == 1
    assert session.get(AssetContent, missing.id).is_missing is True


def test_recovery_skips_a_path_a_live_row_already_occupies(session, temp_dir: Path):
    path = temp_dir / "contested.bin"
    path.write_bytes(b"bytes restored under a path someone else already owns")
    digest = _stored_hash(path)
    stat_result = path.stat()
    missing_a, _ = _missing_content(session, path, digest)
    live_b = AssetContent(
        path=str(path),
        hash=digest,
        is_missing=False,
        size_bytes=stat_result.st_size,
        mtime_ns=stat_result.st_mtime_ns,
    )
    session.add(live_b)
    session.commit()
    live_b_id = live_b.id

    result = recover_missing_content(
        session, str(path), snapshot_hash(str(path)), hashing_is_enabled=True
    )
    session.commit()

    assert result == "no_match", (
        "the file at an occupied path belongs to the live row's story; change detection "
        "owns it, and recovering onto it would put two live rows on one path"
    )
    assert session.get(AssetContent, missing_a.id).is_missing is True
    assert session.get(AssetContent, live_b_id).is_missing is False


def test_seeded_row_takes_the_stat_its_hash_was_verified_against(session, temp_dir: Path):
    path = temp_dir / "rewritten.bin"
    path.write_bytes(b"old")

    def rewrite_then_hash(candidate_path: str):
        path.write_bytes(b"rewritten between the stat and the hash")
        return snapshot_hash(candidate_path)

    with (
        patch("app.assets.scanner.mode.hashing_enabled", return_value=True),
        patch("app.assets.scanner.snapshot_hash", side_effect=rewrite_then_hash),
    ):
        created, _ = seed_asset_specs(session, [_spec(path)])
    session.commit()

    assert created == 1
    content = session.scalars(select(AssetContent)).one()
    assert content.size_bytes == path.stat().st_size
    assert content.mtime_ns == path.stat().st_mtime_ns
