import { RUNTIME_TYPES } from '../constants.js';
export class TeamModel {
    id;
    name;
    description;
    members;
    projectIds;
    hierarchical;
    leaderId;
    leaderIds;
    templateId;
    parentTeamId;
    createdAt;
    updatedAt;
    constructor(data) {
        this.id = data.id || '';
        this.name = data.name || '';
        this.description = data.description;
        this.members = data.members || [];
        this.projectIds = data.projectIds || [];
        this.hierarchical = data.hierarchical;
        this.leaderIds = data.leaderIds;
        this.leaderId = data.leaderId;
        this.templateId = data.templateId;
        this.parentTeamId = data.parentTeamId;
        this.createdAt = data.createdAt || new Date().toISOString();
        this.updatedAt = data.updatedAt || new Date().toISOString();
    }
    assignToProject(projectId) {
        if (!this.projectIds.includes(projectId)) {
            this.projectIds.push(projectId);
        }
        this.updatedAt = new Date().toISOString();
    }
    unassignFromProject(projectId) {
        this.projectIds = this.projectIds.filter(id => id !== projectId);
        this.updatedAt = new Date().toISOString();
    }
    addMember(member) {
        this.members.push(member);
        this.updatedAt = new Date().toISOString();
    }
    removeMember(memberId) {
        this.members = this.members.filter(member => member.id !== memberId);
        this.updatedAt = new Date().toISOString();
    }
    updateMember(memberId, updates) {
        const memberIndex = this.members.findIndex(member => member.id === memberId);
        if (memberIndex !== -1) {
            this.members[memberIndex] = {
                ...this.members[memberIndex],
                ...updates,
                updatedAt: new Date().toISOString()
            };
            this.updatedAt = new Date().toISOString();
        }
    }
    assignTicketsToMember(memberId, ticketIds) {
        const memberIndex = this.members.findIndex(member => member.id === memberId);
        if (memberIndex !== -1) {
            this.members[memberIndex].currentTickets = ticketIds;
            this.members[memberIndex].updatedAt = new Date().toISOString();
            this.updatedAt = new Date().toISOString();
        }
    }
    toJSON() {
        return {
            id: this.id,
            name: this.name,
            description: this.description,
            members: this.members,
            projectIds: this.projectIds,
            ...(this.hierarchical !== undefined ? { hierarchical: this.hierarchical } : {}),
            ...(this.leaderId !== undefined ? { leaderId: this.leaderId } : {}),
            ...(this.leaderIds !== undefined ? { leaderIds: this.leaderIds } : {}),
            ...(this.templateId !== undefined ? { templateId: this.templateId } : {}),
            ...(this.parentTeamId !== undefined ? { parentTeamId: this.parentTeamId } : {}),
            createdAt: this.createdAt,
            updatedAt: this.updatedAt,
        };
    }
    static fromJSON(data) {
        // Handle data migration from legacy format
        const migratedData = { ...data };
        // Migration: convert legacy currentProject to projectIds
        if (data.currentProject && !data.projectIds) {
            migratedData.projectIds = [data.currentProject];
        }
        else {
            migratedData.projectIds = data.projectIds || [];
        }
        // Migration: sync leaderId and leaderIds
        if (migratedData.leaderIds && migratedData.leaderIds.length > 0) {
            // leaderIds is the source of truth; sync leaderId for backward compat
            migratedData.leaderId = migratedData.leaderIds[0];
        }
        else if (migratedData.leaderId && !migratedData.leaderIds) {
            // Legacy data: only has leaderId, migrate to leaderIds
            migratedData.leaderIds = [migratedData.leaderId];
        }
        // Migrate legacy status fields for team members
        if (migratedData.members) {
            migratedData.members = migratedData.members.map((member) => {
                const migratedMember = { ...member };
                // Migrate legacy 'status' field to 'agentStatus'
                if (member.status && !member.agentStatus) {
                    // Map legacy status values to new agentStatus values
                    switch (member.status) {
                        case 'active':
                            migratedMember.agentStatus = 'active';
                            break;
                        case 'idle':
                        case 'inactive':
                            migratedMember.agentStatus = 'inactive';
                            break;
                        case 'activating':
                            migratedMember.agentStatus = 'activating';
                            break;
                        default:
                            migratedMember.agentStatus = 'inactive';
                    }
                    // Remove legacy field
                    delete migratedMember.status;
                }
                // Ensure workingStatus exists (default to 'idle')
                if (!migratedMember.workingStatus) {
                    migratedMember.workingStatus = 'idle';
                }
                // Ensure agentStatus exists (default to 'inactive')
                // CRITICAL: Only set default for truly missing fields, preserve 'activating'/'active' states
                if (migratedMember.agentStatus === undefined || migratedMember.agentStatus === null) {
                    migratedMember.agentStatus = 'inactive';
                }
                // Ensure runtimeType exists (default to 'claude-code')
                if (!migratedMember.runtimeType) {
                    migratedMember.runtimeType = RUNTIME_TYPES.CLAUDE_CODE;
                }
                return migratedMember;
            });
        }
        return new TeamModel(migratedData);
    }
}
//# sourceMappingURL=Team.js.map