"""E2E test configuration and fixtures."""

import shutil
from pathlib import Path

import pytest

from quidclaw.config import QuidClawConfig
from quidclaw.core.ledger import Ledger

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


@pytest.fixture
def data_dir(tmp_path):
    """Create a fresh QuidClaw data directory with initialized ledger."""
    config = QuidClawConfig(data_dir=tmp_path)
    ledger = Ledger(config)
    ledger.init()
    return tmp_path


@pytest.fixture
def data_dir_with_accounts(data_dir):
    """Data directory with default accounts already created."""
    from quidclaw.core.init import LedgerInitializer
    config = QuidClawConfig(data_dir=data_dir)
    ledger = Ledger(config)
    LedgerInitializer(ledger).init_with_template()
    return data_dir


def copy_fixture_to_inbox(data_dir: Path, fixture_relative_path: str, rename: str = None):
    """Copy a fixture file into the data_dir's inbox."""
    src = FIXTURES_DIR / fixture_relative_path
    dest_name = rename or src.name
    dest = data_dir / "inbox" / dest_name
    shutil.copy2(str(src), str(dest))
    return dest
