// udata.jsx — data + model for the User Management & Permissions module.

const DEPARTMENTS = [
  { id:'bgd', vi:'Ban Giám đốc', en:'Executive' },
  { id:'kd', vi:'Kinh doanh', en:'Sales' },
  { id:'kt', vi:'Kỹ thuật', en:'Engineering' },
  { id:'vh', vi:'Vận hành', en:'Operations' },
  { id:'tckt', vi:'Tài chính - Kế toán', en:'Finance & Accounting' },
  { id:'ns', vi:'Nhân sự', en:'Human Resources' },
  { id:'mkt', vi:'Marketing', en:'Marketing' },
];

// resources (modules) that permissions apply to
const PERM_RESOURCES = [
  { id:'dashboard', vi:'Bảng điều khiển', en:'Dashboard' },
  { id:'mail', vi:'Email', en:'Email' },
  { id:'customers', vi:'Khách hàng', en:'Customers' },
  { id:'contacts', vi:'Liên hệ', en:'Contacts' },
  { id:'contracts', vi:'Hợp đồng', en:'Contracts' },
  { id:'finance', vi:'Tài chính', en:'Finance' },
  { id:'users', vi:'Người dùng', en:'Users' },
  { id:'admin', vi:'Quản trị hệ thống', en:'System admin' },
];
const PERM_ACTIONS = [
  { id:'view', vi:'Xem', en:'View' },
  { id:'create', vi:'Tạo', en:'Create' },
  { id:'edit', vi:'Sửa', en:'Edit' },
  { id:'delete', vi:'Xóa', en:'Delete' },
  { id:'manage', vi:'Quản trị', en:'Manage' },
];

// build a perms object from a compact spec: { resId: 'vced m letters' }
function mkPerms(spec){
  const m = {};
  PERM_RESOURCES.forEach(r => {
    const s = spec[r.id] || '';
    m[r.id] = { view:s.includes('v'), create:s.includes('c'), edit:s.includes('e'), delete:s.includes('d'), manage:s.includes('m') };
  });
  return m;
}
function allPerms(){ const s = {}; PERM_RESOURCES.forEach(r => s[r.id] = 'vcedm'); return mkPerms(s); }

const ROLES = [
  { id:'admin', name:{ vi:'Quản trị viên', en:'Administrator' }, color:'#1466E0', system:true,
    desc:{ vi:'Toàn quyền trên toàn hệ thống', en:'Full access to everything' }, perms:allPerms() },
  { id:'manager', name:{ vi:'Quản lý', en:'Manager' }, color:'#0FB0AB', system:true,
    desc:{ vi:'Quản lý nghiệp vụ, phê duyệt', en:'Manage operations & approvals' },
    perms:mkPerms({ dashboard:'v', mail:'vce', customers:'vcem', contacts:'vcem', contracts:'vcem', finance:'vce', users:'v', admin:'v' }) },
  { id:'accountant', name:{ vi:'Kế toán', en:'Accountant' }, color:'#16A34A', system:false,
    desc:{ vi:'Quản lý thu chi, dòng tiền, hợp đồng', en:'Manage finance & contracts' },
    perms:mkPerms({ dashboard:'v', mail:'vce', customers:'v', contacts:'v', contracts:'vce', finance:'vcedm', users:'', admin:'' }) },
  { id:'sales', name:{ vi:'Nhân viên Kinh doanh', en:'Sales rep' }, color:'#C77700', system:false,
    desc:{ vi:'Quản lý khách hàng, liên hệ, hợp đồng', en:'Manage customers, contacts, contracts' },
    perms:mkPerms({ dashboard:'v', mail:'vce', customers:'vce', contacts:'vce', contracts:'vce', finance:'v', users:'', admin:'' }) },
  { id:'staff', name:{ vi:'Nhân viên', en:'Staff' }, color:'#667085', system:true,
    desc:{ vi:'Quyền cơ bản, chủ yếu xem', en:'Basic, mostly read-only' },
    perms:mkPerms({ dashboard:'v', mail:'vce', customers:'v', contacts:'v', contracts:'v', finance:'', users:'', admin:'' }) },
];

