/* ============================================
   DATA — users, projects, work types, tasks
   ============================================ */

const PALETTE = [
  '#6D49F2','#EC4899','#F59E0B','#10B981','#3B82F6','#F97316',
  '#06B6D4','#84CC16','#8B5CF6','#14B8A6','#D946EF','#F43F5E',
];

const USERS = [
  { id: 'u1', name: 'Phurichaya Sirisansaneeyakul', short: 'Phurichaya', role: 'UI Designer',     color: '#6D49F2', email: 'phurichaya@dtgo.com', access: 'Admin',   lastActive: 'Just now' },
  { id: 'u2', name: 'Jirinee Tiangtham',            short: 'Jirinee',    role: 'UI Designer',     color: '#EC4899', email: 'jirinee@dtgo.com',    access: 'Member',  lastActive: '2 hours ago' },
  { id: 'u3', name: 'Jiranuch Chotanarungrueang',   short: 'Jiranuch',   role: 'Project Manager', color: '#F59E0B', email: 'jiranuch@dtgo.com',   access: 'Admin',   lastActive: '5 min ago' },
  { id: 'u4', name: 'Khanit Thainiyom',             short: 'Khanit',     role: 'Front-End Dev',   color: '#10B981', email: 'khanit@dtgo.com',     access: 'Manager', lastActive: '1 hour ago' },
  { id: 'u5', name: 'Pramote Neamsawat',            short: 'Pramote',    role: 'Video Producer',  color: '#3B82F6', email: 'pramote@dtgo.com',    access: 'Member',  lastActive: 'Yesterday' },
  { id: 'u6', name: 'Veeravith Binabdullah',        short: 'Veeravith',  role: 'Front-End Dev',   color: '#F97316', email: 'veeravith@dtgo.com',  access: 'Member',  lastActive: '3 hours ago' },
  { id: 'u7', name: 'Rungroj Madisara',             short: 'Rungroj',    role: 'SEO Specialist',  color: '#06B6D4', email: 'rungroj@dtgo.com',    access: 'Member',  lastActive: 'Yesterday' },
  { id: 'u8', name: 'Sureeporn Tuntiwannasin',      short: 'Sureeporn',  role: 'Marketing',       color: '#84CC16', email: 'sureeporn@dtgo.com',  access: 'Manager', lastActive: '30 min ago' },
  { id: 'u9', name: 'Amnuayporn Jankham',           short: 'Amnuayporn', role: 'Graphic Designer',color: '#8B5CF6', email: 'amnuayporn@dtgo.com', access: 'Member',  lastActive: '4 days ago' },
  { id: 'u10', name: 'Kongyot Rojphutthikul',       short: 'Kongyot',    role: 'UX Researcher',   color: '#14B8A6', email: 'kongyot@dtgo.com',    access: 'Guest',   lastActive: '2 weeks ago' },
];

const PROJECTS = [
  { id: 'whz-cn', name: 'Whizdom Connect',         color: '#6D49F2', favorite: true },
  { id: 'forest', name: 'The Forestias',           color: '#10B981', favorite: true },
  { id: 'mqdc',   name: 'MQDC',                    color: '#EC4899', favorite: true },
  { id: 'nebula', name: 'Nebula',                  color: '#3B82F6', favorite: false },
  { id: 'aspen',  name: 'The Aspen Tree',          color: '#F59E0B', favorite: false },
  { id: 'mulb',   name: 'Mulberry Grove Sukhumvit',color: '#F97316', favorite: false },
  { id: 'mag-r',  name: 'Magnolias Ratchadamri',   color: '#8B5CF6', favorite: false },
  { id: 'cet',    name: 'Customer Experience Tech',color: '#06B6D4', favorite: false },
  { id: 'risc',   name: 'RISC',                    color: '#14B8A6', favorite: false },
];

