import { canAccessPermission, permissionForEntity } from '../../utils/permissions.js';

const byEntity = (entity, action) => ({
  entity,
  action,
  allows(permissions) {
    return permissions.has(permissionForEntity(entity, action));
  },
});

const byPermission = (permission) => ({
  permission,
  allows(permissions) {
    return canAccessPermission({ permissions: [...permissions] }, permission);
  },
});

const alwaysAllowed = () => ({
  permission: null,
  allows() {
    return true;
  },
});

export const ACCESS_CAPABILITY_GROUPS = {
  routes: [
    { key: 'route.dashboard', label: 'Dashboard', category: 'route', ...alwaysAllowed() },
    { key: 'route.customer', label: 'Customer', category: 'route', ...byEntity('client', 'read') },
    { key: 'route.people', label: 'People', category: 'route', ...byEntity('contact', 'read') },
    { key: 'route.company', label: 'Company', category: 'route', ...byEntity('company', 'read') },
    { key: 'route.lead', label: 'Lead', category: 'route', ...byEntity('lead', 'read') },
    { key: 'route.crm', label: 'CRM', category: 'route', ...byEntity('deal', 'read') },
    { key: 'route.offers', label: 'Offers', category: 'route', ...byEntity('offer', 'read') },
    { key: 'route.sales', label: 'Sales', category: 'route', ...byEntity('quote', 'read') },
    { key: 'route.finance', label: 'Finance', category: 'route', ...byEntity('invoice', 'read') },
    { key: 'route.catalog', label: 'Catalog / Inventory', category: 'route', ...byEntity('product', 'read') },
    { key: 'route.pos', label: 'Point of Sale', category: 'route', ...byPermission('inventory.pos.read') },
    { key: 'route.orders', label: 'Orders / Purchase', category: 'route', ...byEntity('order', 'read') },
    { key: 'route.vendors', label: 'Vendors', category: 'route', ...byEntity('purchaseorder', 'read') },
    { key: 'route.marketing', label: 'Marketing', category: 'route', ...byEntity('campaign', 'read') },
    { key: 'route.tasks', label: 'Tasks', category: 'route', ...byEntity('task', 'read') },
    { key: 'route.operations', label: 'Operations', category: 'route', ...byEntity('branch', 'read') },
    { key: 'route.forms', label: 'Public Forms', category: 'route', ...byEntity('publicform', 'read') },
    { key: 'route.developer', label: 'Developer API Keys', category: 'route', ...byEntity('apikey', 'read') },
    { key: 'route.automations', label: 'Automations', category: 'route', ...byPermission('automations.runner.read') },
    { key: 'route.ai-studio', label: 'AI Studio', category: 'route', ...byPermission('ai.studio.read') },
    { key: 'route.ai-providers', label: 'AI Provider Accounts', category: 'route', ...byEntity('integrationaccount', 'read') },
    {
      key: 'route.settings-access-control',
      label: 'Settings / Access Control',
      category: 'route',
      ...byEntity('role', 'read'),
    },
    {
      key: 'route.settings-user-management',
      label: 'Settings / User Management',
      category: 'route',
      ...byPermission('settings.admin.read'),
    },
    {
      key: 'route.form-builder',
      label: 'Form Builder',
      category: 'route',
      ...byPermission('platform.form-builder.read'),
    },
  ],
  workflows: [
    { key: 'workflow.advance-deal-stage', label: 'Advance Deal Stage', category: 'workflow', ...byEntity('deal', 'update') },
    { key: 'workflow.run-deal-won', label: 'Run Deal Won Workflow', category: 'workflow', ...byPermission('automations.workflow.create') },
    { key: 'workflow.create-follow-up-task', label: 'Create Follow-up Task', category: 'workflow', ...byEntity('task', 'create') },
    { key: 'workflow.create-quote', label: 'Create Quote', category: 'workflow', ...byEntity('quote', 'create') },
    { key: 'workflow.create-invoice', label: 'Create Invoice', category: 'workflow', ...byEntity('invoice', 'create') },
    { key: 'workflow.create-purchase-order', label: 'Create Purchase Order', category: 'workflow', ...byEntity('purchaseorder', 'create') },
    { key: 'workflow.complete-pos-sale', label: 'Complete POS Sale', category: 'workflow', ...byPermission('inventory.pos.create') },
    { key: 'workflow.view-pos-reports', label: 'View POS Reports', category: 'workflow', ...byPermission('inventory.pos.report.read') },
    { key: 'workflow.schedule-campaign', label: 'Schedule Campaign', category: 'workflow', ...byEntity('campaign', 'create') },
    { key: 'workflow.run-due-jobs', label: 'Run Due Jobs', category: 'workflow', ...byPermission('automations.runner.create') },
    { key: 'workflow.manage-users', label: 'Create and Update Users', category: 'workflow', ...byPermission('settings.admin.update') },
    { key: 'workflow.build-forms', label: 'Build Custom Forms', category: 'workflow', ...byPermission('platform.form-builder.create') },
  ],
  ai: [
    { key: 'ai.build-agent', label: 'Build Custom Agents', category: 'ai', ...byPermission('ai.agent.create') },
    { key: 'ai.publish-agent', label: 'Publish AI Agents', category: 'ai', ...byPermission('ai.agent.publish') },
    { key: 'ai.run-agent', label: 'Run AI Agents', category: 'ai', ...byPermission('ai.agent.run') },
    { key: 'ai.approve-action', label: 'Approve Agent Actions', category: 'ai', ...byPermission('ai.approval.decide') },
    { key: 'ai.web-research', label: 'Use Web Research', category: 'ai', ...byPermission('ai.web.research') },
    { key: 'ai.publish-social', label: 'Publish Social Content', category: 'ai', ...byPermission('marketing.social.publish') },
    { key: 'ai.manage-providers', label: 'Manage Provider Credentials', category: 'ai', ...byEntity('integrationaccount', 'update') },
    { key: 'ai.generate-text', label: 'Generate Text', category: 'ai', ...byPermission('ai.studio.create') },
    { key: 'ai.generate-brand-asset', label: 'Generate Brand Asset', category: 'ai', ...byPermission('ai.studio.create') },
    { key: 'ai.draft-campaign', label: 'Draft Campaign', category: 'ai', ...byPermission('ai.studio.create') },
  ],
};

export const buildCapabilityGroups = (effectivePermissions = []) => {
  const permissionSet = new Set(effectivePermissions);

  return Object.fromEntries(
    Object.entries(ACCESS_CAPABILITY_GROUPS).map(([group, capabilities]) => [
      group,
      capabilities.map(({ allows, ...capability }) => ({
        ...capability,
        allowed: allows(permissionSet),
      })),
    ])
  );
};
