/* The Upper Room — mock community data. */

const CURRENT_USER = {
  name: 'Francisco Orosa',
  photo: 'images/frank-orosa.png',
  from: 'Orlando, Florida',
  church: 'Grace City Church',
  circle: 1,
  following: 1,
  headline: 'Marketplace believer · Weighing it against the Word',
  since: '2009',
  bio: 'Husband, father, and marketplace believer in Orlando. I write about where God is moving in the economy and the everyday \u2014 always weighed against the Word. Still learning to listen, still learning to wait.',
  communities: [
    { name: 'Grace City Church', kind: 'ministry', role: 'Home church · Orlando, FL' },
    { name: 'Prophecy Voices', kind: 'ministry', role: 'Weekly prophetic word' },
  ],
};

const MY_POSTS = [
  {
    id: 'm1', author: 'Francisco Orosa', from: 'Orlando, Florida', church: 'Grace City Church', time: '2d', verified: true,
    body: 'The dollar isn\u2019t dying \u2014 it\u2019s being quietly re-plumbed. I keep hearing one word over the markets this season: realignment. Not collapse. Realignment. Bringing it before the Lord and weighing it carefully.',
    amens: 36, comments: 9,
  },
  {
    id: 'm2', author: 'Francisco Orosa', from: 'Orlando, Florida', church: 'Grace City Church', time: '5d', verified: true,
    body: 'A quiet reminder to myself this morning: the seed planted in secret is not forgotten. Keep watering it.',
    verse: { text: 'Let us not become weary in doing good, for at the proper time we will reap a harvest if we do not give up.', ref: 'Galatians 6:9' },
    amens: 91, comments: 18,
  },
];

// First-run feed: a single Welcome Post from Adam Cross (Founder AI, auto-connected for every
// new user). All demo posts removed per CEO 2026-06-18. Real posts arrive once the feed is wired.
const FEED_POSTS = [
  {
    id: 'welcome-adam',
    author: 'Adam Cross',
    from: 'Prophecy Voices',
    church: 'Founder \u00b7 The Upper Room',
    time: 'now',
    verified: true,
    body: 'Welcome to The Upper Room. This is where believers gather, work, and weigh it against the Word.\n\nI\u2019m Adam Cross \u2014 I host Prophecy Voices and write the weekly word. You\u2019re connected to me from the start, so the word will always find you here.\n\nStart by adding the people you walk with: head over to Settings to connect your contacts and invite them in. The room grows when we gather.',
    verse: { text: 'I will pour out my Spirit on all people. Your sons and daughters will prophesy.', ref: 'Acts 2:17' },
    amens: 0, comments: 0, voice: false,
  },
];

// Empty on first run — suggestions populate once the user connects contacts (Settings) or the
// "believers near you" matching is wired. Demo people removed per CEO 2026-06-18.
const SUGGESTIONS = [];

/* ── Circle page data ── */
const CIRCLE_STATS = { invites: 5, circle: 1, following: 1 };

const PENDING_INVITES = [
  { name: 'Marcus Reedy', from: 'Charlotte, NC', church: 'Elevation House of Prayer', note: 'Met you in the comments on Adam\u2019s word last week.' },
  { name: 'Grace Okafor', from: 'Houston, TX', church: 'Rivers of Living Water', note: 'Fellow intercessor — would love to gather.' },
];

const CIRCLE_SUGGESTIONS = [
  { name: 'Prophecy Voices', kind: 'ministry', descriptor: 'The weekly prophetic word, weighed against Scripture', followers: '48,200' },
  { name: 'Pastor James Zuniga', kind: 'person', descriptor: 'Living Word Fellowship · Phoenix, AZ', followers: '12,460' },
  { name: 'Bethel House of Prayer', kind: 'ministry', descriptor: 'A 24/7 prayer & worship community', followers: '9,897' },
  { name: 'Maria Gil-Flores', kind: 'person', descriptor: 'Casa de Adoración · San Antonio, TX', followers: '5,262' },
  { name: 'The Upper Room Daily', kind: 'ministry', descriptor: 'Morning Scripture & a verse to carry', followers: '21,140' },
  { name: 'Daniel Sotes', kind: 'person', descriptor: 'Mountain Gate Assembly · Denver, CO', followers: '2,661' },
  { name: 'Vino Nuevo', kind: 'ministry', descriptor: 'Bilingual worship & discipleship', followers: '3,533' },
  { name: 'Yolanda Menegazzo', kind: 'person', descriptor: 'Bayside Worship Center · Tampa, FL', followers: '14,958' },
];

