const fs = require('fs');
const path = require('path');
const pull = require('../server/node_modules/pull-stream');
const moment = require('../server/node_modules/moment');

const agendaConfigPath = path.join(__dirname, '../configs/agenda-config.json');
const { getConfig } = require('../configs/config-manager.js');
const { buildValidatedTombstoneSet } = require('./tombstone_validator');
const logLimit = getConfig().ssbLogStream?.limit || 1000;

function readAgendaConfig() {
  if (!fs.existsSync(agendaConfigPath)) {
    fs.writeFileSync(agendaConfigPath, JSON.stringify({ discardedItems: [] }));
  }
  return JSON.parse(fs.readFileSync(agendaConfigPath));
}

function writeAgendaConfig(cfg) {
  fs.writeFileSync(agendaConfigPath, JSON.stringify(cfg, null, 2));
}

module.exports = ({ cooler, calendarsModel, eventsModel, tasksModel, marketModel, jobsModel, projectsModel, industryModel, housingModel }) => {
  let ssb;
  const openSsb = async () => { if (!ssb) ssb = await cooler.open(); return ssb; };

  const STATUS_ORDER = ['FOR SALE', 'OPEN', 'RESERVED', 'CLOSED', 'SOLD'];
  const sIdx = s => STATUS_ORDER.indexOf(String(s || '').toUpperCase());

  const fetchItems = (targetType) =>
    new Promise((resolve, reject) => {
      openSsb().then((ssbClient) => {
        pull(
          ssbClient.createLogStream({ limit: logLimit }),
          pull.collect((err, msgs) => {
            if (err) return reject(err);

            const tomb = buildValidatedTombstoneSet(msgs);
            const nodes = new Map();
            const parent = new Map();
            const child = new Map();

            for (const m of msgs) {
              const k = m.key;
              const v = m.value;
              const c = v?.content;
              if (!c) continue;
              if (c.type === 'tombstone') continue;
              if (c.type !== targetType) continue;
              if (c.encryptedPayload) continue;
              nodes.set(k, { key: k, ts: v.timestamp || 0, author: v.author, content: c });
              if (c.replaces) parent.set(k, c.replaces);
            }

            for (const [k, p] of Array.from(parent.entries())) {
              const cn = nodes.get(k);
              const pn = nodes.get(p);
              if (!pn) { parent.delete(k); continue; }
              if (!cn || String(cn.author) !== String(pn.author)) { parent.delete(k); nodes.delete(k); }
            }
            for (const [k, p] of parent.entries()) child.set(p, k);

            const rootOf = (id) => { let cur = id; while (parent.has(cur) && nodes.has(parent.get(cur))) cur = parent.get(cur); return cur; };

            const groups = new Map();
            for (const id of nodes.keys()) {
              const r = rootOf(id);
              if (!groups.has(r)) groups.set(r, new Set());
              groups.get(r).add(id);
            }

            const statusOrder = ['FOR SALE', 'OPEN', 'RESERVED', 'CLOSED', 'SOLD'];
            const sIdx = s => statusOrder.indexOf(String(s || '').toUpperCase());

            const out = [];

            for (const [root, ids] of groups.entries()) {
              const items = Array.from(ids).map(id => nodes.get(id)).filter(n => n && !tomb.has(n.key));
              if (!items.length) continue;

              let tipId = Array.from(ids).find(id => !child.has(id));
              let tip = tipId ? nodes.get(tipId) : items.reduce((a, b) => a.ts > b.ts ? a : b);

              if (targetType === 'market') {
                let chosen = items[0];
                for (const n of items) {
                  const a = sIdx(n.content.status);
                  const b = sIdx(chosen.content.status);
                  if (a > b || (a === b && n.ts > chosen.ts)) chosen = n;
                }
                const c = chosen.content;
                let status = c.status;
                if (c.deadline) {
                  const dl = moment(c.deadline);
                  if (dl.isValid() && dl.isBefore(moment()) && String(status).toUpperCase() !== 'SOLD') status = 'DISCARDED';
                }
                if (status === 'FOR SALE' && (c.stock || 0) === 0) continue;

                out.push({
                  ...c,
                  status,
                  author: chosen.author,
                  id: chosen.key,
                  tipId: chosen.key,
                  createdAt: c.createdAt || chosen.ts
                });
                continue;
              }

              if (targetType === 'job') {
                const latest = items.sort((a, b) => b.ts - a.ts)[0];
                const withSubsNode = items
                  .filter(n => Array.isArray(n.content.subscribers))
                  .sort((a, b) => b.ts - a.ts)[0];
                const subscribers = withSubsNode ? withSubsNode.content.subscribers : [];
                const latestWithStatus = items
                  .filter(n => typeof n.content.status !== 'undefined')
                  .sort((a, b) => b.ts - a.ts)[0];
                const resolvedStatus = latestWithStatus
                  ? latestWithStatus.content.status
                  : latest.content.status;

                const c = { ...latest.content, status: resolvedStatus, subscribers };

                out.push({
                  ...c,
                  author: latest.author,
                  id: latest.key,
                  tipId: latest.key,
                  createdAt: c.createdAt || latest.ts
                });
                continue;
              }

              out.push({
                ...tip.content,
                author: tip.author,
                id: tip.key,
                tipId: tip.key,
                createdAt: tip.content.createdAt || tip.ts
              });
            }

            resolve(out);
          })
        );
      }).catch(reject);
    });

  return {
    async listAgenda(filter = 'all') {
      const agendaConfig = readAgendaConfig();
      const discardedItems = agendaConfig.discardedItems || [];
      const ssbClient = await openSsb();
      const userId = ssbClient.id;

      const normalize = (arr) => arr.map(it => ({
        ...it,
        key: it.id || it.key,
        createdAt: it.createdAt || new Date().toISOString(),
        tipId: it.tipId || it.id || it.key
      }));

      const eventsViaModel = eventsModel && typeof eventsModel.listAll === 'function'
        ? eventsModel.listAll(null, 'all').then(normalize).catch(() => [])
        : fetchItems('event');

      const calendarsViaModel = calendarsModel && typeof calendarsModel.listAll === 'function'
        ? calendarsModel.listAll({ filter: 'all', viewerId: userId }).then(normalize).catch(() => [])
        : fetchItems('calendar');

      const tasksViaModel = tasksModel && typeof tasksModel.listAll === 'function'
        ? tasksModel.listAll().then(normalize).catch(() => [])
        : fetchItems('task');

      const marketViaModel = marketModel && typeof marketModel.listAllItems === 'function'
        ? marketModel.listAllItems('all').then(normalize).catch(() => [])
        : fetchItems('market');

      const jobsViaModel = jobsModel && typeof jobsModel.listJobs === 'function'
        ? jobsModel.listJobs('ALL', userId).then(normalize).catch(() => [])
        : fetchItems('job');

      const projectsViaModel = projectsModel && typeof projectsModel.listProjects === 'function'
        ? projectsModel.listProjects('all').then(normalize).catch(() => [])
        : fetchItems('project');

      const industryViaModel = industryModel && typeof industryModel.listMyBuilds === 'function'
        ? industryModel.listMyBuilds().then(normalize).catch(() => [])
        : [];

      const housingViaModel = housingModel && typeof housingModel.listHousing === 'function'
        ? housingModel.listHousing('ALL', userId).then(normalize).catch(() => [])
        : [];

      const [tasksAll, eventsAll, transfersAll, tribesAll, marketAll, reportsAll, jobsAll, projectsAll, calendarsAll, industryAll, housingAll] = await Promise.all([
        tasksViaModel,
        eventsViaModel,
        fetchItems('transfer'),
        fetchItems('tribe'),
        marketViaModel,
        fetchItems('report'),
        jobsViaModel,
        projectsViaModel,
        calendarsViaModel,
        industryViaModel,
        housingViaModel
      ]);

      const tasks = tasksAll.filter(c => Array.isArray(c.assignees) && c.assignees.includes(userId)).map(t => ({ ...t, type: 'task' }));
      const events = eventsAll.filter(c => Array.isArray(c.attendees) && c.attendees.includes(userId)).map(e => ({ ...e, type: 'event' }));
      const transfers = transfersAll.filter(c => c.from === userId || c.to === userId).map(tr => ({ ...tr, type: 'transfer' }));
      const tribes = tribesAll.filter(c => Array.isArray(c.members) && c.members.includes(userId)).map(t => ({ ...t, type: 'tribe', title: t.title }));
      const marketItems = marketAll.filter(c =>
        c.seller === userId || (Array.isArray(c.auctions_poll) && c.auctions_poll.some(b => String(b).split(':')[0] === userId))
      ).map(m => ({ ...m, type: 'market' }));
      const reports = reportsAll.filter(c => c.author === userId || (Array.isArray(c.confirmations) && c.confirmations.includes(userId))).map(r => ({ ...r, type: 'report' }));
      const jobs = jobsAll.filter(c => c.author === userId || (Array.isArray(c.subscribers) && c.subscribers.includes(userId))).map(j => ({ ...j, type: 'job', title: j.title }));
      const projects = projectsAll.map(p => ({ ...p, type: 'project' }));
      const industryBuilds = industryAll.map(b => ({ ...b, type: 'industry', title: b.title }));
      const housingPlaces = housingAll
        .filter(h => h.author === userId || (Array.isArray(h.requests) && h.requests.includes(userId)))
        .map(h => ({ ...h, type: 'housing', date: h.availableFrom || h.createdAt, requested: Array.isArray(h.requests) && h.requests.includes(userId) }));
      const myCalendars = calendarsAll
        .filter(c => c.author === userId || (Array.isArray(c.participants) && c.participants.includes(userId)));
      const calendars = myCalendars.map(c => ({ ...c, type: 'calendar' }));
      const calendarDates = [];
      if (calendarsModel && typeof calendarsModel.getDatesForCalendar === 'function') {
        for (const cal of myCalendars) {
          try {
            const dates = await calendarsModel.getDatesForCalendar(cal.id || cal.key);
            for (const d of (dates || [])) {
              calendarDates.push({
                type: 'calendarDate',
                id: d.key || d.id,
                title: d.label || cal.title || 'Calendar date',
                calendarTitle: cal.title || '',
                calendarId: cal.id || cal.key,
                date: d.date || d.createdAt,
                label: d.label || '',
                author: cal.author,
                createdAt: d.createdAt || cal.createdAt,
                status: 'OPEN'
              });
            }
          } catch (_) {}
        }
      }

      let combined = [
        ...tasks,
        ...events,
        ...transfers,
        ...tribes,
        ...marketItems,
        ...reports,
        ...jobs,
        ...projects,
        ...industryBuilds,
        ...housingPlaces,
        ...calendars,
        ...calendarDates
      ];

      let filtered;
      if (filter === 'discarded') {
        filtered = combined.filter(i => discardedItems.includes(i.id));
      } else {
        filtered = combined.filter(i => !discardedItems.includes(i.id));
        if (filter === 'tasks') filtered = filtered.filter(i => i.type === 'task');
        else if (filter === 'events') filtered = filtered.filter(i => i.type === 'event');
        else if (filter === 'transfers') filtered = filtered.filter(i => i.type === 'transfer');
        else if (filter === 'tribes') filtered = filtered.filter(i => i.type === 'tribe');
        else if (filter === 'market') filtered = filtered.filter(i => i.type === 'market');
        else if (filter === 'reports') filtered = filtered.filter(i => i.type === 'report');
        else if (filter === 'open') filtered = filtered.filter(i => String(i.status).toUpperCase() === 'OPEN');
        else if (filter === 'closed') filtered = filtered.filter(i => String(i.status).toUpperCase() === 'CLOSED');
        else if (filter === 'jobs') filtered = filtered.filter(i => i.type === 'job');
        else if (filter === 'projects') filtered = filtered.filter(i => i.type === 'project');
        else if (filter === 'industry') filtered = filtered.filter(i => i.type === 'industry');
        else if (filter === 'housing') filtered = filtered.filter(i => i.type === 'housing');
        else if (filter === 'calendars') filtered = filtered.filter(i => i.type === 'calendar' || i.type === 'calendarDate');
        else if (filter === 'today') {
          const startOfDay = new Date(); startOfDay.setHours(0, 0, 0, 0);
          const endOfDay = new Date(); endOfDay.setHours(23, 59, 59, 999);
          filtered = filtered.filter(i => {
            const d = new Date(i.date || i.startTime || i.deadline || 0).getTime();
            return d >= startOfDay.getTime() && d <= endOfDay.getTime();
          });
        }
        else if (filter === 'upcoming') {
          const now = Date.now();
          filtered = filtered.filter(i => {
            const d = new Date(i.date || i.startTime || i.deadline || 0).getTime();
            return d > now;
          });
        }
        else if (filter === 'overdue') {
          const startOfDay = new Date(); startOfDay.setHours(0, 0, 0, 0);
          filtered = filtered.filter(i => {
            const d = new Date(i.date || i.startTime || i.deadline || 0).getTime();
            const open = String(i.status || 'OPEN').toUpperCase() === 'OPEN';
            return d > 0 && d < startOfDay.getTime() && open;
          });
        }
      }

      filtered.sort((a, b) => {
        const dateA = a.date || a.startTime || a.deadline || a.createdAt || 0;
        const dateB = b.date || b.startTime || b.deadline || b.createdAt || 0;
        return new Date(dateA) - new Date(dateB);
      });

      const mainItems = combined.filter(i => !discardedItems.includes(i.id));
      const discarded = combined.filter(i => discardedItems.includes(i.id));

      const startOfDay = new Date(); startOfDay.setHours(0, 0, 0, 0);
      const endOfDay = new Date(); endOfDay.setHours(23, 59, 59, 999);
      const now = Date.now();
      const itemTs = (i) => new Date(i.date || i.startTime || i.deadline || 0).getTime();
      return {
        items: filtered,
        counts: {
          all: mainItems.length,
          open: mainItems.filter(i => String(i.status).toUpperCase() === 'OPEN').length,
          closed: mainItems.filter(i => String(i.status).toUpperCase() === 'CLOSED').length,
          tasks: mainItems.filter(i => i.type === 'task').length,
          events: mainItems.filter(i => i.type === 'event').length,
          transfers: mainItems.filter(i => i.type === 'transfer').length,
          tribes: mainItems.filter(i => i.type === 'tribe').length,
          market: mainItems.filter(i => i.type === 'market').length,
          reports: mainItems.filter(i => i.type === 'report').length,
          jobs: mainItems.filter(i => i.type === 'job').length,
          projects: mainItems.filter(i => i.type === 'project').length,
          industry: mainItems.filter(i => i.type === 'industry').length,
          housing: mainItems.filter(i => i.type === 'housing').length,
          calendars: mainItems.filter(i => i.type === 'calendar' || i.type === 'calendarDate').length,
          today: mainItems.filter(i => { const d = itemTs(i); return d >= startOfDay.getTime() && d <= endOfDay.getTime(); }).length,
          upcoming: mainItems.filter(i => itemTs(i) > now).length,
          overdue: mainItems.filter(i => { const d = itemTs(i); return d > 0 && d < startOfDay.getTime() && String(i.status || 'OPEN').toUpperCase() === 'OPEN'; }).length,
          discarded: discarded.length
        }
      };
    },

    async discardItem(itemId) {
      const agendaConfig = readAgendaConfig();
      if (!agendaConfig.discardedItems.includes(itemId)) {
        agendaConfig.discardedItems.push(itemId);
        writeAgendaConfig(agendaConfig);
      }
    },

    async restoreItem(itemId) {
      const agendaConfig = readAgendaConfig();
      agendaConfig.discardedItems = agendaConfig.discardedItems.filter(id => id !== itemId);
      writeAgendaConfig(agendaConfig);
    }
  };
};