function rolePermCount(role){
  let n = 0; PERM_RESOURCES.forEach(r => PERM_ACTIONS.forEach(a => { if (role.perms[r.id] && role.perms[r.id][a.id]) n++; }));
  return n;
}

const USERS_LIST = [
  { id:'u3', name:'Phạm Quốc Bảo', email:'bao.pham@swo.vn', dept:'bgd', role:'admin', status:'active', last:'5 phút trước', joined:'2021-03-01' },
  { id:'u2', name:'Lê Thu Hà', email:'ha.le@swo.vn', dept:'vh', role:'manager', status:'active', last:'1 giờ trước', joined:'2021-06-15' },
  { id:'u1', name:'Trần Minh Quân', email:'quan.tran@swo.vn', dept:'kd', role:'sales', status:'active', last:'Vừa xong', joined:'2022-01-10' },
  { id:'u4', name:'Nguyễn Thị Lan', email:'lan.nguyen@swo.vn', dept:'tckt', role:'accountant', status:'active', last:'2 giờ trước', joined:'2022-04-20' },
  { id:'u5', name:'Đặng Hữu Phúc', email:'phuc.dang@swo.vn', dept:'kt', role:'staff', status:'active', last:'Hôm qua', joined:'2022-09-05' },
  { id:'u6', name:'Võ Thanh Tùng', email:'tung.vo@swo.vn', dept:'kd', role:'sales', status:'active', last:'3 giờ trước', joined:'2023-02-14' },
  { id:'u7', name:'Bùi Khánh Vy', email:'vy.bui@swo.vn', dept:'mkt', role:'staff', status:'active', last:'Hôm qua', joined:'2023-05-22' },
  { id:'u8', name:'Hoàng Minh Đức', email:'duc.hoang@swo.vn', dept:'kt', role:'manager', status:'active', last:'30 phút trước', joined:'2021-11-30' },
  { id:'u9', name:'Trương Mỹ Linh', email:'linh.truong@swo.vn', dept:'ns', role:'staff', status:'active', last:'4 giờ trước', joined:'2023-08-01' },
  { id:'u10', name:'Lý Gia Huy', email:'huy.ly@swo.vn', dept:'kd', role:'sales', status:'invited', last:'—', joined:'2026-06-03' },
  { id:'u11', name:'Phan Tuấn Anh', email:'anh.phan@swo.vn', dept:'vh', role:'staff', status:'invited', last:'—', joined:'2026-06-05' },
  { id:'u12', name:'Đỗ Quỳnh Như', email:'nhu.do@swo.vn', dept:'tckt', role:'accountant', status:'disabled', last:'2 tuần trước', joined:'2022-07-18' },
];
const USER_STATUS = {
  active:   { vi:'Đang hoạt động', en:'Active',   cls:'badge-success' },
  invited:  { vi:'Đã mời',         en:'Invited',  cls:'badge-warning' },
  disabled: { vi:'Vô hiệu hóa',    en:'Disabled', cls:'badge-neutral' },
};

// Quyền xem GIÁ VỐN & BIÊN LỢI NHUẬN. Mặc định theo vai trò (appSettings.costVisibility:
// 'admin' | 'manager'(admin+manager) | 'all'), có ngoại lệ theo từng người
// (appSettings.costViewOverrides = { [userId]: 'allow' | 'deny' }). Mặc định 'manager'.
function canViewCost(p){
  const s = (p && p.appSettings) || {};
  const me = (p && p.me) || {};
  const ov = (s.costViewOverrides || {})[me.id];
  if (ov === 'allow') return true;
  if (ov === 'deny') return false;
  const v = s.costVisibility || 'manager';
  if (v === 'all') return true;
  if (v === 'manager') return p.role === 'admin' || p.role === 'manager';
  return p.role === 'admin';
}

Object.assign(window, {
  DEPARTMENTS, PERM_RESOURCES, PERM_ACTIONS, mkPerms, ROLES, rolePermCount, USERS_LIST, USER_STATUS, canViewCost,
});
