/* Shared mock data + helpers */
const DEPARTMENTS = [
  'Human Resources', 'IT', 'Finance', 'Sales', 'Operations',
  'Customer Support', 'Engineering', 'PMO / Digital Transformation'
];

const COMPETENCIES = [
  { name: 'Communication', group: 'Core', score: 4.2, trend: '+0.3', desc: 'Clarity, listening and structured information sharing across teams.' },
  { name: 'Teamwork', group: 'Core', score: 4.4, trend: '+0.1', desc: 'Collaboration, support and shared accountability for team outcomes.' },
  { name: 'Accountability', group: 'Core', score: 4.1, trend: '0.0', desc: 'Ownership of commitments, deliverables and follow-through.' },
  { name: 'Problem Solving', group: 'Core', score: 3.9, trend: '-0.1', desc: 'Diagnosing issues, weighing options and reaching practical decisions.' },
  { name: 'Customer Focus', group: 'Customer Orientation', score: 4.3, trend: '+0.2', desc: 'Understanding internal/external customer needs and acting on them.' },
  { name: 'Change Adaptability', group: 'Change Management', score: 3.7, trend: '+0.4', desc: 'Responding constructively to shifting priorities and ambiguity.' },
  { name: 'Process Discipline', group: 'Operational Excellence', score: 4.0, trend: '0.0', desc: 'Following standards, documenting work and improving processes.' },
  { name: 'Leadership', group: 'Leadership', score: 3.8, trend: '+0.2', desc: 'Direction-setting, coaching and developing others.' },
  { name: 'Digital Awareness', group: 'Digital', score: 3.6, trend: '+0.5', desc: 'Comfort with digital tools, data and modern ways of working.' },
  { name: 'Collaboration', group: 'Core', score: 4.2, trend: '+0.1', desc: 'Cross-functional partnership and constructive conflict resolution.' },
  { name: 'Strategic Thinking', group: 'Leadership', score: 3.5, trend: '+0.3', desc: 'Long-term framing, prioritisation and connecting decisions to goals.' },
  { name: 'Coaching Others', group: 'Leadership', score: 3.9, trend: '+0.2', desc: 'Building skills in others through feedback, stretch tasks and questions.' },
];

const COMP_GROUPS = [
  { name: 'Core Competencies', icon: 'layers', tint: 'tint-indigo' },
  { name: 'Leadership Competencies', icon: 'flag', tint: 'tint-violet' },
  { name: 'Digital Competencies', icon: 'sparkle', tint: 'tint-sky' },
  { name: 'Customer Orientation', icon: 'target', tint: 'tint-mint' },
  { name: 'Operational Excellence', icon: 'shield', tint: 'tint-emerald' },
  { name: 'Change Management', icon: 'cycles', tint: 'tint-amber' },
  { name: 'Department-Specific', icon: 'dept', tint: 'tint-slate' },
];

