import { Router } from 'express';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import { parse as parseYaml } from 'yaml';
import { detectPennyfarthingProject, loadThemeConfig, loadThemeYaml } from '../pennyfarthing.js';
export function getThemeAgents(projectDir) {
    if (!detectPennyfarthingProject(projectDir)) {
        return {};
    }
    const config = loadThemeConfig(projectDir);
    const themeName = config?.theme;
    if (!themeName)
        return {};
    // Resolve theme file path and load YAML
    const themeFile = `${themeName}.yaml`;
    const themePath = join(projectDir, '.pennyfarthing', 'personas', 'themes', themeFile);
    const themeData = loadThemeYaml(themePath);
    if (!themeData)
        return {};
    const agents = themeData;
    if (!agents)
        return {};
    const result = {};
    for (const [role, data] of Object.entries(agents)) {
        result[role] = {
            character: data?.character || role,
            shortName: data?.shortName,
        };
    }
    return result;
}
function toSlug(name) {
    return name
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-|-$/g, '');
}
function oceanSuffix(ocean) {
    return `${ocean.O}${ocean.C}${ocean.E}${ocean.A}${ocean.N}`;
}
function getFullThemeData(projectDir) {
    if (!detectPennyfarthingProject(projectDir))
        return null;
    const config = loadThemeConfig(projectDir);
    const themeSlug = config?.theme;
    if (!themeSlug)
        return null;
    const themeFile = `${themeSlug}.yaml`;
    const themePath = join(projectDir, '.pennyfarthing', 'personas', 'themes', themeFile);
    if (!existsSync(themePath))
        return null;
    try {
        const content = readFileSync(themePath, 'utf-8');
        const raw = parseYaml(content);
        if (!raw?.agents)
            return null;
        const agents = Object.entries(raw.agents).map(([role, data]) => {
            const shortName = data.shortName || data.character || role;
            const ocean = data.ocean;
            const slug = ocean ? `${toSlug(shortName)}-${oceanSuffix(ocean)}` : toSlug(shortName);
            return {
                role,
                character: data.character || role,
                shortName: data.shortName,
                style: data.style || '',
                background: data.expertise || data.role || '',
                quirks: data.quirks || [],
                slug,
                ocean,
            };
        });
        return {
            theme: themeSlug,
            themeName: raw.theme?.name || themeSlug,
            tier: raw.tier || null,
            agents,
        };
    }
    catch {
        return null;
    }
}
export function createThemeAgentsRouter(getProjectDir) {
    const router = Router();
    router.get('/', (_req, res) => {
        const projectDir = getProjectDir();
        const agents = getThemeAgents(projectDir);
        res.json({ agents });
    });
    router.get('/full', (_req, res) => {
        const projectDir = getProjectDir();
        const data = getFullThemeData(projectDir);
        if (!data) {
            return res.status(404).json({ error: 'Theme data not available' });
        }
        res.json(data);
    });
    return router;
}
//# sourceMappingURL=theme-agents.js.map