const WORK_TYPES = [
  'UI Designer','UX Designer','Front-End','Graphic Design','VIDEO','EDM','E-News',
  'SEO','GA & GTM','Online Marketing','Website Maintenance','Website revised',
  'Updated Content','SMS','Survey','AR/VR','360 Vitarul Tour','AI','LIVE','Event',
  'Collaborated','New Project',
];

const STATUSES = [
  { key: 'Backlog',        color: '#94A3B8', label: 'Backlog' },
  { key: 'In progress',    color: '#3B82F6', label: 'In Progress' },
  { key: 'Hold / Pending', color: '#F59E0B', label: 'On Hold' },
  { key: 'Done',           color: '#10B981', label: 'Done' },
  { key: 'Cancel',         color: '#EF4444', label: 'Cancelled' },
  { key: 'Archived',       color: '#64748B', label: 'Archived' },
];

const PRIORITIES = ['Highest','High','Medium','Low'];

// Generate dates near "today" (we treat May 19, 2026 as today)
const TODAY = new Date(2026, 4, 19);
const d = (offset) => {
  const dt = new Date(TODAY); dt.setDate(dt.getDate() + offset);
  return dt.toISOString().split('T')[0];
};

const TASKS = [
  // Backlog
  { id: 'MT2026-UI-0142', name: '[Whizdom Connect] Design landing page hero section', project: 'whz-cn',
    description: 'ออกแบบ hero section ของหน้า landing page ใหม่ ให้รองรับ video background และ CTA หลัก 2 ตัว',
    workType: 'UI Designer', priority: 'High', status: 'Backlog',
    assignee: 'u1', pic: 'u3', tags: ['design', 'web'],
    startDate: d(2), dueDate: d(12), createdAt: d(-3),
    checklist: [
      { id: 'c1', text: 'Research competitor heroes', done: true },
      { id: 'c2', text: 'Wireframe options × 3', done: false },
      { id: 'c3', text: 'Hi-fi design + responsive', done: false },
      { id: 'c4', text: 'Get sign-off from PM', done: false },
    ],
    comments: [], attachments: [], recurring: null },

  { id: 'MT2026-VID-0143', name: '[The Forestias] Drone shoot for May campaign', project: 'forest',
    description: 'ถ่าย drone shot รอบโครงการช่วงเช้า golden hour สำหรับใช้ใน campaign เดือนพฤษภาคม',
    workType: 'VIDEO', priority: 'Medium', status: 'Backlog',
    assignee: 'u5', pic: 'u3', tags: ['video', 'shoot'],
    startDate: d(5), dueDate: d(8), createdAt: d(-1),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-SEO-0144', name: '[MQDC] Audit & fix Core Web Vitals on corporate site', project: 'mqdc',
    description: 'CLS score กำลังตก ต้อง audit รูปและ font loading ทั้ง site',
    workType: 'SEO', priority: 'Low', status: 'Backlog',
    assignee: 'u7', pic: 'u3', tags: ['seo', 'performance'],
    startDate: d(7), dueDate: d(21), createdAt: d(-5),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-FE-0145', name: '[Nebula] Migrate blog CMS from Webflow to Sanity', project: 'nebula',
    description: '', workType: 'Front-End', priority: 'Medium', status: 'Backlog',
    assignee: 'u4', pic: 'u3', tags: ['dev', 'cms'],
    startDate: d(10), dueDate: d(30), createdAt: d(-2),
    checklist: [], comments: [], attachments: [], recurring: null },

  // In Progress
  { id: 'MT2026-UI-0136', name: '[The Forestias] Redesign property detail page', project: 'forest',
    description: 'ปรับโครงสร้างหน้า property detail ใหม่ทั้งหมด โดยเน้น gallery, virtual tour และ price comparison แบบ inline',
    workType: 'UI Designer', priority: 'Highest', status: 'In progress',
    assignee: 'u1', pic: 'u3', tags: ['design', 'web', 'sprint-12'],
    startDate: d(-7), dueDate: d(3), createdAt: d(-10),
    checklist: [
      { id: 'c1', text: 'Gallery component spec', done: true },
      { id: 'c2', text: 'Tour embed prototype', done: true },
      { id: 'c3', text: 'Mobile layout pass', done: false },
      { id: 'c4', text: 'Dark mode variants', done: false },
      { id: 'c5', text: 'Hand-off to dev', done: false },
    ],
    comments: [
      { id: 'cm1', author: 'u3', text: '@u1 อย่าลืม dark mode นะ ลูกค้าขอ', time: d(-2) },
      { id: 'cm2', author: 'u1', text: 'จัดให้ค่ะ เดี๋ยวทำพรุ่งนี้', time: d(-1) },
    ],
    attachments: [{ id: 'a1', name: 'property-detail-v3.fig', size: '12.4 MB', type: 'figma' }],
    recurring: null },

  { id: 'MT2026-FE-0137', name: '[Whizdom Connect] Implement booking flow with calendar picker', project: 'whz-cn',
    description: 'ต่อ API กับ booking system ของ team b\'\'',
    workType: 'Front-End', priority: 'High', status: 'In progress',
    assignee: 'u4', pic: 'u3', tags: ['dev', 'booking'],
    startDate: d(-5), dueDate: d(5), createdAt: d(-8),
    checklist: [
      { id: 'c1', text: 'API contract review', done: true },
      { id: 'c2', text: 'Date picker component', done: true },
      { id: 'c3', text: 'Form validation', done: false },
      { id: 'c4', text: 'Success/error states', done: false },
    ],
    comments: [
      { id: 'cm1', author: 'u4', text: 'API ยัง return 500 บ่อยมาก รอ backend แก้อยู่', time: d(-1) },
    ],
    attachments: [], recurring: null },

  { id: 'MT2026-VID-0138', name: '[MQDC] Edit corporate brand video Q2', project: 'mqdc',
    description: 'ตัดต่อ video corporate Q2 ความยาว 90 วินาที สำหรับ social และ event',
    workType: 'VIDEO', priority: 'High', status: 'In progress',
    assignee: 'u5', pic: 'u3', tags: ['video', 'brand'],
    startDate: d(-10), dueDate: d(2), createdAt: d(-14),
    checklist: [
      { id: 'c1', text: 'Rough cut v1', done: true },
      { id: 'c2', text: 'Sound mix', done: false },
      { id: 'c3', text: 'Color grade', done: false },
    ],
    comments: [], attachments: [{ id:'a1', name:'rough-cut-v1.mp4', size:'420 MB', type:'video' }], recurring: null },

  { id: 'MT2026-UI-0139', name: '[The Aspen Tree] Email template — May newsletter', project: 'aspen',
    description: '', workType: 'EDM', priority: 'Medium', status: 'In progress',
    assignee: 'u2', pic: 'u8', tags: ['edm'],
    startDate: d(-3), dueDate: d(4), createdAt: d(-5),
    checklist: [], comments: [], attachments: [], recurring: { freq: 'monthly', label: 'Every month' } },

  { id: 'MT2026-MK-0140', name: '[Online Marketing] Facebook ad creative refresh', project: 'mqdc',
    description: 'รีเฟรช creative ทั้ง 8 ตัว สำหรับ campaign Q2',
    workType: 'Online Marketing', priority: 'High', status: 'In progress',
    assignee: 'u8', pic: 'u3', tags: ['ads', 'social'],
    startDate: d(-4), dueDate: d(1), createdAt: d(-6),
    checklist: [
      { id: 'c1', text: 'Hook variants × 3', done: true },
      { id: 'c2', text: 'Asset adaptation 1:1, 9:16, 16:9', done: false },
    ],
    comments: [], attachments: [], recurring: null },

  // Hold / Pending
  { id: 'MT2026-AR-0130', name: '[Customer Experience Tech] VR walkthrough — model unit B', project: 'cet',
    description: 'รอ unit จริงเสร็จก่อนถึงจะ scan ได้ คาดว่าต้นเดือนหน้า',
    workType: 'AR/VR', priority: 'Medium', status: 'Hold / Pending',
    assignee: 'u6', pic: 'u3', tags: ['vr'],
    startDate: d(-2), dueDate: d(25), createdAt: d(-12),
    checklist: [], comments: [
      { id: 'cm1', author: 'u3', text: 'รอข่าวจาก construction team อยู่ คาดว่าจะได้ key 6 มิ.ย.', time: d(-3) },
    ], attachments: [], recurring: null },

  { id: 'MT2026-GD-0131', name: '[Mulberry Grove] Brochure print — Phase 2', project: 'mulb',
    description: '', workType: 'Graphic Design', priority: 'Low', status: 'Hold / Pending',
    assignee: 'u9', pic: 'u3', tags: ['print'],
    startDate: d(-5), dueDate: d(20), createdAt: d(-15),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-UX-0132', name: '[RISC] User testing recruitment for sustainability tool', project: 'risc',
    description: 'รอ participant approval', workType: 'UX Designer', priority: 'Medium', status: 'Hold / Pending',
    assignee: 'u10', pic: 'u3', tags: ['research'],
    startDate: d(-1), dueDate: d(14), createdAt: d(-8),
    checklist: [], comments: [], attachments: [], recurring: null },

  // Done
  { id: 'MT2026-UI-0120', name: '[Magnolias Ratchadamri] Web banner — Mother\'s Day', project: 'mag-r',
    description: 'ทำเสร็จและ deploy เมื่อสัปดาห์ก่อน', workType: 'Graphic Design', priority: 'High', status: 'Done',
    assignee: 'u9', pic: 'u3', tags: ['banner'],
    startDate: d(-15), dueDate: d(-8), createdAt: d(-20),
    checklist: [
      { id: 'c1', text: 'Concept × 2', done: true },
      { id: 'c2', text: 'Final delivery', done: true },
    ],
    comments: [], attachments: [{ id:'a1', name:'banner-final.png', size:'1.2 MB', type:'image' }], recurring: null },

  { id: 'MT2026-FE-0121', name: '[Nebula] Add cookie consent banner (GDPR)', project: 'nebula',
    description: '', workType: 'Front-End', priority: 'Medium', status: 'Done',
    assignee: 'u4', pic: 'u3', tags: ['dev', 'legal'],
    startDate: d(-12), dueDate: d(-5), createdAt: d(-16),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-VID-0122', name: '[The Forestias] Testimonial video — Resident A', project: 'forest',
    description: '', workType: 'VIDEO', priority: 'Medium', status: 'Done',
    assignee: 'u5', pic: 'u3', tags: ['video', 'testimonial'],
    startDate: d(-20), dueDate: d(-10), createdAt: d(-25),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-MK-0123', name: '[MQDC] Q1 marketing report deck', project: 'mqdc',
    description: '', workType: 'Online Marketing', priority: 'High', status: 'Done',
    assignee: 'u8', pic: 'u3', tags: ['report'],
    startDate: d(-18), dueDate: d(-7), createdAt: d(-22),
    checklist: [], comments: [], attachments: [], recurring: { freq:'quarterly', label:'Every quarter' } },

  { id: 'MT2026-UI-0124', name: '[Whizdom Connect] Login & signup flow design', project: 'whz-cn',
    description: '', workType: 'UI Designer', priority: 'High', status: 'Done',
    assignee: 'u1', pic: 'u3', tags: ['design'],
    startDate: d(-14), dueDate: d(-6), createdAt: d(-18),
    checklist: [], comments: [], attachments: [], recurring: null },

  // Cancelled
  { id: 'MT2026-EVT-0115', name: '[The Aspen Tree] Pre-launch open house event', project: 'aspen',
    description: 'เลื่อนออกไปก่อน เนื่องจาก scope ใหม่', workType: 'Event', priority: 'High', status: 'Cancel',
    assignee: 'u8', pic: 'u3', tags: ['event'],
    startDate: d(-30), dueDate: d(-15), createdAt: d(-35),
    checklist: [], comments: [], attachments: [], recurring: null },

  // Archived (old, completed work — kept for record)
  { id: 'MT2025-UI-0042', name: '[Magnolias Ratchadamri] Brand refresh 2025', project: 'mag-r',
    description: 'Done in Q4 2025 — kept for reference', workType: 'UI Designer', priority: 'Medium', status: 'Archived',
    assignee: 'u1', pic: 'u3', tags: ['brand'],
    startDate: d(-180), dueDate: d(-150), createdAt: d(-200),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2025-VID-0038', name: '[The Forestias] Annual showcase reel', project: 'forest',
    description: 'Final delivery archived after 2025 launch event', workType: 'VIDEO', priority: 'Highest', status: 'Archived',
    assignee: 'u5', pic: 'u3', tags: ['video', 'event'],
    startDate: d(-220), dueDate: d(-180), createdAt: d(-240),
    checklist: [], comments: [], attachments: [{ id:'a1', name:'showcase-reel-2025-final.mp4', size:'2.1 GB', type:'video' }], recurring: null },

  { id: 'MT2025-FE-0029', name: '[Nebula] Legacy WordPress to Webflow migration', project: 'nebula',
    description: '', workType: 'Front-End', priority: 'High', status: 'Archived',
    assignee: 'u4', pic: 'u3', tags: ['dev', 'migration'],
    startDate: d(-160), dueDate: d(-120), createdAt: d(-180),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2025-SEO-0021', name: '[MQDC] 2025 search ranking audit', project: 'mqdc',
    description: '', workType: 'SEO', priority: 'Low', status: 'Archived',
    assignee: 'u7', pic: 'u3', tags: ['seo', 'report'],
    startDate: d(-300), dueDate: d(-280), createdAt: d(-310),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2025-EVT-0017', name: '[Whizdom Connect] 2025 grand opening event', project: 'whz-cn',
    description: '', workType: 'Event', priority: 'Highest', status: 'Archived',
    assignee: 'u8', pic: 'u3', tags: ['event'],
    startDate: d(-280), dueDate: d(-260), createdAt: d(-300),
    checklist: [], comments: [], attachments: [], recurring: null },

  // Overdue (still in progress)
  { id: 'MT2026-FE-0118', name: '[CET] Integrate Matterport tour API on developer portal', project: 'cet',
    description: 'งานนี้ยืดมานานเพราะ API doc ของ Matterport เปลี่ยน', workType: 'Front-End', priority: 'Highest', status: 'In progress',
    assignee: 'u6', pic: 'u3', tags: ['dev', 'integration'],
    startDate: d(-20), dueDate: d(-2), createdAt: d(-25),
    checklist: [
      { id: 'c1', text: 'OAuth flow', done: true },
      { id: 'c2', text: 'Tour list endpoint', done: true },
      { id: 'c3', text: 'Embed component', done: false },
    ],
    comments: [
      { id: 'cm1', author: 'u3', text: 'อันนี้เลย due แล้วนะ ติดอะไร?', time: d(-2) },
      { id: 'cm2', author: 'u6', text: 'API doc เปลี่ยน รื้อ flow ใหม่ครับ ขอเลื่อนอีกอาทิตย์', time: d(-1) },
    ],
    attachments: [], recurring: null },

  // upcoming this month
  { id: 'MT2026-GD-0146', name: '[MQDC] Social media kit June', project: 'mqdc',
    description: '', workType: 'Graphic Design', priority: 'Medium', status: 'In progress',
    assignee: 'u9', pic: 'u8', tags: ['social', 'design'],
    startDate: d(0), dueDate: d(10), createdAt: d(-2),
    checklist: [], comments: [], attachments: [], recurring: { freq:'monthly', label:'Every month' } },

  { id: 'MT2026-UI-0147', name: '[Mulberry Grove] Sales gallery interactive screen UX', project: 'mulb',
    description: '', workType: 'UX Designer', priority: 'High', status: 'Backlog',
    assignee: 'u10', pic: 'u3', tags: ['ux'],
    startDate: d(4), dueDate: d(18), createdAt: d(-1),
    checklist: [], comments: [], attachments: [], recurring: null },

  { id: 'MT2026-WB-0148', name: '[MQDC] Monthly content update — properties page', project: 'mqdc',
    description: '', workType: 'Updated Content', priority: 'Low', status: 'Backlog',
    assignee: 'u8', pic: 'u3', tags: ['content'],
    startDate: d(15), dueDate: d(20), createdAt: d(-1),
    checklist: [], comments: [], attachments: [], recurring: { freq:'monthly', label:'Every month' } },

  { id: 'MT2026-AI-0149', name: '[RISC] AI chatbot prototype for sustainability FAQ', project: 'risc',
    description: '', workType: 'AI', priority: 'Medium', status: 'In progress',
    assignee: 'u4', pic: 'u10', tags: ['ai', 'experiment'],
    startDate: d(-3), dueDate: d(14), createdAt: d(-7),
    checklist: [], comments: [], attachments: [], recurring: null },
];