const CYCLES = [
  { id: 'C-2026-001', name: 'Q2 2026 Leadership 360', type: '360 Feedback', target: '74 Leaders', start: 'May 02, 2026', end: 'May 30, 2026', status: 'Active', completion: 68, owner: 'Defne A.' },
  { id: 'C-2026-002', name: 'Engineering Peer Review', type: 'Peer Review', target: '186 Engineers', start: 'May 10, 2026', end: 'Jun 14, 2026', status: 'Active', completion: 42, owner: 'Ergin K.' },
  { id: 'C-2026-003', name: 'May Pulse — Engagement', type: 'Pulse Survey', target: 'All Employees (1,284)', start: 'May 12, 2026', end: 'May 19, 2026', status: 'Active', completion: 81, owner: 'HR Ops' },
  { id: 'C-2026-004', name: 'Sales Org Upward Feedback', type: 'Upward Feedback', target: '32 Managers', start: 'May 05, 2026', end: 'May 26, 2026', status: 'Reporting', completion: 100, owner: 'Selin Y.' },
  { id: 'C-2026-005', name: 'Annual Engagement 2026', type: 'Engagement Survey', target: 'All Employees', start: 'Jun 02, 2026', end: 'Jun 16, 2026', status: 'Draft', completion: 0, owner: 'People Analytics' },
  { id: 'C-2026-006', name: 'Customer Support 180', type: '180 Evaluation', target: '94 Specialists', start: 'Apr 15, 2026', end: 'May 05, 2026', status: 'Closed', completion: 96, owner: 'HR Ops' },
  { id: 'C-2026-007', name: 'Finance Department Survey', type: 'Department Survey', target: 'Finance (62)', start: 'Mar 02, 2026', end: 'Mar 18, 2026', status: 'Archived', completion: 100, owner: 'Selin Y.' },
  { id: 'C-2026-008', name: 'Manager Calibration 360', type: 'Manager Review', target: '142 Managers', start: 'May 15, 2026', end: 'Jun 02, 2026', status: 'Active', completion: 24, owner: 'Defne A.' },
];

const TEMPLATES = [
  { name: 'General Employee 360 Form', type: '360 Feedback', dept: 'All', questions: 28, coverage: 8, updated: 'May 02, 2026', status: 'Published', tint: 'tint-indigo' },
  { name: 'Leadership 360 Form', type: '360 Feedback', dept: 'Leadership', questions: 36, coverage: 10, updated: 'Apr 22, 2026', status: 'Published', tint: 'tint-violet' },
  { name: 'HR Department Evaluation', type: 'Department Survey', dept: 'Human Resources', questions: 22, coverage: 6, updated: 'Apr 28, 2026', status: 'Draft', tint: 'tint-mint' },
  { name: 'IT Support Competency Form', type: 'Manager Review', dept: 'IT', questions: 24, coverage: 7, updated: 'Apr 30, 2026', status: 'Published', tint: 'tint-sky' },
  { name: 'Sales Team Feedback Form', type: 'Peer Review', dept: 'Sales', questions: 20, coverage: 5, updated: 'May 03, 2026', status: 'Published', tint: 'tint-amber' },
  { name: 'Customer Support Survey', type: 'Upward Feedback', dept: 'Customer Support', questions: 18, coverage: 5, updated: 'Apr 19, 2026', status: 'Published', tint: 'tint-emerald' },
  { name: 'Engineering Team Review', type: 'Peer Review', dept: 'Engineering', questions: 30, coverage: 8, updated: 'May 05, 2026', status: 'Published', tint: 'tint-indigo' },
  { name: 'Pulse Engagement Survey', type: 'Pulse Survey', dept: 'All', questions: 8, coverage: 4, updated: 'May 10, 2026', status: 'Published', tint: 'tint-rose' },
];

