"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectRepository = void 0;
const db_1 = require("../db/db");
const schema_1 = require("../db/schema");
const drizzle_orm_1 = require("drizzle-orm");
function generateUUID() {
    if (globalThis.crypto?.randomUUID) {
        return globalThis.crypto.randomUUID();
    }
    // Fallback for environments without crypto.randomUUID
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        const r = (Math.random() * 16) | 0;
        const v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}
class ProjectRepository {
    async createProject(data) {
        const defaultConfig = {
            trackedMetadataKeys: []
        };
        const newProject = {
            id: generateUUID(),
            name: data.name,
            description: data.description,
            createdAt: new Date().toISOString(),
            config: JSON.stringify(data.config || defaultConfig),
        };
        const drizzle = await db_1.db.getDrizzle();
        await drizzle.insert(schema_1.projects).values(newProject);
        return {
            ...newProject,
            config: data.config || defaultConfig,
        };
    }
    async getProjectById(id) {
        const drizzle = await db_1.db.getDrizzle();
        const result = await drizzle.select().from(schema_1.projects).where((0, drizzle_orm_1.eq)(schema_1.projects.id, id));
        if (!result[0])
            return undefined;
        const project = result[0];
        let config;
        try {
            config = JSON.parse(project.config);
        }
        catch {
            config = { trackedMetadataKeys: [] };
        }
        return {
            ...project,
            config,
        };
    }
    async getProjectByName(name) {
        const drizzle = await db_1.db.getDrizzle();
        const result = await drizzle.select().from(schema_1.projects).where((0, drizzle_orm_1.eq)(schema_1.projects.name, name));
        if (!result[0])
            return undefined;
        const project = result[0];
        let config;
        try {
            config = JSON.parse(project.config);
        }
        catch {
            config = { trackedMetadataKeys: [] };
        }
        return {
            ...project,
            config,
        };
    }
    async getAllProjects() {
        const drizzle = await db_1.db.getDrizzle();
        const result = await drizzle.select().from(schema_1.projects);
        return result.map(project => {
            let config;
            try {
                config = JSON.parse(project.config);
            }
            catch {
                config = { trackedMetadataKeys: [] };
            }
            return {
                ...project,
                config,
            };
        });
    }
    async updateProject(id, data) {
        const drizzle = await db_1.db.getDrizzle();
        const updateData = { ...data };
        if (data.config) {
            updateData.config = JSON.stringify(data.config);
        }
        await drizzle.update(schema_1.projects).set(updateData).where((0, drizzle_orm_1.eq)(schema_1.projects.id, id));
        return this.getProjectById(id);
    }
    async deleteProject(id) {
        const drizzle = await db_1.db.getDrizzle();
        await drizzle.delete(schema_1.projects).where((0, drizzle_orm_1.eq)(schema_1.projects.id, id));
    }
}
exports.ProjectRepository = ProjectRepository;
//# sourceMappingURL=projectRepository.js.map