import { SafetyTier, SafetyPolicy } from '../types/agent';
import { PROTECTED_PATHS } from '../constants/safety';

/**
 * Base policy template for new tiers (Principle 10: DRY).
 */
export const BASE_POLICY_TEMPLATE: Partial<SafetyPolicy> = {
  requireCodeApproval: false,
  requireDeployApproval: true,
  requireFileApproval: false,
  requireShellApproval: true,
  requireMcpApproval: true,
  blockedFilePaths: [...PROTECTED_PATHS],
  maxDeploymentsPerDay: 10,
  maxShellCommandsPerHour: 20,
  maxFileWritesPerHour: 50,
  cognitiveThresholds: {
    minCompletionRate: 0.8,
    maxErrorRate: 0.2,
    minCoherence: 6.0,
    maxMissRate: 0.4,
    maxAvgLatencyMs: 12000,
    maxPivotRate: 0.15,
    minSampleTasks: 5,
  },
};

/**
 * Default safety policies for each tier.
 * Centralized in policy-defaults.ts for clean separation of logic and config (Principle 10).
 */
export const DEFAULT_POLICIES: Record<SafetyTier, SafetyPolicy> = {
  [SafetyTier.LOCAL]: {
    tier: SafetyTier.LOCAL,
    requireCodeApproval: false,
    requireDeployApproval: false,
    requireFileApproval: false,
    requireShellApproval: false,
    requireMcpApproval: false,
    blockedFilePaths: [...PROTECTED_PATHS],
    maxDeploymentsPerDay: 50,
    maxShellCommandsPerHour: 200,
    maxFileWritesPerHour: 500,
    cognitiveThresholds: {
      minCompletionRate: 0.7,
      maxErrorRate: 0.3,
      minCoherence: 5.0,
      maxMissRate: 0.5,
      maxAvgLatencyMs: 15000,
      maxPivotRate: 0.2,
      minSampleTasks: 5,
    },
  },
  [SafetyTier.DEV]: {
    tier: SafetyTier.DEV,
    requireCodeApproval: false,
    requireDeployApproval: true,
    requireFileApproval: false,
    requireShellApproval: false,
    requireMcpApproval: false,
    blockedFilePaths: [...PROTECTED_PATHS],
    maxDeploymentsPerDay: 30,
    maxShellCommandsPerHour: 100,
    maxFileWritesPerHour: 200,
    cognitiveThresholds: {
      minCompletionRate: 0.75,
      maxErrorRate: 0.25,
      minCoherence: 5.5,
      maxMissRate: 0.45,
      maxAvgLatencyMs: 14000,
      maxPivotRate: 0.18,
      minSampleTasks: 5,
    },
  },
  [SafetyTier.TEST]: {
    tier: SafetyTier.TEST,
    requireCodeApproval: false,
    requireDeployApproval: true,
    requireFileApproval: false,
    requireShellApproval: true,
    requireMcpApproval: true,
    blockedFilePaths: [...PROTECTED_PATHS],
    maxDeploymentsPerDay: 20,
    maxShellCommandsPerHour: 50,
    maxFileWritesPerHour: 100,
    cognitiveThresholds: {
      minCompletionRate: 0.8,
      maxErrorRate: 0.2,
      minCoherence: 6.0,
      maxMissRate: 0.4,
      maxAvgLatencyMs: 12000,
      maxPivotRate: 0.15,
      minSampleTasks: 5,
    },
  },
  [SafetyTier.PROD]: {
    tier: SafetyTier.PROD,
    requireCodeApproval: false,
    requireDeployApproval: true,
    requireFileApproval: false,
    requireShellApproval: true,
    requireMcpApproval: true,
    blockedFilePaths: [...PROTECTED_PATHS],
    maxDeploymentsPerDay: 10,
    maxShellCommandsPerHour: 50,
    maxFileWritesPerHour: 100,
    cognitiveThresholds: {
      minCompletionRate: 0.85,
      maxErrorRate: 0.15,
      minCoherence: 7.0,
      maxMissRate: 0.3,
      maxAvgLatencyMs: 10000,
      maxPivotRate: 0.1,
      minSampleTasks: 10,
    },
    timeRestrictions: [
      {
        daysOfWeek: [1, 2, 3, 4, 5], // Weekdays
        startHour: 9,
        endHour: 17,
        timezone: 'America/New_York',
        restrictedActions: ['deployment'],
        restrictionType: 'require_approval',
      },
    ],
  },
};
