"""Generic event models for service provider notifications."""

from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any

from notify_bridge_core.models.media import MediaAsset
from notify_bridge_core.providers.base import ServiceProviderType


class EventType(str, Enum):
    """Types of events a service provider can emit."""

    # Immich events
    ASSETS_ADDED = "assets_added"
    ASSETS_REMOVED = "assets_removed"
    COLLECTION_RENAMED = "collection_renamed"
    COLLECTION_DELETED = "collection_deleted"
    SHARING_CHANGED = "sharing_changed"

    # Gitea events
    PUSH = "push"
    ISSUE_OPENED = "issue_opened"
    ISSUE_CLOSED = "issue_closed"
    ISSUE_COMMENTED = "issue_commented"
    PR_OPENED = "pr_opened"
    PR_CLOSED = "pr_closed"
    PR_MERGED = "pr_merged"
    PR_COMMENTED = "pr_commented"
    RELEASE_PUBLISHED = "release_published"

    # Planka events
    CARD_CREATED = "card_created"
    CARD_UPDATED = "card_updated"
    CARD_MOVED = "card_moved"
    CARD_DELETED = "card_deleted"
    CARD_COMMENTED = "card_commented"
    COMMENT_UPDATED = "comment_updated"
    BOARD_CREATED = "board_created"
    BOARD_UPDATED = "board_updated"
    BOARD_DELETED = "board_deleted"
    LIST_CREATED = "list_created"
    LIST_UPDATED = "list_updated"
    LIST_DELETED = "list_deleted"
    ATTACHMENT_CREATED = "attachment_created"
    CARD_LABEL_ADDED = "card_label_added"
    TASK_COMPLETED = "task_completed"

    # Scheduler events
    SCHEDULED_MESSAGE = "scheduled_message"

    # Generic Webhook events
    WEBHOOK_RECEIVED = "webhook_received"

    # NUT (Network UPS Tools) events
    UPS_ONLINE = "ups_online"
    UPS_ON_BATTERY = "ups_on_battery"
    UPS_LOW_BATTERY = "ups_low_battery"
    UPS_BATTERY_RESTORED = "ups_battery_restored"
    UPS_COMMS_LOST = "ups_comms_lost"
    UPS_COMMS_RESTORED = "ups_comms_restored"
    UPS_REPLACE_BATTERY = "ups_replace_battery"
    UPS_OVERLOAD = "ups_overload"

    # Home Assistant events
    HA_STATE_CHANGED = "ha_state_changed"
    HA_AUTOMATION_TRIGGERED = "ha_automation_triggered"
    HA_SERVICE_CALLED = "ha_service_called"
    HA_EVENT_FIRED = "ha_event_fired"

    # Bridge self-monitoring events — emitted by the bridge itself when
    # internal failures cross configured thresholds.
    BRIDGE_SELF_POLL_FAILURES = "bridge_self_poll_failures"
    BRIDGE_SELF_DEFERRED_BACKLOG = "bridge_self_deferred_backlog"
    BRIDGE_SELF_TARGET_FAILURES = "bridge_self_target_failures"


@dataclass
class ServiceEvent:
    """A generic event emitted by a service provider.

    Contains all information needed to render a notification template.
    Provider-specific data goes in the `extra` dict.
    """

    event_type: EventType
    provider_type: ServiceProviderType
    provider_name: str
    collection_id: str
    collection_name: str
    timestamp: datetime

    # Change details
    added_assets: list[MediaAsset] = field(default_factory=list)
    removed_asset_ids: list[str] = field(default_factory=list)
    added_count: int = 0
    removed_count: int = 0

    # For renames
    old_name: str | None = None
    new_name: str | None = None

    # For sharing changes
    old_shared: bool | None = None
    new_shared: bool | None = None

    # Provider-specific extra data
    extra: dict[str, Any] = field(default_factory=dict)