/* ── Comments, keyed by post id ── */
const POST_COMMENTS = {
  p1: [
    { author: 'Maria Gil-Flores', from: 'San Antonio, TX', church: 'Casa de Adoración', time: '2h', body: 'Yes — I woke with the same heaviness over our city. Praying into this with you, Pastor.', amens: 14 },
    { author: 'Daniel Sotes', from: 'Denver, CO', church: 'Mountain Gate Assembly', time: '1h', body: 'Joel 2 has been on repeat in our prayer room all month. Confirming.', amens: 6 },
  ],
  p2: [
    { author: 'Grace Okafor', from: 'Houston, TX', church: 'Rivers of Living Water', time: '4h', body: 'This is exactly what I needed tonight. Thank you for being obedient to share it. 🙏', amens: 22 },
  ],
  m2: [
    { author: 'Pastor James Zuniga', from: 'Phoenix, AZ', church: 'Living Word Fellowship', time: '4d', body: 'Amen, brother. Galatians 6:9 carried me through a long season once.', amens: 8 },
  ],
};
const DEFAULT_COMMENTS = [
  { author: 'Thomas Fisher', from: 'Atlanta, GA', church: 'Cornerstone Chapel', time: '1h', body: 'Weighing this carefully — grateful for a room where we can test things together.', amens: 3 },
];

/* ── Alerts / notifications ──
   kind: word | amen | comment | invite | follow | reach | pray
   person → render their avatar; icon → render a copper icon tile.
   target → where a click routes: {post}, {profile}, {ministry}, {circle}. */
const ALERTS = [
  { id: 'a1', kind: 'word', name: 'Prophecy Voices', rest: 'posted this week\u2019s word — “I will pour out my Spirit on all people.” Read it and weigh it.', time: '4h', unread: true, icon: 'sparkles', target: { ministry: 'Prophecy Voices' } },
  { id: 'a2', kind: 'amen', name: 'Maria Gil-Flores', rest: 'and 23 others said Amen to your word.', time: '6h', unread: true, person: 'Maria Gil-Flores', excerpt: 'The dollar isn\u2019t dying — it\u2019s being quietly re-plumbed…', target: { post: 'm1' } },
  { id: 'a3', kind: 'comment', name: 'Pastor James Zuniga', rest: 'commented on your word: “Amen, brother. Galatians 6:9 carried me through a long season once.”', time: '8h', unread: true, person: 'Pastor James Zuniga', target: { post: 'm2' } },
  { id: 'a4', kind: 'invite', name: 'Grace Okafor', rest: 'wants to join your circle.', time: '21h', unread: false, person: 'Grace Okafor', action: 'Accept', target: { profile: 'Grace Okafor', from: 'Houston, TX', church: 'Rivers of Living Water' } },
  { id: 'a5', kind: 'follow', name: 'Daniel Sotes', rest: 'is now following your words.', time: '1d', unread: false, person: 'Daniel Sotes', action: 'Connect', target: { profile: 'Daniel Sotes', from: 'Denver, CO', church: 'Mountain Gate Assembly' } },
  { id: 'a6', kind: 'reach', name: 'Your word', rest: 'reached 128 believers this week.', time: '1d', unread: false, icon: 'trending-up', excerpt: 'A quiet reminder to myself this morning: the seed planted in secret is not forgotten…', target: { post: 'm2' } },
  { id: 'a7', kind: 'pray', name: '5 believers', rest: 'are praying with you this week. Be encouraged.', time: '2d', unread: false, icon: 'hand-heart' },
  { id: 'a8', kind: 'follow', name: 'Believers near you', rest: 'are following Bethel House of Prayer.', time: '2d', unread: false, icon: 'users', action: 'Follow', target: { ministry: 'Bethel House of Prayer' } },
];

/* ── Messages / conversations ──
   Each thread: a person + a running list of messages. `me:true` = sent by
   the current user. Guests can't reach this (Messages is member-only). */
const CONVERSATIONS = [
  {
    id: 'c1', name: 'Pastor James Zuniga', from: 'Phoenix, AZ', church: 'Living Word Fellowship', time: '2h', unread: true,
    messages: [
      { me: false, text: 'Brother, that word you shared on provision really sat with me. Mind if I read it to our prayer team?', time: 'Tue 9:14 AM' },
      { me: true, text: 'Of course — honored. Weigh it with them and let me know what the Lord confirms.', time: 'Tue 9:31 AM' },
      { me: false, text: 'Will do. Praying for your family this week. 🙏', time: '2h' },
    ],
  },
  {
    id: 'c2', name: 'Maria Gil-Flores', from: 'San Antonio, TX', church: 'Casa de Adoración', time: '1d', unread: false,
    messages: [
      { me: false, text: 'Gracias for the encouragement on my post — it meant more than you know.', time: 'Mon 6:02 PM' },
      { me: true, text: 'He sees the seed you’re planting, hermana. Keep watering it.', time: 'Mon 6:20 PM' },
    ],
  },
  {
    id: 'c3', name: 'Grace Okafor', from: 'Houston, TX', church: 'Rivers of Living Water', time: '3d', unread: false,
    messages: [
      { me: false, text: 'Hi! We connected through the comments on Adam’s word. Would love to gather sometime.', time: 'Sat 11:40 AM' },
    ],
  },
];

