#
# Copyright (c) 2021-2026 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# pylint: disable=too-many-public-methods
from asyncio import Lock
from dataclasses import replace
from typing import Self

from ahriman.core.exceptions import UnknownPackageError
from ahriman.core.log import LazyLogging
from ahriman.core.repository.package_info import PackageInfo
from ahriman.core.status import Client
from ahriman.core.status.event_bus import EventBus
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
from ahriman.models.changes import Changes
from ahriman.models.dependencies import Dependencies
from ahriman.models.event import Event, EventType
from ahriman.models.log_record import LogRecord
from ahriman.models.package import Package
from ahriman.models.pkgbuild_patch import PkgbuildPatch


class Watcher(LazyLogging):
    """
    package status watcher

    Attributes:
        client(Client): reporter instance
        event_bus(EventBus): event bus instance
        package_info(PackageInfo): package info instance
        status(BuildStatus): daemon status
    """

    def __init__(self, client: Client, package_info: PackageInfo, event_bus: EventBus) -> None:
        """
        Args:
            client(Client): reporter instance
            package_info(PackageInfo): package info instance
            event_bus(EventBus): event bus instance
        """
        self.client = client
        self.package_info = package_info
        self.event_bus = event_bus

        self._lock = Lock()
        self._known: dict[str, tuple[Package, BuildStatus]] = {}
        self.status = BuildStatus()

    async def event_add(self, event: Event) -> None:
        """
        create new event

        Args:
            event(Event): audit log event
        """
        self.client.event_add(event)

    async def event_get(self, event: str | EventType | None, object_id: str | None,
                        from_date: int | float | None = None, to_date: int | float | None = None,
                        limit: int = -1, offset: int = 0) -> list[Event]:
        """
        retrieve list of events

        Args:
            event(str | EventType | None): filter by event type
            object_id(str | None): filter by event object
            from_date(int | float | None, optional): minimal creation date, inclusive (Default value = None)
            to_date(int | float | None, optional): maximal creation date, exclusive (Default value = None)
            limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
            offset(int, optional): records offset (Default value = 0)

        Returns:
            list[Event]: list of audit log events
        """
        return self.client.event_get(event, object_id, from_date, to_date, limit, offset)

    async def load(self) -> None:
        """
        load packages from local database
        """
        async with self._lock:
            self._known = {
                package.base: (package, status)
                for package, status in self.client.package_get(None)
            }

    async def logs_rotate(self, keep_last_records: int) -> None:
        """
        remove older logs from storage

        Args:
            keep_last_records(int): number of last records to keep
        """
        self.client.logs_rotate(keep_last_records)

    async def package_archives(self, package_base: str) -> list[Package]:
        """
        get known package archives

        Args:
            package_base(str): package base

        Returns:
            list[Package]: list of built package for this package base
        """
        return self.package_info.package_archives(package_base)

    async def package_changes_get(self, package_base: str) -> Changes:
        """
        get package changes

        Args:
            package_base(str): package base to retrieve

        Returns:
            Changes: package changes if available and empty object otherwise
        """
        return self.client.package_changes_get(package_base)

    async def package_changes_update(self, package_base: str, changes: Changes) -> None:
        """
        update package changes

        Args:
            package_base(str): package base to update
            changes(Changes): changes descriptor
        """
        self.client.package_changes_update(package_base, changes)

    async def package_dependencies_get(self, package_base: str) -> Dependencies:
        """
        get package dependencies

        Args:
            package_base(str): package base to retrieve

        Returns:
            list[Dependencies]: package implicit dependencies if available
        """
        return self.client.package_dependencies_get(package_base)

    async def package_dependencies_update(self, package_base: str, dependencies: Dependencies) -> None:
        """
        update package dependencies

        Args:
            package_base(str): package base to update
            dependencies(Dependencies): dependencies descriptor
        """
        self.client.package_dependencies_update(package_base, dependencies)

    async def package_get(self, package_base: str) -> tuple[Package, BuildStatus]:
        """
        get current package base build status

        Args:
            package_base(str): package base

        Returns:
            tuple[Package, BuildStatus]: package and its status

        Raises:
            UnknownPackageError: if no package found
        """
        try:
            async with self._lock:
                return self._known[package_base]
        except KeyError:
            raise UnknownPackageError(package_base) from None

    async def package_hold_update(self, package_base: str, *, enabled: bool) -> None:
        """
        update package hold status

        Args:
            package_base(str): package base name
            enabled(bool): new hold status
        """
        package, status = await self.package_get(package_base)
        async with self._lock:
            self._known[package_base] = (package, replace(status, is_held=enabled))
        self.client.package_hold_update(package_base, enabled=enabled)

        await self.event_bus.broadcast(EventType.PackageHeld, package_base, is_held=enabled)

    async def package_logs_add(self, log_record: LogRecord) -> None:
        """
        post log record

        Args:
            log_record(LogRecord): log record
        """
        self.client.package_logs_add(log_record)

        await self.event_bus.broadcast(EventType.BuildLog, log_record.log_record_id.package_base, **log_record.view())

    async def package_logs_get(self, package_base: str, version: str | None = None, process_id: str | None = None,
                               limit: int = -1, offset: int = 0) -> list[LogRecord]:
        """
        get package logs

        Args:
            package_base(str): package base
            version(str | None, optional): package version to search (Default value = None)
            process_id(str | None, optional): process identifier to search (Default value = None)
            limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
            offset(int, optional): records offset (Default value = 0)

        Returns:
            list[LogRecord]: package logs
        """
        return self.client.package_logs_get(package_base, version, process_id, limit, offset)

    async def package_logs_remove(self, package_base: str, version: str | None) -> None:
        """
        remove package logs

        Args:
            package_base(str): package base
            version(str | None): package version to remove logs. If ``None`` is set, all logs will be removed
        """
        self.client.package_logs_remove(package_base, version)

    async def package_patches_get(self, package_base: str, variable: str | None) -> list[PkgbuildPatch]:
        """
        get package patches

        Args:
            package_base(str): package base to retrieve
            variable(str | None): optional filter by patch variable

        Returns:
            list[PkgbuildPatch]: list of patches for the specified package
        """
        return self.client.package_patches_get(package_base, variable)

    async def package_patches_remove(self, package_base: str, variable: str | None) -> None:
        """
        remove package patch

        Args:
            package_base(str): package base to update
            variable(str | None): patch name. If ``None`` is set, all patches will be removed
        """
        self.client.package_patches_remove(package_base, variable)

    async def package_patches_update(self, package_base: str, patch: PkgbuildPatch) -> None:
        """
        create or update package patch

        Args:
            package_base(str): package base to update
            patch(PkgbuildPatch): package patch
        """
        self.client.package_patches_update(package_base, patch)

    async def package_remove(self, package_base: str) -> None:
        """
        remove package base from known list if any

        Args:
            package_base(str): package base
        """
        async with self._lock:
            self._known.pop(package_base, None)
        self.client.package_remove(package_base)

        await self.event_bus.broadcast(EventType.PackageRemoved, package_base)

    async def package_status_update(self, package_base: str, status: BuildStatusEnum) -> None:
        """
        update package status

        Args:
            package_base(str): package base to update
            status(BuildStatusEnum): new build status
        """
        package, current_status = await self.package_get(package_base)
        async with self._lock:
            self._known[package_base] = (package, BuildStatus(status, is_held=current_status.is_held))
        self.client.package_status_update(package_base, status)

        await self.event_bus.broadcast(EventType.PackageStatusChanged, package_base, status=status.value)

    async def package_update(self, package: Package, status: BuildStatusEnum) -> None:
        """
        update package

        Args:
            package(Package): package description
            status(BuildStatusEnum): new build status
        """
        async with self._lock:
            _, current_status = self._known.get(package.base, (package, BuildStatus()))
            self._known[package.base] = (package, BuildStatus(status, is_held=current_status.is_held))
        self.client.package_update(package, status)

        await self.event_bus.broadcast(
            EventType.PackageUpdated, package.base, status=status.value, version=package.version,
        )

    async def packages(self) -> list[tuple[Package, BuildStatus]]:
        """
        get current known packages list

        Returns:
            list[tuple[Package, BuildStatus]]: list of packages together with their statuses
        """
        async with self._lock:
            return list(self._known.values())

    async def shutdown(self) -> None:
        """
        gracefully shutdown watcher
        """
        await self.event_bus.shutdown()

    async def status_update(self, status: BuildStatusEnum) -> None:
        """
        update service status

        Args:
            status(BuildStatusEnum): new service status
        """
        self.status = BuildStatus(status)

        await self.event_bus.broadcast(EventType.ServiceStatusChanged, None, status=status.value)

    def __call__(self, package_base: str | None) -> Self:
        """
        extract client for future calls

        Args:
            package_base(str | None): package base to validate that package exists if applicable

        Returns:
            Self: instance of self to pass calls to the client

        Raises:
            UnknownPackageError: if no package found
        """
        # keep check here instead of calling package_get to keep this method synchronized
        if package_base is not None and package_base not in self._known:
            raise UnknownPackageError(package_base)
        return self
