"""KITECH Manufacturing Data Repository Library and CLI."""

__version__ = "0.3.4"
__author__ = "KITECH Repository Team"

from kitech_repository.core.auth import AuthManager
from kitech_repository.core.client import KitechClient
from kitech_repository.core.config import Config


# Convenience functions for library usage
def download(repository_id: int, remote_path: str = "", local_path: str = None, app_key: str = None):
    """Download a file or directory from repository.

    Args:
        repository_id: The repository ID
        remote_path: Path to file/folder in repository.
                     - "" or "/" : Download entire repository (root)
                     - "file.txt" : Download single file
                     - "folder/" : Download folder (must end with /)
        local_path: Local directory to save (None for default download directory)
        app_key: API app key (None to use stored app key)

    Returns:
        Path: Path to downloaded file/directory
    """
    # Normalize: "/" or empty string = full repository download
    path = None if (not remote_path or remote_path == "/") else remote_path

    with KitechClient(app_key=app_key) as client:
        return client.download_file(
            repository_id=repository_id, path=path, output_dir=local_path, show_progress=True
        )


def upload_file(repository_id: int, file_path: str, remote_path: str = "", app_key: str = None):
    """Upload a single file to repository.

    Args:
        repository_id: The repository ID
        file_path: Local file path to upload
        remote_path: Remote path within repository (empty string for root).
                     Leading/trailing slashes are automatically removed.
        app_key: API app key (None to use stored app key)

    Returns:
        dict: Upload response data

    Raises:
        UploadError: If user has VIEWER role (no upload permission)
    """
    from pathlib import Path

    path = Path(file_path)

    if not path.is_file():
        raise ValueError(f"Not a file: {file_path}")

    with KitechClient(app_key=app_key) as client:
        return client.upload_file(
            repository_id=repository_id, file_path=path, remote_path=remote_path, show_progress=True
        )


def upload(repository_id: int, local_path: str, remote_path: str = "", app_key: str = None, show_progress: bool = True):
    """Upload a file or folder to repository.

    Args:
        repository_id: The repository ID
        local_path: Local file or folder path to upload
        remote_path: Remote path within repository (empty string for root).
                     Leading/trailing slashes are automatically removed.
        app_key: API app key (None to use stored app key)
        show_progress: Show progress messages during upload

    Returns:
        dict: Upload response data (for file) or upload statistics (for folder)

    Raises:
        UploadError: If user has VIEWER role (no upload permission)
    """
    from pathlib import Path

    path = Path(local_path)

    if path.is_file():
        # Single file upload
        with KitechClient(app_key=app_key) as client:
            return client.upload_file(
                repository_id=repository_id, file_path=path, remote_path=remote_path, show_progress=show_progress
            )
    elif path.is_dir():
        # Folder upload
        return upload_folder(
            repository_id=repository_id,
            folder_path=local_path,
            remote_path=remote_path,
            app_key=app_key,
            show_progress=show_progress,
        )
    else:
        raise ValueError(f"Path does not exist: {local_path}")


def list_repositories(app_key: str = None):
    """List available repositories.

    Args:
        app_key: API app key (None to use stored app key)

    Returns:
        list: List of Repository objects
    """
    with KitechClient(app_key=app_key) as client:
        result = client.list_repositories()
        return result["repositories"]


def upload_folder(
    repository_id: int, folder_path: str, remote_path: str = "", app_key: str = None, show_progress: bool = True
):
    """Upload a folder and all its contents to repository.

    Args:
        repository_id: The repository ID
        folder_path: Local folder path to upload
        remote_path: Remote base path within repository.
                     Leading/trailing slashes are automatically removed.
        app_key: API app key (None to use stored app key)
        show_progress: Show progress messages during upload

    Returns:
        dict: Upload statistics (uploaded count, failed count, file list)

    Raises:
        UploadError: If user has VIEWER role (no upload permission)
    """
    from pathlib import Path

    from kitech_repository.core.exceptions import UploadError

    folder = Path(folder_path)

    if not folder.is_dir():
        raise ValueError(f"Not a directory: {folder_path}")

    # Get all files in the folder
    files = list(folder.rglob("*"))
    files = [f for f in files if f.is_file()]

    uploaded = []
    failed = []

    with KitechClient(app_key=app_key) as client:
        # Check upload permission before starting (VIEWER role cannot upload)
        repos_result = client.list_repositories(page=0, limit=100)
        repository = next((r for r in repos_result["repositories"] if r.id == repository_id), None)

        if repository is None:
            raise UploadError(f"Repository {repository_id} not found")

        if repository.user_role == "VIEWER":
            raise UploadError(
                "Upload permission denied: You have VIEWER role. Only OWNER and ADMIN roles can upload files."
            )

        total_files = len(files)
        if show_progress:
            print(f"Uploading {total_files} files...")

        for idx, file_path in enumerate(files, 1):
            relative_path = file_path.relative_to(folder)
            # Build remote directory path (without filename) - same as TUI F3 logic
            # The API uses the filename from multipart form data, path is just the directory
            if relative_path.parent != Path("."):
                # File is in a subdirectory
                remote_dir_path = f"{remote_path}/{relative_path.parent}/".replace("\\", "/")
            else:
                # File is in root
                remote_dir_path = f"{remote_path}/".replace("\\", "/") if remote_path else ""
            # Normalize: remove leading slashes, keep trailing slash for directories
            remote_dir_path = remote_dir_path.lstrip("/")

            if show_progress:
                print(f"[{idx}/{total_files}] {relative_path}")

            try:
                client.upload_file(
                    repository_id=repository_id,
                    file_path=file_path,
                    remote_path=remote_dir_path,
                    show_progress=show_progress,
                    _skip_permission_check=True,  # Already checked above
                )
                uploaded.append(str(relative_path))
            except Exception as e:
                failed.append({"file": str(relative_path), "error": str(e)})
                if show_progress:
                    print(f"  ❌ Failed: {e}")

        if show_progress:
            print(f"✓ Upload complete: {len(uploaded)}/{total_files} succeeded")

    return {
        "total": len(files),
        "uploaded": len(uploaded),
        "failed": len(failed),
        "uploaded_files": uploaded,
        "failed_files": failed,
    }


def list_files(repository_id: int, path: str = "", app_key: str = None, page: int = 0, limit: int = 100):
    """List files in repository.

    Args:
        repository_id: The repository ID
        path: Path within repository (empty string for root)
        app_key: API app key (None to use stored app key)
        page: Page number (default: 0)
        limit: Number of files per page (default: 100)

    Returns:
        dict: Dictionary containing 'files' list and pagination metadata
    """
    with KitechClient(app_key=app_key) as client:
        return client.list_files(repository_id, prefix=path if path else "", page=page, limit=limit)


# Export main classes and functions
__all__ = [
    "KitechClient",
    "AuthManager",
    "Config",
    "download",
    "upload",
    "upload_file",
    "upload_folder",
    "list_repositories",
    "list_files",
]
