// T2 Falcon Admin — mock data + helpers

const seedUsers = [
  { id: 'u1', username: 'thamer',   firstName: 'Thamer',   email: 'thamer@t2.sa',   phone: '+966572838628', role: 'System Admin', permGroup: 'Support', status: 'active' },
  { id: 'u2', username: 'anas',     firstName: 'Anas',     email: 'anas@t2.sa',     phone: '+966572838628', role: 'Operation',    permGroup: 'Ops',     status: 'suspended' },
  { id: 'u3', username: 'Hajeer',   firstName: 'Hajeer',   email: 'hajeer@t2.sa',   phone: '+966572838628', role: 'Products',     permGroup: 'Support', status: 'active' },
  { id: 'u4', username: 'Najla',    firstName: 'Najla',    email: 'najla@t2.sa',    phone: '+966572838628', role: 'System Admin', permGroup: 'Support', status: 'deleted' },
  { id: 'u5', username: 'Faisal',   firstName: 'Faisal',   email: 'faisal@t2.sa',   phone: '+966572838628', role: 'System Admin', permGroup: 'Support', status: 'locked' },
  { id: 'u6', username: 'Abdallah', firstName: 'Abdallah', email: 'abdallah@t2.sa', phone: '+966572838628', role: 'System Admin', permGroup: 'Support', status: 'pending' },
];

// Tree: falcon root -> clients -> sub-nodes
const seedTree = {
  id: 'falcon',
  type: 'root',
  name: 'Falcon',
  users: seedUsers,
  children: [
    { id: 'alrajhi', type: 'client', brand: 'alrajhi', name: 'Al-Rajhi Bank', users: seedUsers, children: [] },
    { id: 'snb',     type: 'client', brand: 'snb',     name: 'Saudi National Bank', users: seedUsers, children: [
      { id: 'snb-corp',     type: 'node', name: 'Corporate & Institutional Banking Division', users: seedUsers, children: [
        { id: 'snb-corp-rm',   type: 'node', name: 'Relationship Management — Large Enterprises', users: seedUsers, children: [] },
        { id: 'snb-corp-trade', type: 'node', name: 'Trade Finance & Documentary Credits', users: seedUsers, children: [] },
      ] },
      { id: 'snb-retail',   type: 'node', name: 'Retail Banking & Wealth Management Group', users: seedUsers, children: [] },
      { id: 'snb-ops',      type: 'node', name: 'Operations, Risk & Regulatory Compliance Department', users: seedUsers, children: [] },
    ] },
    { id: 'bupa',    type: 'client', brand: 'bupa',    name: 'Bupa Arabia for Cooperative Insurance Company', users: seedUsers, children: [] },
    { id: 'aramco',  type: 'client', brand: 'aramco',  name: 'Aramco', users: seedUsers, children: [
      { id: 'hr',       type: 'node', name: 'Human Resources', users: seedUsers, children: [] },
      { id: 'digital',  type: 'node', name: 'Procurement & Supply Chain', users: seedUsers, children: [] },
      { id: 'cc',       type: 'node', name: 'Contact Center', users: seedUsers, children: [
        { id: 'inbound',  type: 'node', name: 'Inbound Call',  users: seedUsers, children: [] },
        { id: 'outbound', type: 'node', name: 'Outbound Call', users: seedUsers, children: [] },
        { id: 'care',     type: 'node', name: 'Customer Care', users: seedUsers, children: [] },
      ] },
      { id: 'marketing', type: 'node', name: 'Marketing', users: seedUsers, children: [] },
      { id: 'itsec',     type: 'node', name: 'IT & Cybersecurity', users: seedUsers, children: [] },
    ] },
    /* BMW: deep 10-level hierarchy demo (BMW Group → ... → Bay 03 station) */
    { id: 'bmw',     type: 'client', brand: 'bmw',     name: 'BMW Group', users: seedUsers, children: [
      { id: 'bmw-l1', type: 'node', name: 'Production & Manufacturing', users: seedUsers, children: [
        { id: 'bmw-l2', type: 'node', name: 'European Plants Network', users: seedUsers, children: [
          { id: 'bmw-l3', type: 'node', name: 'Plant Munich (Werk München)', users: seedUsers, children: [
            { id: 'bmw-l4', type: 'node', name: 'Body Shop Operations', users: seedUsers, children: [
              { id: 'bmw-l5', type: 'node', name: 'Welding Hall A', users: seedUsers, children: [
                { id: 'bmw-l6', type: 'node', name: 'Robotic Cells Cluster 4', users: seedUsers, children: [
                  { id: 'bmw-l7', type: 'node', name: 'Line 7 — Floor Pan Assembly', users: seedUsers, children: [
                    { id: 'bmw-l8', type: 'node', name: 'Station 7B — Spot Weld', users: seedUsers, children: [
                      { id: 'bmw-l9', type: 'node', name: 'Bay 03 — Quality Inspection', users: seedUsers, children: [
                        { id: 'bmw-l10', type: 'node', name: 'Workstation 03-α (Final QA)', users: seedUsers, children: [] },
                      ] },
                    ] },
                  ] },
                ] },
                /* sibling at level 6 to demonstrate guide-line branching */
                { id: 'bmw-l6b', type: 'node', name: 'Robotic Cells Cluster 5', users: seedUsers, children: [] },
              ] },
              { id: 'bmw-l5b', type: 'node', name: 'Welding Hall B', users: seedUsers, children: [] },
            ] },
            { id: 'bmw-l4b', type: 'node', name: 'Paint Shop Operations', users: seedUsers, children: [] },
            { id: 'bmw-l4c', type: 'node', name: 'Final Assembly Lines', users: seedUsers, children: [] },
          ] },
          { id: 'bmw-l3b', type: 'node', name: 'Plant Dingolfing', users: seedUsers, children: [] },
          { id: 'bmw-l3c', type: 'node', name: 'Plant Regensburg', users: seedUsers, children: [] },
        ] },
        { id: 'bmw-l2b', type: 'node', name: 'Americas Plants Network', users: seedUsers, children: [] },
        { id: 'bmw-l2c', type: 'node', name: 'Asia-Pacific Plants Network', users: seedUsers, children: [] },
      ] },
      { id: 'bmw-rd',     type: 'node', name: 'Research & Development', users: seedUsers, children: [] },
      { id: 'bmw-sales',  type: 'node', name: 'Sales & Aftersales', users: seedUsers, children: [] },
      { id: 'bmw-fin',    type: 'node', name: 'Finance & Treasury', users: seedUsers, children: [] },
    ] },
  ],
};