/* ── Gatherings ──
   Member-planned events for a local church or community. Believers tied to a
   church (via the signup field) see its gatherings and can RSVP.
   YOUR_CHURCH is resolved from the profile's `church` field — see BUILD_MAP
   for the church-resolution backend hook (website/location/service times). */
const YOUR_CHURCH = { name: 'Grace City Church', from: 'Orlando, FL', website: 'gracecitychurch.org', services: 'Sundays · 9:00 & 11:00 AM', members: 12 };

const GATHERINGS = [
  { id: 'g1', host: 'Grace City Church', isChurch: true, church: 'Grace City Church', title: 'Wednesday Night Prayer', when: 'Wed · 7:00 PM', where: 'Main Sanctuary · Orlando, FL', going: ['Maria Gil-Flores', 'Daniel Sotes', 'Grace Okafor'], goingCount: 8, mine: true },
  { id: 'g2', host: 'Pastor James Zuniga', isChurch: false, church: 'Living Word Fellowship', title: 'Worship & Word Night', when: 'Fri · 6:30 PM', where: 'Living Word Fellowship · Phoenix, AZ', going: ['Maria Gil-Flores', 'Thomas Fisher'], goingCount: 24, mine: false },
  { id: 'g3', host: 'Grace City Church', isChurch: true, church: 'Grace City Church', title: 'Sunrise Prayer Walk', when: 'Sun · 6:00 AM', where: 'Lake Eola Park · Orlando, FL', going: ['Daniel Sotes'], goingCount: 5, mine: true },
  { id: 'g4', host: 'Maria Gil-Flores', isChurch: false, church: 'Casa de Adoración', title: 'Neighborhood Outreach + Lunch', when: 'Sat · 11:00 AM', where: 'Casa de Adoración · San Antonio, TX', going: ['Grace Okafor'], goingCount: 15, mine: false },
];

const NEARBY_CHURCHES = [
  { name: 'Grace City Church', from: 'Orlando, FL', members: 12, mine: true },
  { name: 'Summit Orlando', from: 'Orlando, FL', members: 7 },
  { name: 'Status Church', from: 'Winter Park, FL', members: 4 },
  { name: 'City Beautiful Church', from: 'Orlando, FL', members: 3 },
];

/* ── Saved words (posts the member bookmarked) ──
   References post ids from FEED_POSTS / MY_POSTS so a saved word stays in
   sync with the original. The reason line is the member's own note. */
const SAVED_WORDS = [
  { id: 'p2', note: 'For the season I’m in — the harvest is just underground a little longer.' },
  { id: 'p1', note: 'Praying into "the latter rain" over Orlando.' },
  { id: 'm2', note: 'My own word, to come back to. Galatians 6:9.' },
];

/* ── Prayer wall ──
   Requests believers have laid down for the room to carry. `praying` = how
   many have tapped "I’m praying". `answered` flags testimonies. */
const PRAYER_WALL = [
  { id: 'pr1', name: 'Grace Okafor', from: 'Houston, TX', church: 'Rivers of Living Water', time: '2h', text: 'My father goes in for surgery Thursday morning. Believing for steady hands on the surgeons and peace over our family.', praying: 34, answered: false, urgent: true },
  { id: 'pr2', name: 'Daniel Sotes', from: 'Denver, CO', church: 'Mountain Gate Assembly', time: '5h', text: 'Starting a new role Monday after a long season of waiting. Pray I’d carry His presence into that workplace.', praying: 21, answered: false, urgent: false },
  { id: 'pr3', name: 'Maria Gil-Flores', from: 'San Antonio, TX', church: 'Casa de Adoración', time: '1d', text: 'Update / praise: the job came through! Thank you to everyone who stood with me. He is faithful.', praying: 58, answered: true, urgent: false },
  { id: 'pr4', name: 'Thomas Fisher', from: 'Atlanta, GA', church: 'Cornerstone Chapel', time: '2d', text: 'For my daughter’s heart to soften toward the Lord. I’m believing for her, even when it’s quiet.', praying: 40, answered: false, urgent: false },
];

