// data.jsx — categories, mock reports, translations

const CATEGORIES = [
  { id: 'classroom',   th: 'ห้องเรียน/อาคาร',         en: 'Classroom/Building', tint: '#5B6BE8', code: 'CR' },
  { id: 'restroom',    th: 'ห้องน้ำ',                  en: 'Restroom',           tint: '#7B5BE8', code: 'RR' },
  { id: 'equipment',   th: 'อุปกรณ์/เครื่องมือ',      en: 'Equipment',          tint: '#4F86E8', code: 'EQ' },
  { id: 'cleanliness', th: 'ความสะอาด',                en: 'Cleanliness',        tint: '#5BB6E8', code: 'CL' },
  { id: 'electricity', th: 'ไฟฟ้า/แสงสว่าง',          en: 'Electricity',        tint: '#9B5BE8', code: 'EL' },
  { id: 'water',       th: 'น้ำประปา',                 en: 'Water',              tint: '#4FB6E8', code: 'WT' },
  { id: 'safety',      th: 'ความปลอดภัย',              en: 'Safety',             tint: '#E85BA8', code: 'SF' },
  { id: 'common',      th: 'สนาม/พื้นที่ส่วนกลาง',   en: 'Field/Common',       tint: '#6B7BE8', code: 'CM' },
];

const CAT_BY_ID = Object.fromEntries(CATEGORIES.map(c => [c.id, c]));

const URGENCY = {
  normal: { th: 'ปกติ', en: 'Normal' },
  urgent: { th: 'เร่งด่วน', en: 'Urgent' },
};

const STATUSES = {
  pending:    { th: 'รอรับเรื่อง',  en: 'Pending'     },
  'in-progress': { th: 'กำลังแก้ไข', en: 'In progress' },
  resolved:   { th: 'แก้ไขแล้ว',   en: 'Resolved'    },
};

