const express = require('express');
const router = express.Router();
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const sharp = require('sharp');

const OPS_ROOT = '/opt/firefrost-ops-manual';
const CACHE_DIR = '/var/cache/arbiter/branding-thumbs';
const THUMB_SIZE = 256;
const IMAGE_EXTS = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif']);

// Scan roots — both /branding and /docs/branding
const SCAN_ROOTS = [
    { label: 'branding',      rel: 'branding' },
    { label: 'docs/branding', rel: 'docs/branding' }
];

function walkImages(dir, baseRel) {
    const out = [];
    let entries;
    try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
    catch { return out; }
    for (const entry of entries) {
        if (entry.name.startsWith('.')) continue;
        const full = path.join(dir, entry.name);
        const rel = path.posix.join(baseRel, entry.name);
        if (entry.isDirectory()) {
            out.push(...walkImages(full, rel));
        } else if (entry.isFile()) {
            const ext = path.extname(entry.name).toLowerCase();
            if (IMAGE_EXTS.has(ext)) {
                try {
                    const st = fs.statSync(full);
                    out.push({
                        rel, full,
                        name: entry.name,
                        category: path.posix.dirname(rel),
                        size: st.size,
                        mtime: st.mtimeMs
                    });
                } catch { /* skip */ }
            }
        }
    }
    return out;
}

function listAllAssets() {
    const all = [];
    for (const root of SCAN_ROOTS) {
        const abs = path.join(OPS_ROOT, root.rel);
        all.push(...walkImages(abs, root.rel));
    }
    // Sort by category, then name
    all.sort((a, b) => a.category.localeCompare(b.category) || a.name.localeCompare(b.name));
    return all;
}

// GET /admin/branding-assets/list — modal body
router.get('/list', (req, res) => {
    try {
        const assets = listAllAssets();
        // Group by category
        const byCategory = {};
        for (const a of assets) {
            if (!byCategory[a.category]) byCategory[a.category] = [];
            byCategory[a.category].push(a);
        }
        res.render('admin/social-calendar/_assets', { byCategory, total: assets.length, layout: false });
    } catch (err) {
        console.error('Asset list error:', err);
        res.status(500).send("<div class='p-6 text-red-500'>Error loading assets.</div>");
    }
});

// GET /admin/branding-assets/thumb?path=branding/logos/firefrost-emblem-512.png
router.get('/thumb', async (req, res) => {
    try {
        const rel = String(req.query.path || '');
        // Security: must be under one of our scan roots, no path traversal
        const normalized = path.posix.normalize(rel);
        if (normalized.includes('..') || path.isAbsolute(normalized)) {
            return res.status(400).send('Bad path');
        }
        const allowed = SCAN_ROOTS.some(r => normalized.startsWith(r.rel + '/'));
        if (!allowed) return res.status(400).send('Out of scope');

        const source = path.join(OPS_ROOT, normalized);
        if (!fs.existsSync(source)) return res.status(404).send('Not found');
        const stat = fs.statSync(source);

        // Cache key: sha1 of path + mtime so edits bust cache automatically
        const key = crypto.createHash('sha1').update(normalized + ':' + stat.mtimeMs).digest('hex');
        const cachePath = path.join(CACHE_DIR, key + '.webp');

        if (!fs.existsSync(cachePath)) {
            await sharp(source)
                .resize(THUMB_SIZE, THUMB_SIZE, { fit: 'inside', withoutEnlargement: true })
                .webp({ quality: 80 })
                .toFile(cachePath);
        }

        res.set('Content-Type', 'image/webp');
        res.set('Cache-Control', 'private, max-age=86400');
        fs.createReadStream(cachePath).pipe(res);
    } catch (err) {
        console.error('Thumb generation error:', err);
        res.status(500).send('Thumb error');
    }
});

module.exports = router;