// Flatten tree to a map for quick access
function flattenTree(root, path = [], map = {}) {
  const thisPath = [...path, root.id];
  map[root.id] = { ...root, path: thisPath };
  (root.children || []).forEach(c => flattenTree(c, thisPath, map));
  return map;
}

// Deep clone helper
function clone(x) { return JSON.parse(JSON.stringify(x)); }

// Find node by id in tree (returns reference)
function findNode(tree, id) {
  if (tree.id === id) return tree;
  for (const c of tree.children || []) {
    const r = findNode(c, id);
    if (r) return r;
  }
  return null;
}

// Collect ancestor path ids for id
function pathTo(tree, id, acc = []) {
  if (tree.id === id) return [...acc, id];
  for (const c of tree.children || []) {
    const r = pathTo(c, id, [...acc, tree.id]);
    if (r) return r;
  }
  return null;
}

// Client brand logos — real image assets with inline fallback
const BRAND_IMG = {
  aramco:  'admin/assets/aramco-logo.png',
  snb:     'admin/assets/snb-logo.png',
  bupa:    'admin/assets/bupa-logo.png',
  alrajhi: 'admin/assets/alrajhi-logo.png',
  falcon:  'admin/assets/falcon-logo.png',
};
const BrandLogo = ({ brand, size = 26 }) => {
  const commonStyle = { width: size, height: size };
  const imgSrc = BRAND_IMG[brand];
  if (imgSrc) {
    return (
      <div className={`client-logo bank-${brand}`} style={commonStyle}>
        <img
          src={imgSrc}
          alt={brand}
          width={size}
          height={size}
          style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }}
        />
      </div>
    );
  }
  if (brand === 'bmw') {
    const half = size / 2;
    return (
      <div className="client-logo bank-bmw" style={{
        ...commonStyle,
        position: 'relative',
        borderRadius: '50%',
        background: '#000',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        boxSizing: 'border-box',
        padding: 2,
      }}>
        <div style={{
          width: '100%', height: '100%',
          borderRadius: '50%',
          background: `conic-gradient(from 0deg, #ffffff 0deg 90deg, #1c69d4 90deg 180deg, #ffffff 180deg 270deg, #1c69d4 270deg 360deg)`,
          position: 'relative',
        }}>
          <div style={{
            position: 'absolute', inset: 1,
            borderRadius: '50%',
            border: '0px solid transparent',
          }}/>
        </div>
        <div style={{
          position: 'absolute',
          color: 'white',
          fontSize: Math.round(size * 0.22),
          fontWeight: 700,
          letterSpacing: '0.5px',
          fontFamily: 'system-ui, sans-serif',
          textAlign: 'center',
          width: '100%',
          top: 1,
          lineHeight: 1,
        }}>BMW</div>
      </div>
    );
  }
  return <div className="client-logo node-generic" style={commonStyle}></div>;
};

window.seedTree = seedTree;
window.flattenTree = flattenTree;
window.clone = clone;
window.findNode = findNode;
window.pathTo = pathTo;
window.BrandLogo = BrandLogo;