/* ── Prophetic Voices Analysis — its own section ──
   The log behind the "Prophetic Voices" section: each entry is a prophetic
   voice/word, where it was surfaced, the Scripture it is WEIGHED against
   (an array — multiple comparisons), and the weighing notes + a status.

   PIPELINE: entries are NOT hand-authored at scale. AIxWrk Intelligence
   surfaces the prophetic voice → Social routes it into the section → EA
   orchestrates/finalizes. Verse text on pipeline-written entries is pulled
   from the KJV table (bible_kjv) in the AIxWrk private DB, never typed by hand.

   ENTRY SCHEMA (v1):
     id            string   stable id
     voice         string   the prophetic word/voice being weighed
     speaker       string   who/what gave it (person, ministry, or source)
     surfaced_by   string   pipeline origin — 'AIxWrk Intelligence' | 'Social' | 'member'
     source        string   where it came from (channel/url/handle) — provenance
     logged_at     string   ISO timestamp the entry was logged
     comparisons   array    Bible comparisons — [{ ref, text, relation }]
                            relation: 'confirms' | 'tempers' | 'tests' | 'contradicts'
     weighing      string   the analysis — how the voice holds up against the Word
     status        string   'pending' | 'confirmed' | 'tempered' | 'flagged'
     reviewed_by   string   role/person who weighed it (EA, member name, '')
   ── */
const PROPHETIC_ANALYSIS = [
  {
    id: 'pa1',
    voice: 'I keep hearing one word over the markets this season: realignment — not collapse. The latter rain is returning to the hungry.',
    speaker: 'Prophecy Voices',
    surfaced_by: 'AIxWrk Intelligence',
    source: 'Prophecy Voices — weekly word',
    logged_at: '2026-06-05T22:26:10-04:00',
    comparisons: [
      { ref: 'Joel 2:23', text: 'Be glad, people of Zion, rejoice in the Lord your God, for he has given you the autumn rains because he is faithful.', relation: 'confirms' },
      { ref: 'Galatians 6:9', text: 'Let us not become weary in doing good, for at the proper time we will reap a harvest if we do not give up.', relation: 'tempers' },
    ],
    weighing: 'The "latter rain / realignment" word aligns with Joel’s promise of restoration in due season — confirmed in theme. Tempered by Galatians 6:9: the harvest is "at the proper time," so the word counsels patience and steady obedience, not a dated prediction. No contradiction with Scripture; treat as encouragement, weigh dates carefully.',
    status: 'confirmed',
    reviewed_by: 'EA',
  },
];

/* ── Search corpus ──
   A flat index over people, ministries, churches, and words so the search
   screen can match a single query across every entity type. Built from the
   existing mock data — no new source of truth. */
function buildSearchIndex() {
  const people = {};
  const ministries = {};
  const churches = {};
  function addPerson(name, from, church) {
    if (!name || people[name]) return;
    people[name] = { name: name, from: from || '', church: church || '', kind: 'person' };
    if (church && !churches[church]) churches[church] = { name: church, from: from || '', kind: 'church' };
  }
  function addMinistry(name, descriptor, followers) {
    if (!name || ministries[name]) return;
    ministries[name] = { name: name, descriptor: descriptor || '', followers: followers, kind: 'ministry' };
  }
  FEED_POSTS.concat(MY_POSTS).forEach((p) => addPerson(p.author, p.from, p.church));
  SUGGESTIONS.forEach((s) => addPerson(s.name, s.from, s.church));
  PENDING_INVITES.forEach((i) => addPerson(i.name, i.from, i.church));
  CONVERSATIONS.forEach((c) => addPerson(c.name, c.from, c.church));
  CIRCLE_SUGGESTIONS.forEach((s) => { if (s.kind === 'ministry') addMinistry(s.name, s.descriptor, s.followers); else addPerson(s.name, (s.descriptor || '').split(' · ')[1], (s.descriptor || '').split(' · ')[0]); });
  NEARBY_CHURCHES.forEach((c) => { if (!churches[c.name]) churches[c.name] = { name: c.name, from: c.from, kind: 'church' }; });
  addPerson(CURRENT_USER.name, CURRENT_USER.from, CURRENT_USER.church);
  return {
    people: Object.keys(people).map((k) => people[k]),
    ministries: Object.keys(ministries).map((k) => ministries[k]),
    churches: Object.keys(churches).map((k) => churches[k]),
    words: FEED_POSTS.concat(MY_POSTS),
  };
}

Object.assign(window, { CURRENT_USER, MY_POSTS, FEED_POSTS, SUGGESTIONS, CIRCLE_STATS, PENDING_INVITES, CIRCLE_SUGGESTIONS, POST_COMMENTS, DEFAULT_COMMENTS, ALERTS, CONVERSATIONS, YOUR_CHURCH, GATHERINGS, NEARBY_CHURCHES, SAVED_WORDS, PRAYER_WALL, PROPHETIC_ANALYSIS, buildSearchIndex });
