// fe/src/types/unified-group.ts
// Unified Group/Household Types - Canonical data contract

import type { BaseEntity } from './shared'
import type { UserPublic } from './user'
import type { ChoreHistory } from './unified-chore'

/**
 * Enums
 */
export enum UserRole {
    OWNER = 'owner',
    MEMBER = 'member'
}

/**
 * User-Group Association
 */
export interface UserGroup extends BaseEntity {
    user_id: number
    group_id: number
    role: UserRole
    joined_at: string
    user?: UserPublic | null
}

/**
 * Group/Household Entity
 */
export interface Group extends BaseEntity {
    name: string
    description?: string | null
    created_by_id: number
    creator?: UserPublic | null
    member_associations: UserGroup[]
    chore_history: ChoreHistory[]

    // Computed properties
    members: UserPublic[]
    member_count: number
    is_member?: boolean // Whether current user is a member
}

// Type alias for consistency with existing code
export type GroupPublic = Group
export type Household = Group

export interface GroupCreate {
    name: string
    description?: string
}

export interface GroupUpdate {
    name?: string
    description?: string
    version: number
}

/**
 * Invite Management
 */
export interface Invite extends BaseEntity {
    code: string
    group_id: number
    created_by_id: number
    expires_at: string
    is_active: boolean
    usage_count?: number
    max_uses?: number
    group?: Group | null
    creator?: UserPublic | null
}

export interface InviteCreate {
    group_id: number
    expires_at?: string
    max_uses?: number
}

/**
 * Member activity tracking
 */
export interface MemberActivity {
    user_id: number
    last_seen: string
    is_active: boolean
    current_action?: string
}

/**
 * Group health metrics
 */
export interface GroupHealth {
    score: number // 0-100
    status: 'excellent' | 'good' | 'needs-attention'
    active_members: number
    total_members: number
    metrics: {
        engagement_rate: number
        completion_rate: number
        collaboration_score: number
    }
}

/**
 * Real-time events for groups
 */
export interface GroupRealtimeEvents {
    'group:updated': { group: Group }
    'group:member_joined': { group_id: number; member: UserPublic }
    'group:member_left': { group_id: number; user_id: number }
    'group:member_activity': { user_id: number; last_seen: string; is_active: boolean; action?: string }
    'group:invite_created': { code: string; expires_at: string }
    'group:invite_used': { code: string; new_member: UserPublic }
}

/**
 * Group settings and preferences
 */
export interface GroupSettings {
    group_id: number
    theme?: string
    language?: string
    timezone?: string
    notifications: {
        chore_reminders: boolean
        expense_notifications: boolean
        activity_updates: boolean
    }
    features: {
        chore_rotation: boolean
        expense_splitting: boolean
        time_tracking: boolean
    }
} 