// Mock reports — chosen to make the dashboard feel populated
const INITIAL_REPORTS = [
  {
    id: 'BP-2026-0148', category: 'cleanliness',
    title: 'ห้องน้ำหญิงตึก 3 สกปรก ไม่ได้ทำความสะอาด',
    titleEn: "Girls' restroom on Building 3 not cleaned",
    desc: 'ห้องน้ำหญิงชั้น 3 ตึก 3 มีขยะกองอยู่ที่พื้นและมีกลิ่นไม่พึงประสงค์ ครับ/ค่ะ',
    descEn: 'Trash on the floor in the 3rd-floor girls\' restroom, bad smell.',
    location: 'อาคาร 3 ชั้น 3', locationEn: 'Building 3, Floor 3',
    reporter: 'ปุณยวีร์ ส.', grade: 'ม.5/3',
    submittedAt: minutesAgo(28), urgency: 'normal', status: 'pending', progress: 0,
    before: { label: 'TRASH ON FLOOR · LIGHT 3F', hue: 200 },
    after: null, timeline: [['submitted', minutesAgo(28), 'นักเรียน']],
  },
  {
    id: 'BP-2026-0147', category: 'equipment',
    title: 'โต๊ะหักในโรงอาหาร 2 ตัว',
    titleEn: 'Two broken tables in cafeteria',
    desc: 'โต๊ะนั่งทานข้าวฝั่งซ้ายของโรงอาหารหักลง 2 ตัว เสี่ยงเด็กล้มทับ',
    descEn: 'Two cafeteria tables collapsed, may cause injury.',
    location: 'โรงอาหาร ชั้น 1', locationEn: 'Cafeteria, Floor 1',
    reporter: 'ภานุวัฒน์ ก.', grade: 'ม.6/1',
    submittedAt: hoursAgo(2), urgency: 'urgent', status: 'pending', progress: 0,
    before: { label: 'BROKEN TABLES · CAFETERIA', hue: 250 },
    after: null, timeline: [['submitted', hoursAgo(2), 'นักเรียน']],
  },
  {
    id: 'BP-2026-0146', category: 'electricity',
    title: 'หลอดไฟห้อง 401 ดับไป 3 หลอด',
    titleEn: 'Three lights out in Room 401',
    desc: 'หลอดไฟกลางห้องเรียน 401 ดับไป 3 หลอด มืดเวลาเรียนช่วงบ่าย',
    descEn: 'Three ceiling lights in Room 401 are out — too dark to study in the afternoon.',
    location: 'อาคาร 4 ชั้น 4 ห้อง 401', locationEn: 'Building 4, Floor 4, Room 401',
    reporter: 'ธนกร ใ.', grade: 'ม.5/2',
    submittedAt: hoursAgo(6), urgency: 'normal', status: 'in-progress', progress: 35,
    before: { label: 'CEILING LIGHTS OUT · 401', hue: 290 },
    after: null,
    timeline: [
      ['submitted', hoursAgo(6), 'ธนกร'],
      ['in-progress', hoursAgo(2), 'ครูสมศักดิ์', 'รับเรื่องครับ จะเปลี่ยนหลอดให้ภายในวันนี้'],
    ],
  },
  {
    id: 'BP-2026-0145', category: 'safety',
    title: 'พื้นกระเบื้องสนามบาสแตก เสี่ยงสะดุดล้ม',
    titleEn: 'Cracked tiles on basketball court',
    desc: 'พื้นข้างห่วงด้านทิศใต้แตกเป็นแนวยาวประมาณ 1 เมตร',
    descEn: 'Long crack along south hoop tiles.',
    location: 'สนามบาสกลางแจ้ง', locationEn: 'Outdoor basketball court',
    reporter: 'นันทพงศ์ ป.', grade: 'ม.4/4',
    submittedAt: hoursAgo(9), urgency: 'urgent', status: 'in-progress', progress: 25,
    before: { label: 'CRACKED COURT TILES', hue: 320 },
    after: null,
    timeline: [
      ['submitted', hoursAgo(9), 'นันทพงศ์'],
      ['in-progress', hoursAgo(3), 'ครูประภาส', 'ปิดพื้นที่ชั่วคราว รออัปเดต'],
    ],
  },
  {
    id: 'BP-2026-0144', category: 'water',
    title: 'ก๊อกน้ำห้องน้ำชายตึก 2 รั่ว',
    titleEn: "Leaking faucet in boys' restroom",
    desc: 'ก๊อกน้ำในห้องน้ำชายชั้น 2 ตึก 2 รั่วตลอดเวลา น้ำไหลตลอด',
    descEn: 'Faucet running continuously in 2F boys\' restroom.',
    location: 'อาคาร 2 ชั้น 2', locationEn: 'Building 2, Floor 2',
    reporter: 'จิรายุ ม.', grade: 'ม.3/5',
    submittedAt: daysAgo(2), urgency: 'normal', status: 'resolved', progress: 100,
    before: { label: 'LEAKING FAUCET · BLDG 2', hue: 215 },
    after:  { label: 'AFTER · FAUCET REPLACED',  hue: 155 },
    timeline: [
      ['submitted', daysAgo(2), 'จิรายุ'],
      ['in-progress', daysAgo(2, 4), 'ครูประภาส', 'ติดต่อช่างประปาแล้ว'],
      ['resolved', daysAgo(1), 'ช่างประปา / ครูประภาส', 'เปลี่ยนก๊อกน้ำใหม่ทั้ง 4 จุด'],
    ],
    thanks: 1,
  },
  {
    id: 'BP-2026-0143', category: 'equipment',
    title: 'แอร์ห้องสมุดไม่เย็น',
    titleEn: 'Library AC not cooling',
    desc: 'แอร์ในห้องสมุดเย็นไม่พอ อุณหภูมิประมาณ 30°C ทำให้นั่งอ่านหนังสือลำบาก',
    descEn: 'Library AC at ~30°C, hard to study.',
    location: 'ห้องสมุด อาคาร 5', locationEn: 'Library, Building 5',
    reporter: 'พิมพ์ลภัส อ.', grade: 'ม.6/3',
    submittedAt: daysAgo(3), urgency: 'normal', status: 'in-progress', progress: 60,
    before: { label: 'LIBRARY AC · WARM', hue: 250 },
    after: null,
    timeline: [
      ['submitted', daysAgo(3), 'พิมพ์ลภัส'],
      ['in-progress', daysAgo(2), 'ครูพัชรินทร์', 'รอช่างแอร์เข้ามาตรวจ'],
    ],
  },
  {
    id: 'BP-2026-0142', category: 'classroom',
    title: 'ผ้าม่านห้อง 302 ขาด',
    titleEn: 'Torn curtain in Room 302',
    desc: 'ผ้าม่านฝั่งติดหน้าต่างของห้อง 302 ขาดเป็นรู ทำให้แดดส่องตอนเช้า',
    descEn: 'Curtain in Room 302 has a hole — morning glare.',
    location: 'อาคาร 3 ชั้น 2 ห้อง 302', locationEn: 'Building 3, Floor 2, Room 302',
    reporter: 'ชนิกานต์ พ.', grade: 'ม.4/2',
    submittedAt: daysAgo(8), urgency: 'normal', status: 'resolved', progress: 100,
    before: { label: 'TORN CURTAIN · 302', hue: 245 },
    after:  { label: 'AFTER · NEW CURTAIN',  hue: 155 },
    timeline: [
      ['submitted', daysAgo(8), 'ชนิกานต์'],
      ['in-progress', daysAgo(7), 'ครูสมศักดิ์'],
      ['resolved', daysAgo(5), 'ครูสมศักดิ์', 'เปลี่ยนผ้าม่านใหม่เรียบร้อย'],
    ],
    thanks: 2,
  },
  {
    id: 'BP-2026-0141', category: 'electricity',
    title: 'ไฟทางเดินหน้าตึก 1 ดับ',
    titleEn: 'Walkway light out in front of Building 1',
    desc: 'ไฟทางเดินหน้าตึก 1 ดับ 3 จุดติด ตอนเย็นเดินมืดมาก',
    descEn: 'Three walkway lights in front of Building 1 are out, dark in the evening.',
    location: 'หน้าตึก 1', locationEn: 'In front of Building 1',
    reporter: 'ศุภวิชญ์ ห.', grade: 'ม.5/1',
    submittedAt: daysAgo(4), urgency: 'urgent', status: 'in-progress', progress: 70,
    before: { label: 'WALKWAY LIGHTS OUT', hue: 295 },
    after: null,
    timeline: [
      ['submitted', daysAgo(4), 'ศุภวิชญ์'],
      ['in-progress', daysAgo(3), 'ครูประภาส', 'สั่งหลอด LED มาแล้ว รอติดตั้ง'],
    ],
  },
  {
    id: 'BP-2026-0140', category: 'safety',
    title: 'กระจกหน้าต่างโรงยิมแตก',
    titleEn: 'Broken gym window',
    desc: 'กระจกบานเล็กฝั่งทิศตะวันออกของโรงยิมแตกร้าว',
    descEn: 'Small east-side gym window cracked.',
    location: 'โรงยิม', locationEn: 'Gymnasium',
    reporter: 'ณัฐภัทร ส.', grade: 'ม.6/2',
    submittedAt: daysAgo(14), urgency: 'normal', status: 'resolved', progress: 100,
    before: { label: 'CRACKED GYM WINDOW', hue: 320 },
    after:  { label: 'AFTER · GLASS REPLACED', hue: 155 },
    timeline: [
      ['submitted', daysAgo(14), 'ณัฐภัทร'],
      ['in-progress', daysAgo(13), 'ครูประภาส'],
      ['resolved', daysAgo(10), 'ช่าง / ครูประภาส', 'เปลี่ยนกระจกใหม่และตรวจสอบกรอบ'],
    ],
    thanks: 3,
  },
  {
    id: 'BP-2026-0139', category: 'common',
    title: 'น้ำขังทางเดินตึก 5',
    titleEn: 'Water pooling on Building 5 walkway',
    desc: 'หลังฝนตกน้ำขังบริเวณทางเดินเชื่อมตึก 5 ไม่ระบาย เดินลื่น',
    descEn: 'Water pools after rain on Building 5 walkway, slippery.',
    location: 'ทางเดินตึก 5', locationEn: 'Building 5 walkway',
    reporter: 'อชิรญาณ์ ส.', grade: 'ม.4/1',
    submittedAt: daysAgo(20), urgency: 'normal', status: 'resolved', progress: 100,
    before: { label: 'WATER POOLING · BLDG 5', hue: 260 },
    after:  { label: 'AFTER · DRAIN CLEARED',  hue: 155 },
    timeline: [
      ['submitted', daysAgo(20), 'อชิรญาณ์'],
      ['in-progress', daysAgo(19), 'ครูประภาส'],
      ['resolved', daysAgo(17), 'ภารโรง', 'ลอกท่อระบายน้ำเรียบร้อย'],
    ],
    thanks: 2,
  },
];

