"""Domain entities for identity and tenancy."""

from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from typing import TYPE_CHECKING
from uuid import UUID

from noteflow.domain.identity.roles import ProjectRole, WorkspaceRole
from noteflow.domain.settings.base import ExtensibleSettings
from noteflow.domain.utils.time import utc_now

if TYPE_CHECKING:
    from noteflow.domain.entities.project import ExportRules, TriggerRules


@dataclass
class User:
    """A user account in the system.

    In local-first mode, a default user is created automatically.
    For multi-user deployments, users are authenticated externally.
    """

    id: UUID
    display_name: str
    email: str | None = None
    is_default: bool = False
    created_at: datetime = field(default_factory=utc_now)
    updated_at: datetime = field(default_factory=utc_now)
    metadata: dict[str, object] = field(default_factory=dict)


@dataclass
class WorkspaceSettings(ExtensibleSettings):
    """Define workspace-level configuration defaults.

    Projects inherit from these settings unless they override them.
    Typed fields are stable features. Use `extensions` for experimental
    features that may be promoted to typed fields in future sprints.

    Example lifecycle for new settings:
        Sprint N:   extensions["retention_days"] = 90        # Experimental
        Sprint N+1: retention_days: int | None = None       # Promoted

    Attributes:
        export_rules: Default export configuration.
        trigger_rules: Default trigger configuration.
        rag_enabled: Whether RAG Q&A is enabled.
        default_summarization_template: Default template ID.
    """

    export_rules: ExportRules | None = None
    trigger_rules: TriggerRules | None = None
    rag_enabled: bool | None = None
    default_summarization_template: str | None = None


@dataclass
class Workspace:
    """A workspace for organizing meetings and resources.

    Workspaces provide multi-tenant boundaries. Each user has
    at least one workspace (default: Personal). Workspaces contain
    projects which in turn contain meetings.

    Attributes:
        id: Unique workspace identifier.
        name: User-provided workspace name.
        slug: URL-friendly identifier.
        is_default: Whether this is the user's default workspace.
        settings: Workspace-level settings and rules.
        created_at: Workspace creation timestamp.
        updated_at: Last modification timestamp.
        metadata: Extensible metadata dictionary.
    """

    id: UUID
    name: str
    slug: str | None = None
    is_default: bool = False
    settings: WorkspaceSettings = field(default_factory=WorkspaceSettings)
    created_at: datetime = field(default_factory=utc_now)
    updated_at: datetime = field(default_factory=utc_now)
    metadata: dict[str, object] = field(default_factory=dict)


@dataclass
class WorkspaceMembership:
    """Represent a user's membership in a workspace with a specific role.

    Track user-workspace relationships and permissions.
    """

    workspace_id: UUID
    user_id: UUID
    role: WorkspaceRole
    created_at: datetime = field(default_factory=utc_now)


@dataclass
class ProjectMembership:
    """Represent a user's membership in a project with a specific role.

    Note: Workspace OWNER/ADMIN always have ProjectRole.ADMIN for all
    projects in their workspace, resolved at service layer.

    Attributes:
        project_id: The project identifier.
        user_id: The user identifier.
        role: The user's role in the project.
        joined_at: When the user joined the project.
    """

    project_id: UUID
    user_id: UUID
    role: ProjectRole
    joined_at: datetime = field(default_factory=utc_now)
