import shutil
import time
from os import path as osp
from pathlib import Path

import pytest

import constants as c_mod


def _prepare_form_dataset(
    monkeypatch,
    tmp_path,
    constants_patch,
    fixture_images_dir,
    fixture_labels_dir,
    count,
    corrupt_stems,
):
    constants_patch(tmp_path)
    import train

    data_img = Path(c_mod.config.images_dir)
    data_lbl = Path(c_mod.config.labels_dir)
    data_img.mkdir(parents=True, exist_ok=True)
    data_lbl.mkdir(parents=True, exist_ok=True)

    imgs = sorted(fixture_images_dir.glob("*.jpg"))[:count]
    for p in imgs:
        stem = p.stem
        shutil.copy2(fixture_images_dir / f"{stem}.jpg", data_img / f"{stem}.jpg")
        dst = data_lbl / f"{stem}.txt"
        shutil.copy2(fixture_labels_dir / f"{stem}.txt", dst)
        if stem in corrupt_stems:
            dst.write_text("0 1.5 0.5 0.1 0.1\n", encoding="utf-8")

    today_ds = osp.join(c_mod.config.datasets_dir, train.today_folder)
    return train, today_ds


@pytest.mark.performance
def test_pt_dsf_01_dataset_formation_under_thirty_seconds(
    monkeypatch,
    tmp_path,
    constants_patch,
    fixture_images_dir,
    fixture_labels_dir,
):
    # Arrange
    train, today_ds = _prepare_form_dataset(
        monkeypatch,
        tmp_path,
        constants_patch,
        fixture_images_dir,
        fixture_labels_dir,
        100,
        set(),
    )
    # Act
    t0 = time.perf_counter()
    train.form_dataset()
    elapsed = time.perf_counter() - t0
    # Assert
    assert elapsed <= 30.0
