from datetime import datetime
from pathlib import Path
from typing import Optional

from pydantic import BaseModel, ConfigDict


class LoggingConfig(BaseModel):
    model_config = ConfigDict(extra="forbid", validate_assignment=True)

    initialize: bool = True
    file_path: Optional[str] = None
    file_dir: Optional[str] = None
    file_name_pattern: Optional[str] = None
    level: int = 20
    suppress_node_logs: bool = True


def resolve_log_file_path(cfg: LoggingConfig) -> Optional[str]:
    if cfg.file_path:
        return cfg.file_path
    if not cfg.file_dir:
        return None

    pattern = cfg.file_name_pattern or "pipeline_{timestamp}.log"
    ts = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = pattern.replace("{timestamp}", ts)
    log_dir = Path(cfg.file_dir)
    log_dir.mkdir(parents=True, exist_ok=True)
    return str(log_dir / filename)