const QUESTIONS = [
  { text: 'Bu kişi ekipler arası iş birliği gerektiren konularda açık ve zamanında iletişim kurar.', competency: 'Communication', dept: 'All', rater: 'Peer', type: '1–5 Likert', scale: '1–5 + N/A', required: true, active: true, lang: 'TR' },
  { text: 'Demonstrates ownership of outcomes, even when challenges arise.', competency: 'Accountability', dept: 'All', rater: 'Manager', type: '1–5 Likert', scale: '1–5 + N/A', required: true, active: true, lang: 'EN' },
  { text: 'Proactively shares knowledge with the team to support shared goals.', competency: 'Teamwork', dept: 'All', rater: 'Peer', type: '1–5 Likert', scale: '1–5', required: true, active: true, lang: 'EN' },
  { text: 'Breaks complex problems into clear, actionable steps.', competency: 'Problem Solving', dept: 'Engineering', rater: 'Manager', type: '1–5 Likert', scale: '1–5 + N/A', required: true, active: true, lang: 'EN' },
  { text: 'Responds constructively to changes in scope or priorities.', competency: 'Change Adaptability', dept: 'All', rater: 'Peer', type: '1–5 Likert', scale: '1–5', required: false, active: true, lang: 'EN' },
  { text: 'What would you like this person to start, stop or continue doing?', competency: 'Communication', dept: 'All', rater: 'All', type: 'Open-ended', scale: 'Text', required: false, active: true, lang: 'EN' },
  { text: 'Internal customers receive a timely, respectful response from this person.', competency: 'Customer Focus', dept: 'Customer Support', rater: 'Internal Customer', type: '1–5 Likert', scale: '1–5 + N/A', required: true, active: true, lang: 'EN' },
  { text: 'Bu kişi geri bildirimleri açık ve yapıcı bir şekilde verir.', competency: 'Coaching Others', dept: 'All', rater: 'Direct Report', type: '1–5 Likert', scale: '1–5 + N/A', required: true, active: true, lang: 'TR' },
  { text: 'Frames decisions with the long-term strategy in mind.', competency: 'Strategic Thinking', dept: 'Leadership', rater: 'Manager', type: '1–5 Likert', scale: '1–5', required: false, active: false, lang: 'EN' },
  { text: 'I would recommend this team as a great place to work. (eNPS)', competency: 'Engagement', dept: 'All', rater: 'Self', type: '0–10 NPS', scale: '0–10', required: true, active: true, lang: 'EN' },
];

const TEAM_MEMBERS = [
  { name: 'Aylin Demir', role: 'Sr. Product Designer', dept: 'Engineering', avatar: '#6B73F0', initials: 'AD' },
  { name: 'Mert Yılmaz',  role: 'Engineering Manager', dept: 'Engineering', avatar: '#8B5CF6', initials: 'MY' },
  { name: 'Ceren Kaya',   role: 'Backend Engineer',    dept: 'Engineering', avatar: '#14B8A6', initials: 'CK' },
  { name: 'Burak Aslan',  role: 'Data Scientist',      dept: 'PMO / Digital Transformation', avatar: '#38BDF8', initials: 'BA' },
  { name: 'Selin Öztürk', role: 'Product Manager',     dept: 'Engineering', avatar: '#F59E0B', initials: 'SÖ' },
  { name: 'Emre Şahin',   role: 'Frontend Engineer',   dept: 'Engineering', avatar: '#EF4444', initials: 'EŞ' },
  { name: 'Defne Aydın',  role: 'HR Business Partner', dept: 'Human Resources', avatar: '#6D28D9', initials: 'DA' },
  { name: 'Kerem Doğan',  role: 'QA Lead',             dept: 'Engineering', avatar: '#0D9488', initials: 'KD' },
];

const MY_EVALS = [
  { person: 'Aylin Demir', role: 'Sr. Product Designer', relationship: 'Peer', cycle: 'Q2 2026 Leadership 360', due: 'May 22, 2026', estTime: '8 min', progress: 0, status: 'Not started', avatar: '#6B73F0', initials: 'AD', questionsTotal: 12 },
  { person: 'Mert Yılmaz', role: 'Engineering Manager', relationship: 'Upward (Manager)', cycle: 'Sales Org Upward Feedback', due: 'May 26, 2026', estTime: '12 min', progress: 35, status: 'In progress', avatar: '#8B5CF6', initials: 'MY', questionsTotal: 18 },
  { person: 'Self', role: 'Sr. Frontend Engineer', relationship: 'Self', cycle: 'Q2 2026 Leadership 360', due: 'May 22, 2026', estTime: '10 min', progress: 70, status: 'In progress', avatar: '#14B8A6', initials: 'You', questionsTotal: 14 },
  { person: 'Ceren Kaya', role: 'Backend Engineer', relationship: 'Peer', cycle: 'Engineering Peer Review', due: 'May 19, 2026', estTime: '8 min', progress: 100, status: 'Submitted', avatar: '#38BDF8', initials: 'CK', questionsTotal: 12 },
  { person: 'Burak Aslan', role: 'Data Scientist', relationship: 'Peer', cycle: 'Engineering Peer Review', due: 'May 14, 2026', estTime: '8 min', progress: 0, status: 'Overdue', avatar: '#F59E0B', initials: 'BA', questionsTotal: 12 },
  { person: 'Emre Şahin', role: 'Frontend Engineer', relationship: 'Direct Report', cycle: 'Q2 2026 Leadership 360', due: 'May 24, 2026', estTime: '10 min', progress: 100, status: 'Submitted', avatar: '#EF4444', initials: 'EŞ', questionsTotal: 14 },
];

