import os
import shutil
import threading

from pydantic import model_validator

from wren.app_server.file_store.files import FileStore
from wren.app_server.utils.logger import wren_logger as logger


class LocalFileStore(FileStore):
    root: str

    @model_validator(mode='after')
    def _setup_root(self) -> 'LocalFileStore':
        if self.root.startswith('~'):
            self.root = os.path.expanduser(self.root)
        os.makedirs(self.root, exist_ok=True)
        return self

    def get_full_path(self, path: str) -> str:
        if path.startswith('/'):
            path = path[1:]

        root_abs = os.path.abspath(self.root)
        # Reject path traversal: the resolved path must stay inside root.
        # abspath normalizes '..' without requiring the target to exist, so any
        # escape sequence (e.g. '../../etc/passwd') bubbles to a path outside root.
        candidate = os.path.abspath(os.path.join(root_abs, path))
        if candidate != root_abs and not candidate.startswith(root_abs + os.sep):
            raise ValueError(
                f'Path "{path}" escapes the file store root "{self.root}"'
            )
        return candidate

    def write(self, path: str, contents: str | bytes) -> None:
        full_path = self.get_full_path(path)
        os.makedirs(os.path.dirname(full_path), exist_ok=True)
        mode = 'w' if isinstance(contents, str) else 'wb'

        # Use atomic write: write to temp file, then rename
        # This prevents race conditions where concurrent writes could corrupt the file
        temp_path = f'{full_path}.tmp.{os.getpid()}.{threading.get_ident()}'
        try:
            with open(temp_path, mode) as f:
                f.write(contents)
                f.flush()
                os.fsync(f.fileno())
            os.replace(temp_path, full_path)
        except Exception:
            if os.path.exists(temp_path):
                os.remove(temp_path)
            raise

    def write_from_path(self, path: str, source_path: str) -> None:
        # shutil.copyfile streams in chunks (never the whole file in RAM); keep
        # the same write-temp-then-atomic-rename to avoid torn concurrent writes.
        full_path = self.get_full_path(path)
        os.makedirs(os.path.dirname(full_path), exist_ok=True)
        temp_path = f'{full_path}.tmp.{os.getpid()}.{threading.get_ident()}'
        try:
            shutil.copyfile(source_path, temp_path)
            os.replace(temp_path, full_path)
        except Exception:
            if os.path.exists(temp_path):
                os.remove(temp_path)
            raise

    def read(self, path: str) -> str:
        full_path = self.get_full_path(path)
        with open(full_path, 'r') as f:
            return f.read()

    def list(self, path: str) -> list[str]:
        full_path = self.get_full_path(path)
        files = [os.path.join(path, f) for f in os.listdir(full_path)]
        files = [f + '/' if os.path.isdir(self.get_full_path(f)) else f for f in files]
        return files

    def delete(self, path: str) -> None:
        try:
            full_path = self.get_full_path(path)
            if not os.path.exists(full_path):
                logger.debug(f'Local path does not exist: {full_path}')
                return
            if os.path.isfile(full_path):
                os.remove(full_path)
                logger.debug(f'Removed local file: {full_path}')
            elif os.path.isdir(full_path):
                shutil.rmtree(full_path)
                logger.debug(f'Removed local directory: {full_path}')
        except Exception as e:
            logger.error(f'Error clearing local file store: {str(e)}')