// ── ACTIVITY LOG ──
const ACTIVITY = [
  { id:'a1', user:'u3', verb:'assigned', target:'MT2026-UI-0142', meta:'to Phurichaya', time: d(-1) },
  { id:'a2', user:'u4', verb:'completed checklist', target:'MT2026-FE-0137', meta:'"API contract review"', time: d(-1) },
  { id:'a3', user:'u1', verb:'commented on', target:'MT2026-UI-0136', meta:'', time: d(-1) },
  { id:'a4', user:'u3', verb:'created', target:'MT2026-WB-0148', meta:'in MQDC', time: d(-1) },
  { id:'a5', user:'u9', verb:'moved', target:'MT2026-UI-0120', meta:'to Done', time: d(-2) },
  { id:'a6', user:'u6', verb:'requested extension on', target:'MT2026-FE-0118', meta:'', time: d(-1) },
];

// ── HELPERS ──
const getUser = (id) => USERS.find(u => u.id === id);
const getProject = (id) => PROJECTS.find(p => p.id === id);
const getStatus = (key) => STATUSES.find(s => s.key === key);

function initials(name) {
  if (!name) return '?';
  const parts = name.trim().split(/\s+/);
  if (parts.length >= 2) return (parts[0][0] + parts[1][0]).toUpperCase();
  return name.slice(0, 2).toUpperCase();
}