// helpers — return Date in past
function minutesAgo(n) {
  const d = new Date(); d.setMinutes(d.getMinutes() - n); return d.toISOString();
}
function hoursAgo(h, m = 0) {
  const d = new Date(); d.setHours(d.getHours() - h); d.setMinutes(d.getMinutes() - m); return d.toISOString();
}
function daysAgo(n, hOffset = 0) {
  const d = new Date(); d.setDate(d.getDate() - n); d.setHours(d.getHours() - hOffset); return d.toISOString();
}

// Relative time formatter, TH/EN
function relTime(iso, lang = 'th') {
  const now = new Date();
  const then = new Date(iso);
  const diff = Math.max(0, (now - then) / 1000);
  if (lang === 'th') {
    if (diff < 60) return 'เมื่อสักครู่';
    if (diff < 3600) return `${Math.floor(diff/60)} นาทีที่แล้ว`;
    if (diff < 86400) return `${Math.floor(diff/3600)} ชั่วโมงที่แล้ว`;
    if (diff < 7*86400) return `${Math.floor(diff/86400)} วันที่แล้ว`;
    return then.toLocaleDateString('th-TH', { day:'numeric', month:'short' });
  } else {
    if (diff < 60) return 'just now';
    if (diff < 3600) return `${Math.floor(diff/60)} min ago`;
    if (diff < 86400) return `${Math.floor(diff/3600)} h ago`;
    if (diff < 7*86400) return `${Math.floor(diff/86400)} d ago`;
    return then.toLocaleDateString('en-US', { day:'numeric', month:'short' });
  }
}

