"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectEntity = void 0;
const typeorm_1 = require("typeorm");
/**
 * Project Entity with Hierarchical Tree Structure
 * Uses Materialized Path pattern for efficient tree traversal
 */
let ProjectEntity = class ProjectEntity {
    constructor() {
        // Computed properties
        this._ancestors = [];
        this._depth = 0;
    }
    /**
     * Calculate ancestors from materialized path
     */
    calculateAncestors() {
        if (this.path) {
            const parts = this.path.split('.');
            this._ancestors = parts.slice(0, -1);
            this._depth = parts.length - 1;
        }
    }
    /**
     * Generate path for new nodes
     */
    async generatePath() {
        if (!this.path && this.parentId) {
            // Path will be set by the service based on parent's path
            // This is a placeholder for the entity lifecycle
        }
    }
    /**
     * Update level based on path
     */
    updateLevel() {
        if (this.path) {
            this.level = this.path.split('.').length - 1;
        }
    }
    /**
     * Get all ancestor IDs from the materialized path
     */
    getAncestorIds() {
        if (!this.path)
            return [];
        const parts = this.path.split('.');
        // Remove the last part (current node)
        return parts.slice(0, -1);
    }
    /**
     * Check if this project is an ancestor of another
     */
    isAncestorOf(other) {
        if (!other.path || !this.path)
            return false;
        return other.path.startsWith(this.path + '.');
    }
    /**
     * Check if this project is a descendant of another
     */
    isDescendantOf(other) {
        if (!this.path || !other.path)
            return false;
        return this.path.startsWith(other.path + '.');
    }
    /**
     * Get the next sibling path
     */
    getNextSiblingPath() {
        if (!this.path)
            return '0001';
        const parts = this.path.split('.');
        const lastPart = parts[parts.length - 1];
        const nextNumber = (parseInt(lastPart, 10) + 1).toString().padStart(4, '0');
        if (parts.length === 1) {
            return nextNumber;
        }
        return [...parts.slice(0, -1), nextNumber].join('.');
    }
    /**
     * Check if project is a root node
     */
    isRoot() {
        return !this.parentId && this.level === 0;
    }
    /**
     * Check if project is a leaf node (has no children)
     */
    isLeaf() {
        return !this.children || this.children.length === 0;
    }
};
exports.ProjectEntity = ProjectEntity;
__decorate([
    (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
    __metadata("design:type", String)
], ProjectEntity.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'varchar', length: 255 }),
    __metadata("design:type", String)
], ProjectEntity.prototype, "name", void 0);
__decorate([
    (0, typeorm_1.Column)({ type: 'text', nullable: true }),
    __metadata("design:type", String)
], ProjectEntity.prototype, "description", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 20,
        default: 'active'
    }),
    __metadata("design:type", String)
], ProjectEntity.prototype, "status", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 1000,
        comment: 'Materialized path for tree structure (e.g., "0001.0002.0003")'
    }),
    __metadata("design:type", String)
], ProjectEntity.prototype, "path", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'int',
        default: 0,
        comment: 'Depth level in the tree hierarchy'
    }),
    __metadata("design:type", Number)
], ProjectEntity.prototype, "level", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'uuid',
        nullable: true,
        name: 'parent_project'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "parentId", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => ProjectEntity, project => project.children, {
        nullable: true,
        onDelete: 'CASCADE'
    }),
    (0, typeorm_1.JoinColumn)({ name: 'parent_project' }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "parent", void 0);
__decorate([
    (0, typeorm_1.OneToMany)(() => ProjectEntity, project => project.parent),
    __metadata("design:type", Array)
], ProjectEntity.prototype, "children", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 255,
        nullable: true,
        name: 'repository_url'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "repositoryUrl", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 50,
        nullable: true,
        name: 'bmad_workflow_type'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "bmadWorkflowType", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 255,
        nullable: true,
        name: 'task_master_project_id'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "taskMasterProjectId", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'varchar',
        length: 20,
        default: 'medium'
    }),
    __metadata("design:type", String)
], ProjectEntity.prototype, "priority", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'date',
        nullable: true,
        name: 'start_date'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "startDate", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'date',
        nullable: true,
        name: 'due_date'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "dueDate", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'int',
        default: 0,
        name: 'completion_percentage'
    }),
    __metadata("design:type", Number)
], ProjectEntity.prototype, "completionPercentage", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'uuid',
        nullable: true,
        name: 'created_by'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "createdBy", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'uuid',
        nullable: true,
        name: 'assigned_to'
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "assignedTo", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'simple-json',
        nullable: true
    }),
    __metadata("design:type", Object)
], ProjectEntity.prototype, "metadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        type: 'int',
        default: 0
    }),
    __metadata("design:type", Number)
], ProjectEntity.prototype, "sort", void 0);
__decorate([
    (0, typeorm_1.CreateDateColumn)({ name: 'date_created' }),
    __metadata("design:type", Date)
], ProjectEntity.prototype, "dateCreated", void 0);
__decorate([
    (0, typeorm_1.UpdateDateColumn)({ name: 'date_updated' }),
    __metadata("design:type", Date)
], ProjectEntity.prototype, "dateUpdated", void 0);
__decorate([
    (0, typeorm_1.AfterLoad)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ProjectEntity.prototype, "calculateAncestors", null);
__decorate([
    (0, typeorm_1.BeforeInsert)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], ProjectEntity.prototype, "generatePath", null);
__decorate([
    (0, typeorm_1.BeforeInsert)(),
    (0, typeorm_1.BeforeUpdate)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ProjectEntity.prototype, "updateLevel", null);
exports.ProjectEntity = ProjectEntity = __decorate([
    (0, typeorm_1.Entity)('projects')
], ProjectEntity);
//# sourceMappingURL=project.entity.js.map