"""JSON schemas and utilities for workflow settings"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/settings/schemas.ipynb.

# %% auto 0
__all__ = ['WORKFLOW_SETTINGS_SCHEMA', 'WorkflowSettings']

# %% ../../nbs/settings/schemas.ipynb 3
from dataclasses import dataclass, field
from typing import Dict, Any, List, Optional, ClassVar

from fastcore.basics import patch

from cjm_fasthtml_workflow_transcription_single_file.core.schemas import (
    SCHEMA_TITLE, SCHEMA_DESC, SCHEMA_MIN, SCHEMA_MAX, SCHEMA_ENUM,
    SCHEMA_MIN_LEN, SCHEMA_MAX_LEN, SCHEMA_PATTERN, SCHEMA_FORMAT,
    dataclass_to_jsonschema
)
from ..core.config import SingleFileWorkflowConfig
from ..media.config import MediaConfig
from ..storage.config import StorageConfig

# %% ../../nbs/settings/schemas.ipynb 5
@dataclass
class WorkflowSettings:
    """User-configurable settings for single-file transcription workflow."""
    
    # Class-level schema metadata (ClassVar excludes these from dataclass fields)
    __schema_name__: ClassVar[str] = "single_file_workflow"
    __schema_title__: ClassVar[str] = "Single File Transcription Settings"
    __schema_description__: ClassVar[str] = "Configure media scanning, storage, and workflow behavior"
    
    # Media settings (field names match MediaConfig for seamless config loading)
    directories: List[str] = field(
        default_factory=list,
        metadata={
            SCHEMA_TITLE: "Media Directories",
            SCHEMA_DESC: "Directories to scan for media files"
        }
    )
    scan_audio: bool = field(
        default=True,
        metadata={
            SCHEMA_TITLE: "Scan Audio Files",
            SCHEMA_DESC: "Include audio files in scan results"
        }
    )
    scan_video: bool = field(
        default=True,
        metadata={
            SCHEMA_TITLE: "Scan Video Files",
            SCHEMA_DESC: "Include video files in scan results"
        }
    )
    recursive_scan: bool = field(
        default=True,
        metadata={
            SCHEMA_TITLE: "Recursive Scan",
            SCHEMA_DESC: "Scan subdirectories"
        }
    )
    items_per_page: int = field(
        default=30,
        metadata={
            SCHEMA_TITLE: "Items Per Page",
            SCHEMA_DESC: "Number of files to show per page",
            SCHEMA_MIN: 10,
            SCHEMA_MAX: 100
        }
    )
    default_view: str = field(
        default="list",
        metadata={
            SCHEMA_TITLE: "Default View",
            SCHEMA_DESC: "Default view mode for file selection",
            SCHEMA_ENUM: ["list"]  # Currently only list view is implemented
        }
    )
    
    # Storage settings
    auto_save: bool = field(
        default=True,
        metadata={
            SCHEMA_TITLE: "Auto-save Results",
            SCHEMA_DESC: "Automatically save transcription results when complete"
        }
    )
    results_directory: str = field(
        default="transcription_results",
        metadata={
            SCHEMA_TITLE: "Results Directory",
            SCHEMA_DESC: "Directory to save transcription results"
        }
    )
    
    # Resource management settings
    gpu_memory_threshold_percent: float = field(
        default=45.0,
        metadata={
            SCHEMA_TITLE: "GPU Memory Threshold (%)",
            SCHEMA_DESC: "GPU memory usage threshold for conflict detection (0-100)",
            SCHEMA_MIN: 0,
            SCHEMA_MAX: 100
        }
    )

# %% ../../nbs/settings/schemas.ipynb 8
WORKFLOW_SETTINGS_SCHEMA = dataclass_to_jsonschema(WorkflowSettings) # Auto-generate schema from WorkflowSettings dataclass

# %% ../../nbs/settings/schemas.ipynb 10
@patch(cls_method=True)
def from_configs(
    cls: WorkflowSettings,
    media_config: MediaConfig,      # MediaConfig instance with media scanning settings
    storage_config: StorageConfig,  # StorageConfig instance with result storage settings
    workflow_config: Optional[SingleFileWorkflowConfig] = None  # Optional workflow config for additional settings
) -> "WorkflowSettings":  # WorkflowSettings instance with values from configs
    """Create WorkflowSettings from runtime config objects."""
    return cls(
        # Media settings
        directories=media_config.directories,
        scan_audio=media_config.scan_audio,
        scan_video=media_config.scan_video,
        recursive_scan=media_config.recursive_scan,
        items_per_page=media_config.items_per_page,
        default_view=media_config.default_view,
        # Storage settings
        auto_save=storage_config.auto_save,
        results_directory=storage_config.results_directory,
        # Workflow settings
        gpu_memory_threshold_percent=workflow_config.gpu_memory_threshold_percent if workflow_config else 45.0
    )

# %% ../../nbs/settings/schemas.ipynb 11
@patch
def apply_to_configs(
    self: WorkflowSettings,
    media_config: MediaConfig,      # MediaConfig instance to update
    storage_config: StorageConfig,  # StorageConfig instance to update
    workflow_config: Optional[SingleFileWorkflowConfig] = None  # Optional workflow config to update
) -> None:
    """Apply settings to runtime config objects."""
    # Media settings
    media_config.directories = self.directories
    media_config.scan_audio = self.scan_audio
    media_config.scan_video = self.scan_video
    media_config.recursive_scan = self.recursive_scan
    media_config.items_per_page = self.items_per_page
    media_config.default_view = self.default_view
    
    # Storage settings
    storage_config.auto_save = self.auto_save
    storage_config.results_directory = self.results_directory
    
    # Workflow settings
    if workflow_config:
        workflow_config.gpu_memory_threshold_percent = self.gpu_memory_threshold_percent

# %% ../../nbs/settings/schemas.ipynb 12
@patch
def to_dict(
    self: WorkflowSettings
) -> Dict[str, Any]:  # Dictionary of settings values
    """Convert settings to a dictionary for serialization."""
    return {
        "directories": self.directories,
        "scan_audio": self.scan_audio,
        "scan_video": self.scan_video,
        "recursive_scan": self.recursive_scan,
        "items_per_page": self.items_per_page,
        "default_view": self.default_view,
        "auto_save": self.auto_save,
        "results_directory": self.results_directory,
        "gpu_memory_threshold_percent": self.gpu_memory_threshold_percent,
    }
