# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022-2023 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""LXD Provider class."""

from __future__ import annotations

import contextlib
import logging
from datetime import timedelta
from enum import Enum
from typing import TYPE_CHECKING

from typing_extensions import override

from craft_providers import Executor, Provider, bases
from craft_providers.base import Base
from craft_providers.errors import BaseConfigurationError

from .errors import LXDError, LXDUnstableImageError
from .installer import ensure_lxd_is_ready, install, is_installed
from .launcher import launch
from .lxc import LXC
from .lxd_instance import LXDInstance
from .remotes import get_remote_image

if TYPE_CHECKING:
    import pathlib
    from collections.abc import Callable, Collection, Iterator
    from enum import Enum

    from craft_providers import Executor
    from craft_providers.base import Base

logger = logging.getLogger(__name__)


class LXDProvider(Provider):
    """LXD build environment provider.

    This class is not stable and is likely to change. This class will be stable and
    recommended for use in the release of craft-providers 2.0.

    :param lxc: Optional lxc client to use.
    :param lxd_project: LXD project to use (default is default).
    :param lxd_remote: LXD remote to use (default is local).
    :param intercept_mknod: If the host can, tell LXD instance to intercept mknod
    """

    def __init__(
        self,
        *,
        lxc: LXC | None = None,
        lxd_project: str = "default",
        lxd_remote: str = "local",
        intercept_mknod: bool = True,
    ) -> None:
        self.lxc = lxc or LXC()
        self.lxd_project = lxd_project
        self.lxd_remote = lxd_remote
        self._intercept_mknod = intercept_mknod

    @property
    def name(self) -> str:
        """Name of the provider."""
        return "LXD"

    @property
    def install_recommendation(self) -> str:
        """Recommended way to install the provider."""
        return "Visit https://ubuntu.com/lxd/install for instructions to install LXD."

    @classmethod
    def ensure_provider_is_available(cls) -> None:
        """Ensure provider is available and ready, installing if required.

        :raises LXDInstallationError: if LXD cannot be installed
        :raises LXDError: if provider is not available
        """
        if not is_installed():
            install()
        ensure_lxd_is_ready()

    @classmethod
    def is_provider_installed(cls) -> bool:
        """Check if provider is installed.

        :returns: True if installed.
        """
        return is_installed()

    @override
    def list_instances(
        self,
        *,
        project_name: str | None = None,
        instance_name_prefix: str | None = None,
        include_base_instances: bool = False,
    ) -> Collection[LXDInstance]:
        """Get a collection of all existing instances for this LXD provider."""
        project = project_name or self.lxd_project
        names = self.lxc.list_names(project=project, remote=self.lxd_remote)

        instances: list[LXDInstance] = []

        for name in names:
            if name.startswith("base-instance-") and not include_base_instances:
                continue
            if instance_name_prefix and not name.startswith(instance_name_prefix):
                continue

            instances.append(
                LXDInstance(
                    name=name, project=project, remote=self.lxd_remote, lxc=self.lxc
                )
            )

        return instances

    def create_environment(self, *, instance_name: str) -> Executor:
        """Create a bare environment for specified base.

        No initializing, launching, or cleaning up of the environment occurs.

        :param instance_name: Name of the instance.
        """
        return LXDInstance(
            name=instance_name,
            project=self.lxd_project,
            remote=self.lxd_remote,
            intercept_mknod=self._intercept_mknod,
        )

    @override
    @contextlib.contextmanager
    def launched_environment(
        self,
        *,
        project_name: str,
        project_path: pathlib.Path,
        base_configuration: Base[Enum],
        instance_name: str,
        allow_unstable: bool = False,
        shutdown_delay_mins: int | None = None,
        use_base_instance: bool = True,
        prepare_instance: Callable[[Executor], None] | None = None,
        instance_architecture: str | None = None,
    ) -> Iterator[Executor]:
        """Configure and launch environment for specified base.

        When this method loses context, all directories are unmounted and the
        environment is stopped. For more control of environment setup and teardown,
        use `create_environment()` instead.

        :param project_name: Name of project.
        :param project_path: Path to project.
        :param base_configuration: Base configuration to apply to instance.
        :param instance_name: Name of the instance to launch.
        :param allow_unstable: If true, allow unstable images to be launched.
        :param use_base_instance: Enable base instances to reduce setup time.
        :param shutdown_delay_mins: Minutes by which to delay shutdown when exiting
            the instance.
        :param prepare_instance: A callback to perform early instance configuration
            before the base image setup.
        :param instance_architecture: A string representing the architecture to request.
            For example, on amd64 both ``amd64`` and ``i386`` are valid.

        :raises LXDError: if instance cannot be configured and launched.
        """
        image = get_remote_image(base_configuration)
        image.add_remote(lxc=self.lxc)

        # only allow launching unstable images when opted-in with `allow_unstable`
        if not image.is_stable and not allow_unstable:
            raise LXDUnstableImageError(
                brief=(
                    f"Cannot launch an unstable image {image.image_name!r} from remote "
                    f"{image.remote_name!r}"
                ),
            )

        # unstable images should be refreshed more often
        expiration = timedelta(days=90) if image.is_stable else timedelta(days=14)

        image_name = (
            image.image_name
            if instance_architecture is None
            else f"{image.image_name}/{instance_architecture}"
        )

        try:
            instance = launch(
                name=instance_name,
                base_configuration=base_configuration,
                image_name=image_name,
                image_remote=image.remote_name,
                auto_clean=True,
                auto_create_project=True,
                map_user_uid=True,
                uid=project_path.stat().st_uid,
                use_base_instance=use_base_instance,
                project=self.lxd_project,
                remote=self.lxd_remote,
                expiration=expiration,
                prepare_instance=prepare_instance,
            )
        except BaseConfigurationError as error:
            raise LXDError(str(error)) from error

        # It would be ideal to check if the instance is compatible before creating/launching
        # it, but unfortunately image_name can be a lot of things, so we have to read the OS
        # release from the running instance.
        bases.ensure_guest_compatible(
            base_configuration,
            instance,
            self.lxc.get_server_version(),
        )

        try:
            yield instance
        finally:
            # Ensure to unmount everything and stop instance upon completion.
            instance.unmount_all()
            instance.stop(delay_mins=shutdown_delay_mins)