const STATUS_TO_BADGE = {
  'Active': 'badge-active',
  'Draft': 'badge-draft',
  'Closed': 'badge-closed',
  'Reporting': 'badge-reporting',
  'Archived': 'badge-archived',
  'Published': 'badge-active',
  'Not started': 'badge-draft',
  'In progress': 'badge-warning',
  'Submitted': 'badge-active',
  'Overdue': 'badge-danger',
};

const initialsOf = (name) => name.split(' ').map(p => p[0]).join('').slice(0, 2).toUpperCase();

const Avatar = ({ name, color, size = 'sm', initials, className = '' }) => {
  const init = initials || (name ? initialsOf(name) : '?');
  const cls = size === 'md' ? 'avatar-md' : size === 'lg' ? 'avatar-lg' : 'avatar-sm';
  return (
    <div className={`avatar-sm ${cls} ${className}`} style={{ background: color || '#6B73F0' }}>
      {init}
    </div>
  );
};

const Badge = ({ children, status, kind, className = '' }) => {
  const cls = status ? STATUS_TO_BADGE[status] || 'badge-soft' : (kind ? `badge-${kind}` : 'badge-soft');
  return (
    <span className={`badge ${cls} ${className}`}>
      <span className="dot"></span>
      {children || status}
    </span>
  );
};

const Progress = ({ value, tone = 'default', showLabel = false, height }) => {
  const cls = tone === 'success' ? 'success' : tone === 'warning' ? 'warning' : tone === 'danger' ? 'danger' : '';
  const auto = value >= 80 ? 'success' : value >= 50 ? '' : value > 0 ? 'warning' : '';
  return (
    <div className="progress-row" style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      <div className={`progress ${tone === 'auto' ? auto : cls}`} style={{ flex: 1, height: height || 6 }}>
        <div className="progress-bar" style={{ width: `${Math.min(100, Math.max(0, value))}%` }}></div>
      </div>
      {showLabel && <span style={{ fontSize: 12, fontWeight: 600, color: 'var(--ink-700)', minWidth: 32, textAlign: 'right' }}>{value}%</span>}
    </div>
  );
};

const StatCard = ({ icon, tint = 'tint-indigo', label, value, delta, deltaDir, foot }) => (
  <div className="stat-card">
    <div className={`stat-icon ${tint}`}>
      <Icon name={icon} size={18} />
    </div>
    <div className="stat-label">{label}</div>
    <div className="stat-value">{value}</div>
    <div className="stat-foot">
      {delta && (
        <span className={`delta ${deltaDir === 'up' ? 'up' : deltaDir === 'down' ? 'down' : 'neutral'}`}>
          <Icon name={deltaDir === 'up' ? 'arrow_up' : deltaDir === 'down' ? 'arrow_down' : 'arrow_right'} size={11} />
          {delta}
        </span>
      )}
      {foot}
    </div>
  </div>
);

Object.assign(window, {
  DEPARTMENTS, COMPETENCIES, COMP_GROUPS, CYCLES, TEMPLATES, QUESTIONS, TEAM_MEMBERS, MY_EVALS,
  STATUS_TO_BADGE, initialsOf, Avatar, Badge, Progress, StatCard
});