function fullDateTime(iso, lang = 'th') {
  const d = new Date(iso);
  if (lang === 'th') {
    const date = d.toLocaleDateString('th-TH', { day: 'numeric', month: 'long', year: 'numeric' });
    const time = d.toLocaleTimeString('th-TH', { hour: '2-digit', minute: '2-digit' });
    return `${date} · ${time} น.`;
  }
  return d.toLocaleDateString('en-US', { day: 'numeric', month: 'short', year: 'numeric' }) +
    ' · ' + d.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: false });
}

// i18n
const T = {
  th: {
    appName: 'Bodin plus',
    schoolName: 'โรงเรียนบดินทรเดชา (สิงห์ สิงหเสนี)',
    chooseRole: 'เลือกบทบาทของคุณ',
    chooseRoleSub: 'ระบบจัดการความเรียบร้อยภายในโรงเรียน',
    student: 'นักเรียน',
    studentSub: 'แจ้งจุดที่ต้องปรับปรุง',
    teacher: 'ครู',
    teacherSub: 'จัดการเรื่องที่ได้รับแจ้ง',
    enter: 'เข้าใช้งาน',
    // student
    greeting: 'สวัสดี',
    greetingMorning: 'อรุณสวัสดิ์',
    greetingEvening: 'สวัสดีตอนเย็น',
    reportIssue: 'แจ้งปัญหาที่พบ',
    reportIssueSub: 'ถ่ายรูป อธิบายสั้นๆ แล้วส่ง',
    myReports: 'รายการของฉัน',
    seeAll: 'ดูทั้งหมด',
    recent: 'เรื่องล่าสุด',
    home: 'หน้าหลัก',
    profile: 'โปรไฟล์',
    history: 'ประวัติ',
    notifications: 'การแจ้งเตือน',
    // camera
    takePhoto: 'ถ่ายภาพ',
    retake: 'ถ่ายใหม่',
    usePhoto: 'ใช้ภาพนี้',
    pickFromGallery: 'เลือกจากคลัง',
    // form
    addDetails: 'เพิ่มรายละเอียด',
    category: 'หมวดหมู่',
    description: 'อธิบายปัญหา',
    descPlaceholder: 'เกิดอะไรขึ้น อยู่ตรงไหน อย่างไร...',
    location: 'ตำแหน่ง',
    locPlaceholder: 'เช่น อาคาร 4 ชั้น 4 ห้อง 401',
    urgency: 'ระดับความเร่งด่วน',
    normal: 'ปกติ',
    urgent: 'เร่งด่วน',
    submit: 'ส่งรายงาน',
    submitting: 'กำลังส่ง...',
    back: 'ย้อนกลับ',
    cancel: 'ยกเลิก',
    // success
    submitted: 'ส่งเรียบร้อยแล้ว',
    submittedSub: 'ขอบคุณที่ช่วยทำให้โรงเรียนน่าอยู่ขึ้น',
    pointsEarned: 'คุณได้รับ',
    points: 'คะแนนพลเมืองดี',
    viewMine: 'ดูรายการของฉัน',
    backToHome: 'กลับหน้าหลัก',
    // statuses
    pending: 'รอรับเรื่อง',
    inProgress: 'กำลังแก้ไข',
    resolved: 'แก้ไขแล้ว',
    // teacher
    dashboard: 'แดชบอร์ด',
    issues: 'รายการแจ้ง',
    timeline: 'ไทม์ไลน์',
    settings: 'ตั้งค่า',
    resolvedPct: 'แก้ไขแล้ว',
    thisMonth: 'เดือนนี้',
    thisWeek: 'สัปดาห์นี้',
    today: 'วันนี้',
    totalReports: 'เรื่องทั้งหมด',
    open: 'รอแก้',
    avgTime: 'เฉลี่ยปิดเรื่อง',
    days: 'วัน',
    byCategory: 'แยกตามหมวดหมู่',
    filter: 'ตัวกรอง',
    allCategories: 'ทุกหมวดหมู่',
    allStatuses: 'ทุกสถานะ',
    search: 'ค้นหา...',
    reporter: 'ผู้แจ้ง',
    submittedOn: 'แจ้งเมื่อ',
    markInProgress: 'รับเรื่อง',
    uploadAfter: 'อัปโหลดภาพหลังแก้ไข',
    markResolved: 'ปิดเรื่อง',
    addNote: 'บันทึก',
    note: 'หมายเหตุ',
    notePlaceholder: 'เพิ่มหมายเหตุการแก้ไข...',
    before: 'ก่อนแก้ไข',
    after: 'หลังแก้ไข',
    timelineEvents: 'ไทม์ไลน์การดำเนินการ',
    thank: 'ขอบคุณนักเรียน',
    thanked: 'ขอบคุณแล้ว',
    sendThanks: 'ส่งคำขอบคุณ',
    notifSent: 'ส่ง notification ถึงนักเรียนแล้ว',
    days7: '7 วัน',
    days30: '30 วัน',
    days90: '90 วัน',
    activeIssues: 'เรื่องที่ดำเนินการ',
    fixed: 'แก้แล้ว',
    pendingCount: 'รอแก้',
    inProgressCount: 'กำลังแก้',
    backToList: 'กลับไปยังรายการ',
    switchRole: 'สลับมุมมอง',
    photoLabel: 'ภาพประกอบ',
    confirmResolve: 'ยืนยันการปิดเรื่อง',
    progress: 'ความคืบหน้า',
    updateProgress: 'อัปเดตความคืบหน้า',
    progressHint: 'เลื่อนเพื่อระบุเปอร์เซ็นการแก้ไข · ระบบบันทึกเวลาให้อัตโนมัติ',
    progressSaved: 'อัปเดต {n}% เรียบร้อย',
    completedFully: 'แก้ไขเสร็จ 100% — พร้อมปิดเรื่อง',
    autoLogged: 'บันทึกอัตโนมัติ',
  },
  en: {
    appName: 'Bodin plus',
    schoolName: 'Bodindecha (Sing Singhaseni) School',
    chooseRole: 'Choose your role',
    chooseRoleSub: 'In-school maintenance workflow system',
    student: 'Student',
    studentSub: 'Report things to improve',
    teacher: 'Teacher',
    teacherSub: 'Manage incoming reports',
    enter: 'Continue',
    greeting: 'Hi',
    greetingMorning: 'Good morning',
    greetingEvening: 'Good evening',
    reportIssue: 'Report an issue',
    reportIssueSub: 'Snap a photo, describe it, send.',
    myReports: 'My reports',
    seeAll: 'See all',
    recent: 'Recent',
    home: 'Home',
    profile: 'Profile',
    history: 'History',
    notifications: 'Notifications',
    takePhoto: 'Take photo',
    retake: 'Retake',
    usePhoto: 'Use photo',
    pickFromGallery: 'Choose from library',
    addDetails: 'Add details',
    category: 'Category',
    description: 'Describe the issue',
    descPlaceholder: 'What happened, where, how…',
    location: 'Location',
    locPlaceholder: 'e.g. Building 4, Floor 4, Room 401',
    urgency: 'Urgency',
    normal: 'Normal',
    urgent: 'Urgent',
    submit: 'Submit report',
    submitting: 'Sending…',
    back: 'Back',
    cancel: 'Cancel',
    submitted: 'Report submitted',
    submittedSub: 'Thanks for helping improve our school.',
    pointsEarned: 'You earned',
    points: 'good-citizen points',
    viewMine: 'View my reports',
    backToHome: 'Back to home',
    pending: 'Pending',
    inProgress: 'In progress',
    resolved: 'Resolved',
    dashboard: 'Dashboard',
    issues: 'Reports',
    timeline: 'Timeline',
    settings: 'Settings',
    resolvedPct: 'Resolved',
    thisMonth: 'This month',
    thisWeek: 'This week',
    today: 'Today',
    totalReports: 'Total reports',
    open: 'Open',
    avgTime: 'Avg. resolution',
    days: 'days',
    byCategory: 'By category',
    filter: 'Filter',
    allCategories: 'All categories',
    allStatuses: 'All statuses',
    search: 'Search…',
    reporter: 'Reporter',
    submittedOn: 'Submitted',
    markInProgress: 'Accept',
    uploadAfter: 'Upload after photo',
    markResolved: 'Mark resolved',
    addNote: 'Save note',
    note: 'Note',
    notePlaceholder: 'Add a note about the fix…',
    before: 'Before',
    after: 'After',
    timelineEvents: 'Activity timeline',
    thank: 'Thank student',
    thanked: 'Thanked',
    sendThanks: 'Send thanks',
    notifSent: 'Notification sent to student',
    days7: '7 days',
    days30: '30 days',
    days90: '90 days',
    activeIssues: 'Active reports',
    fixed: 'Fixed',
    pendingCount: 'Pending',
    inProgressCount: 'In progress',
    backToList: 'Back to list',
    switchRole: 'Switch role',
    photoLabel: 'Photo',
    confirmResolve: 'Confirm resolve',
    progress: 'Progress',
    updateProgress: 'Update progress',
    progressHint: 'Drag to set the fix progress · timestamp is logged automatically',
    progressSaved: 'Progress updated to {n}%',
    completedFully: 'Fully complete — ready to close',
    autoLogged: 'Auto-logged',
  },
};

Object.assign(window, {
  CATEGORIES, CAT_BY_ID, URGENCY, STATUSES,
  INITIAL_REPORTS, relTime, fullDateTime, T,
});
