// fe/src/types/unified-chore.ts
// Unified Chore Types - Canonical data contract

import type { BaseEntity } from './shared'
import type { UserPublic } from './user'

/**
 * Enums - Use exact backend enum values
 */
export enum ChoreFrequency {
    ONE_TIME = 'one_time',
    DAILY = 'daily',
    WEEKLY = 'weekly',
    MONTHLY = 'monthly',
    CUSTOM = 'custom'
}

export enum ChoreType {
    PERSONAL = 'personal',
    GROUP = 'group'
}

export enum ChoreHistoryEventType {
    CREATED = 'created',
    UPDATED = 'updated',
    DELETED = 'deleted',
    COMPLETED = 'completed',
    REOPENED = 'reopened',
    ASSIGNED = 'assigned',
    UNASSIGNED = 'unassigned',
    REASSIGNED = 'reassigned',
    SCHEDULE_GENERATED = 'schedule_generated',
    DUE_DATE_CHANGED = 'due_date_changed',
    DETAILS_CHANGED = 'details_changed'
}

/**
 * Chore History
 */
export interface ChoreHistory extends BaseEntity {
    chore_id?: number | null
    group_id?: number | null
    event_type: ChoreHistoryEventType
    event_data?: Record<string, any> | null
    changed_by_user_id?: number | null
    timestamp: string
    changed_by_user?: UserPublic | null
}

export interface ChoreAssignmentHistory extends BaseEntity {
    assignment_id: number
    event_type: ChoreHistoryEventType
    event_data?: Record<string, any> | null
    changed_by_user_id?: number | null
    timestamp: string
    changed_by_user?: UserPublic | null
}

/**
 * Chore Assignment
 */
export interface ChoreAssignment extends BaseEntity {
    chore_id: number
    assigned_to_user_id: number
    due_date: string // Date as ISO string
    is_complete: boolean
    completed_at?: string | null
    assigned_user?: UserPublic | null
    history: ChoreAssignmentHistory[]
}

export interface ChoreAssignmentCreate {
    chore_id: number
    assigned_to_user_id: number
    due_date: string
}

export interface ChoreAssignmentUpdate {
    is_complete?: boolean
    due_date?: string
    assigned_to_user_id?: number
}

/**
 * Main Chore Entity
 */
export interface Chore extends BaseEntity {
    name: string
    description?: string | null
    frequency: ChoreFrequency
    custom_interval_days?: number | null
    next_due_date: string // Date as ISO string
    last_completed_at?: string | null
    type: ChoreType
    group_id?: number | null
    created_by_id: number
    parent_chore_id?: number | null

    // Relationships
    creator?: UserPublic | null
    assignments: ChoreAssignment[]
    history: ChoreHistory[]
    parent_chore?: Chore | null
    child_chores: Chore[]

    // Computed fields for UI convenience
    current_assignment?: ChoreAssignment | null
    is_overdue?: boolean
    days_until_due?: number
}

export interface ChoreCreate {
    name: string
    description?: string
    frequency: ChoreFrequency
    custom_interval_days?: number
    next_due_date: string
    type: ChoreType
    group_id?: number
    parent_chore_id?: number
    assigned_to_user_id?: number // For creating initial assignment
}

export interface ChoreUpdate {
    name?: string
    description?: string
    frequency?: ChoreFrequency
    custom_interval_days?: number
    next_due_date?: string
    type?: ChoreType
    group_id?: number
    parent_chore_id?: number
    version: number
}

/**
 * Enhanced chore with completion tracking (for UI components)
 */
export interface ChoreWithCompletion extends Chore {
    current_assignment_id: number | null
    is_completed: boolean
    completed_at: string | null
    assigned_user_name?: string
    completed_by_name?: string
    assigned_to_user_id?: number | null
    updating: boolean
    subtext?: string
}

/**
 * Time tracking for chores
 */
export interface TimeEntry extends BaseEntity {
    chore_assignment_id: number
    user_id: number
    start_time: string
    end_time?: string | null
    duration_seconds?: number | null
    user?: UserPublic | null
}

export interface TimeEntryCreate {
    chore_assignment_id: number
    start_time: string
    end_time?: string
}

/**
 * Real-time events for chores
 */
export interface ChoreRealtimeEvents {
    'chore:created': { chore: Chore }
    'chore:updated': { chore: Chore }
    'chore:deleted': { chore_id: number }
    'chore:assigned': { assignment: ChoreAssignment }
    'chore:completed': { assignment: ChoreAssignment; completed_by: UserPublic }
    'chore:reopened': { assignment: ChoreAssignment }
    'chore:editing_started': { chore_id: number; user_id: number; user_name: string }
    'chore:editing_stopped': { chore_id: number }
} 