"""
Photon Mosaic Main Workflow

This is the main Snakefile that orchestrates the entire photon mosaic processing pipeline.
It handles dataset discovery, target generation, and coordinates the preprocessing,
suite2p analysis workflows.

The workflow:
1. Discovers datasets and their TIFF files from the raw data directory
2. Generates preprocessing targets for each dataset/session combination
3. Generates suite2p analysis targets for processed data
4. Generates dff calculation targets for processed data
5. Includes preprocessing.smk, suite2p.smk modules to execute the actual processing
"""

from pathlib import Path
from photon_mosaic import log_cuda_availability
from photon_mosaic.paths_selection import (
    find_raw_data_paths,
    adapt_paths_to_output_pattern,
    set_up_suite2p_targets,
    _RAWDATA,
    _DERIVATIVES,
)
import logging

# Configure logging based on config settings
log_level = (
    logging.DEBUG
    if config.get("logging", {}).get("snakemake_verbose", False)
    else logging.INFO
)
logging.basicConfig(level=log_level)
logger = logging.getLogger("snakemake.workflow")


# CUDA availability check on workflow start
onstart:
    log_cuda_availability()


project_path = Path(config["project_path"]).resolve()
slurm_config = config.get("slurm", {})
output_pattern = config["preprocessing"]["output_pattern"]

logger.info(f"Project path: {project_path}")

# Log SLURM configuration
use_slurm = config.get("use_slurm", False)
if use_slurm:
    logger.info("SLURM execution enabled")
    logger.info(f"SLURM configuration: {slurm_config}")
else:
    logger.info("SLURM execution disabled - running locally")

# Discover datasets and their TIFF files
all_selected_tiff_paths = find_raw_data_paths(
    project_path,
    tiff_patterns=config["dataset_discovery"].get("tiff_patterns", ["*.tif"]),
    exclude_datasets=config["dataset_discovery"].get("exclude_datasets"),
    exclude_sessions=config["dataset_discovery"].get("exclude_sessions"),
)

logger.info(f"Found {len(all_selected_tiff_paths)} TIFF file(s)")

# Wildcard constraints derived from discovered paths
_subject_names = sorted(
    {p.parts[p.parts.index(_RAWDATA) + 1] for p in all_selected_tiff_paths}
)
_session_names = sorted(
    {p.parts[p.parts.index(_RAWDATA) + 2] for p in all_selected_tiff_paths}
)

preproc_targets = adapt_paths_to_output_pattern(all_selected_tiff_paths, output_pattern)

logger.info(f"Preprocessing targets: {preproc_targets}")

suite2p_targets = set_up_suite2p_targets(preproc_targets)

logger.info(f"Suite2p targets: {suite2p_targets}")


include: "preprocessing.smk"
include: "suite2p.smk"


rule all:
    input:
        preproc_targets,
        suite2p_targets,
