const express = require('express');
const router = express.Router();
const db = require('../../database');

const PLATFORMS = ['tiktok','facebook','instagram','x','bluesky','youtube','twitch','reddit'];
const STATUSES = ['draft','ready','scheduled','published','skipped'];

// Parse ISO date (YYYY-MM-DD) into Monday of that week (local time)
function weekStart(dateStr) {
    const d = dateStr ? new Date(dateStr + 'T00:00:00') : new Date();
    const day = d.getDay(); // 0=Sun..6=Sat
    const diff = day === 0 ? -6 : 1 - day; // shift to Monday
    d.setDate(d.getDate() + diff);
    d.setHours(0,0,0,0);
    return d;
}

function toISODate(d) {
    return d.toISOString().slice(0,10);
}

// Split comma/space/newline hashtag input into array, strip leading #, dedupe
function parseHashtags(raw) {
    if (!raw) return [];
    return [...new Set(
        String(raw).split(/[\s,]+/).map(t => t.replace(/^#+/, '').trim()).filter(Boolean)
    )];
}

// Shell route
router.get('/', (req, res) => {
    const start = weekStart(req.query.week);
    res.render('admin/social-calendar/index', {
        title: 'Social Calendar',
        weekStartISO: toISODate(start),
        PLATFORMS, STATUSES
    });
});

// HTMX week view
router.get('/week', async (req, res) => {
    try {
        const start = weekStart(req.query.week);
        const end = new Date(start); end.setDate(end.getDate() + 7);
        const { rows: plans } = await db.query(`
            SELECT id, scheduled_at, platforms, status, caption, hashtags,
                   media_notes, link, assigned_to, notes
            FROM social_post_plans
            WHERE scheduled_at >= $1 AND scheduled_at < $2
            ORDER BY scheduled_at ASC
        `, [start, end]);

        // Group by ISO date of scheduled_at
        const byDay = {};
        for (let i = 0; i < 7; i++) {
            const d = new Date(start); d.setDate(d.getDate() + i);
            byDay[toISODate(d)] = { date: d, plans: [] };
        }
        for (const p of plans) {
            const key = toISODate(new Date(p.scheduled_at));
            if (byDay[key]) byDay[key].plans.push(p);
        }

        const prev = new Date(start); prev.setDate(prev.getDate() - 7);
        const next = new Date(start); next.setDate(next.getDate() + 7);

        res.render('admin/social-calendar/_week', {
            byDay, PLATFORMS, STATUSES,
            weekStartISO: toISODate(start),
            prevWeekISO: toISODate(prev),
            nextWeekISO: toISODate(next),
            thisWeekISO: toISODate(weekStart()),
            layout: false
        });
    } catch (err) {
        console.error('Social calendar week error:', err);
        res.status(500).send("<div class='text-red-500 p-4'>Error loading calendar.</div>");
    }
});

// Create plan
router.post('/', async (req, res) => {
    const { scheduled_at, platforms, status, caption, hashtags, media_notes, link, assigned_to, notes } = req.body;
    const adminUsername = req.user.username;
    try {
        const platformsArr = Array.isArray(platforms) ? platforms : (platforms ? [platforms] : []);
        const cleanPlatforms = platformsArr.filter(p => PLATFORMS.includes(p));
        const cleanStatus = STATUSES.includes(status) ? status : 'draft';
        const tagArr = parseHashtags(hashtags);
        await db.query(`
            INSERT INTO social_post_plans
                (scheduled_at, platforms, status, caption, hashtags, media_notes, link, assigned_to, notes, created_by)
            VALUES ($1, $2::social_platform[], $3, $4, $5, $6, $7, $8, $9, $10)
        `, [scheduled_at, cleanPlatforms, cleanStatus, caption || '', tagArr,
            media_notes || null, link || null, assigned_to || null, notes || null, adminUsername]);
        res.set('HX-Trigger', 'refresh-week').status(201).send('');
    } catch (err) {
        console.error('Create plan error:', err);
        res.status(500).send(`<div class='text-red-500 text-xs'>${err.message}</div>`);
    }
});

// Update plan
router.post('/:id/update', async (req, res) => {
    const { id } = req.params;
    const { scheduled_at, platforms, status, caption, hashtags, media_notes, link, assigned_to, notes } = req.body;
    try {
        const platformsArr = Array.isArray(platforms) ? platforms : (platforms ? [platforms] : []);
        const cleanPlatforms = platformsArr.filter(p => PLATFORMS.includes(p));
        const cleanStatus = STATUSES.includes(status) ? status : 'draft';
        const tagArr = parseHashtags(hashtags);
        await db.query(`
            UPDATE social_post_plans
            SET scheduled_at=$1, platforms=$2::social_platform[], status=$3, caption=$4,
                hashtags=$5, media_notes=$6, link=$7, assigned_to=$8, notes=$9, updated_at=NOW()
            WHERE id=$10
        `, [scheduled_at, cleanPlatforms, cleanStatus, caption || '', tagArr,
            media_notes || null, link || null, assigned_to || null, notes || null, id]);
        res.set('HX-Trigger', 'refresh-week').status(200).send('');
    } catch (err) {
        console.error('Update plan error:', err);
        res.status(500).send(`<div class='text-red-500 text-xs'>${err.message}</div>`);
    }
});

// Delete plan
router.post('/:id/delete', async (req, res) => {
    const { id } = req.params;
    try {
        await db.query(`DELETE FROM social_post_plans WHERE id=$1`, [id]);
        res.set('HX-Trigger', 'refresh-week').status(200).send('');
    } catch (err) {
        console.error('Delete plan error:', err);
        res.status(500).send(`<div class='text-red-500 text-xs'>${err.message}</div>`);
    }
});

// Edit form (returns modal body)
router.get('/:id/edit', async (req, res) => {
    try {
        const { rows } = await db.query(`SELECT * FROM social_post_plans WHERE id=$1`, [req.params.id]);
        if (!rows[0]) return res.status(404).send('Not found');
        res.render('admin/social-calendar/_form', {
            plan: rows[0], PLATFORMS, STATUSES, layout: false
        });
    } catch (err) {
        console.error('Edit form error:', err);
        res.status(500).send('Error');
    }
});

// New form (blank modal body, optionally prefilled with date)
router.get('/new', (req, res) => {
    const date = req.query.date || new Date().toISOString().slice(0,10);
    const plan = { id: null, scheduled_at: `${date}T12:00`, platforms: [], status: 'draft',
                   caption: '', hashtags: [], media_notes: '', link: '', assigned_to: '', notes: '' };
    res.render('admin/social-calendar/_form', { plan, PLATFORMS, STATUSES, layout: false });
});

module.exports = router;