function formatDate(iso, opts = {}) {
  if (!iso) return '';
  const dt = new Date(iso);
  if (isNaN(dt)) return '';
  return dt.toLocaleDateString('en-US', { month: 'short', day: 'numeric', ...opts });
}

function daysFromToday(iso) {
  if (!iso) return null;
  const dt = new Date(iso); dt.setHours(0,0,0,0);
  const today = new Date(TODAY); today.setHours(0,0,0,0);
  return Math.round((dt - today) / 86400000);
}

function relativeDue(iso, status) {
  if (!iso) return null;
  const days = daysFromToday(iso);
  const overdue = days < 0 && !['Done','Archived','Cancel'].includes(status);
  if (overdue) return { text: `${Math.abs(days)}d overdue`, kind: 'overdue' };
  if (days === 0) return { text: 'Today', kind: 'today' };
  if (days === 1) return { text: 'Tomorrow', kind: 'soon' };
  if (days > 0 && days <= 7) return { text: `In ${days}d`, kind: 'soon' };
  return { text: formatDate(iso), kind: 'normal' };
}

function uid(prefix = 'id') {
  return prefix + '-' + Math.random().toString(36).slice(2, 9);
}

Object.assign(window, {
  USERS, PROJECTS, WORK_TYPES, STATUSES, PRIORITIES, TASKS, ACTIVITY,
  TODAY, PALETTE,
  getUser, getProject, getStatus,
  initials, formatDate, daysFromToday, relativeDue, uid,
});
