// ============================================================
// data.jsx — พนักงาน 2 สาขา + 2 กะ (rotation engine)
// ============================================================

const WEEKDAYS_TH = ['อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์'];
const WEEKDAYS_SHORT = ['อา','จ','อ','พ','พฤ','ศ','ส'];

const DOW_COLORS = [
  '#f4768a', // อาทิตย์
  '#f5c451', // จันทร์
  '#f59ab8', // อังคาร
  '#9fd368', // พุธ
  '#f0a24a', // พฤหัสบดี
  '#7cc0e8', // ศุกร์
  '#b89ae0', // เสาร์
];

const MONTHS_TH = ['มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
  'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'];

// ---- สาขา (ชื่อปรับได้ผ่าน Tweaks) ----
const BRANCHES = {
  b1: { id:'b1', name:'ฟาร์ม่าวัน', short:'สาขา 1', num:'1', solid:'#1f9e8f', tint:'#e6f5f2', text:'#0e6b60' },
  b2: { id:'b2', name:'น้ำริดเภสัช',   short:'สาขา 2', num:'2', solid:'#e8745a', tint:'#fcece6', text:'#a8472f' },
  off:{ id:'off',name:'หยุด',       short:'หยุด',   num:'',  solid:'#9aa3b2', tint:'#eef0f4', text:'#69707e' },
};

// ---- กะการทำงาน ----
const SHIFTS = {
  m: { id:'m', label:'เช้า',      time:'08:30–18:30', char:'ช', solid:'#e0a52a', tint:'#fdf3da', text:'#9a6a12' },
  a: { id:'a', label:'บ่าย',      time:'11:00–21:00', char:'บ', solid:'#6664c4', tint:'#ececfb', text:'#46449e' },
  c: { id:'c', label:'กำหนดเอง', time:'–',            char:'ก', solid:'#6b9e52', tint:'#ecf5e8', text:'#3a6128' },
};

// ---- ประเภทพนักงาน ----
const TYPES = {
  full:  { id:'full',  label:'พนักงานประจำ', short:'ประจำ' },
  part:  { id:'part',  label:'พาร์ทไทม์',    short:'พาร์ทไทม์' },
  owner: { id:'owner', label:'เจ้าของ',       short:'เจ้าของ' },
};

// ---- พนักงาน (offDays = วันหยุดประจำสัปดาห์) ----
const STAFF = [
  { id:'pron',  nick:'พร',    full:'พร',    type:'full', offDays:[5], hue:262 },
  { id:'baem',  nick:'แบม',   full:'แบม',   type:'full', offDays:[0], hue:196 },
  { id:'cream', nick:'ครีม',  full:'ครีม',  type:'full', offDays:[2], hue:338 },
  { id:'film',  nick:'ฟิล์ม', full:'ฟิล์ม', type:'part', offDays:[6], hue:28  },
];

// ---- เจ้าของ (ไม่อยู่ในตารางเวร) ----
const OWNERS = [
  { id:'yok',  nick:'หยก',  full:'หยก',  type:'owner', offDays:[], hue:90  },
  { id:'form', nick:'ฟอร์ม', full:'ฟอร์ม', type:'owner', offDays:[], hue:220 },
];

const STAFF_BY_ID = Object.fromEntries([...STAFF, ...OWNERS].map(s => [s.id, s]));
const CURRENT_USER_ID = 'pron';

function staffColor(staff, kind='solid'){
  if(kind==='tint')  return `oklch(0.95 0.035 ${staff.hue})`;
  if(kind==='text')  return `oklch(0.42 0.11 ${staff.hue})`;
  return `oklch(0.62 0.13 ${staff.hue})`;
}

function weekIndex(date){
  const ms = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
  return Math.floor(ms/86400000 / 7);
}

// 4 ช่องงาน = สาขา × กะ ; หมุนเวียนรายสัปดาห์
// 3 ช่องงาน: น้ำริดเช้า · ฟมวเช้า · ฟมวบ่าย (คนที่ 4 หยุดตาม offDays)
const SLOTS = [
  { b:'b2', s:'m' }, // น้ำริดเภสัช · เช้า (อยู่คนเดียว)
  { b:'b1', s:'m' }, // ฟาร์ม่าวัน · เช้า
  { b:'b1', s:'a' }, // ฟาร์ม่าวัน · บ่าย
];

// คืนค่า: { staffId: 'off' | {b,s} }
function assignmentsForDate(date, overrides={}, leaves={}){
  const w = weekIndex(date);
  const map = {};
  STAFF.forEach((s, i) => {
    const lk = dayKey ? dayKey(date,s.id) : `${date.getFullYear()}-${String(date.getMonth()+1).padStart(2,'0')}-${String(date.getDate()).padStart(2,'0')}_${s.id}`;
    if(leaves[lk] && leaves[lk].status==='approved'){ map[s.id]='off'; return; }
    if(overrides[lk] !== undefined){ map[s.id]=overrides[lk]; return; }
    if(s.offDays.includes(date.getDay())){ map[s.id] = 'off'; return; }
    const slot = SLOTS[(i + w) % SLOTS.length];
    map[s.id] = { b:slot.b, s:slot.s };
  });
  return map;
}

// จัดกลุ่มตามสาขา (พร้อมกะ) ในหนึ่งวัน
function dayGroups(date, overrides={}, leaves={}){
  const map = assignmentsForDate(date, overrides, leaves);
  const out = { b1:[], b2:[], off:[] };
  STAFF.forEach(s => {
    const a = map[s.id];
    if(a === 'off') out.off.push(s);
    else out[a.b].push({ staff:s, shift:a.s, customTime:a.customTime });
  });
  out.b1.sort((x,y)=> x.shift==='m' ? -1 : 1);
  out.b2.sort((x,y)=> x.shift==='m' ? -1 : 1);
  return out;
}

function monthGrid(year, month){
  const first = new Date(year, month, 1);
  const startDow = first.getDay();
  const daysInMonth = new Date(year, month+1, 0).getDate();
  const cells = [];
  for(let i=0;i<startDow;i++){
    cells.push({ date:new Date(year, month, 1 - (startDow - i)), inMonth:false });
  }
  for(let d=1; d<=daysInMonth; d++){
    cells.push({ date:new Date(year, month, d), inMonth:true });
  }
  while(cells.length % 7 !== 0){
    const last = cells[cells.length-1].date;
    cells.push({ date:new Date(last.getFullYear(), last.getMonth(), last.getDate()+1), inMonth:false });
  }
  return cells;
}

function daysOfMonth(year, month){
  const n = new Date(year, month+1, 0).getDate();
  return Array.from({length:n}, (_,i)=> new Date(year, month, i+1));
}

function isSameDay(a, b){
  return a.getFullYear()===b.getFullYear() && a.getMonth()===b.getMonth() && a.getDate()===b.getDate();
}

const TODAY = new Date();
function thaiYear(y){ return y + 543; }

Object.assign(window, {
  WEEKDAYS_TH, WEEKDAYS_SHORT, DOW_COLORS, MONTHS_TH,
  BRANCHES, SHIFTS, TYPES, STAFF, OWNERS, STAFF_BY_ID, CURRENT_USER_ID,
  staffColor, assignmentsForDate, dayGroups, monthGrid, daysOfMonth,
  isSameDay, TODAY, thaiYear, weekIndex,
});
