// tiers.ts
// Scope: access tier definitions, permission groups, and limit enforcement.
//
// Access tiers:
//   limited   — capped daily usage and 2 project slots
//   unlimited — unlimited project slots and unlimited daily usage
//   backstage — unlimited access plus System/root project access

import type { UserTier } from "./auth";

export type TierLimits = {
  runsPerDay: number;
  maxFilesPerRun: number;
  maxLinesPerRun: number;
  bridgeCallsPerDay: number;
  maxProjects: number;
  localAIEnabled: boolean;
  manifestAbstractionEnabled: boolean;
  chatEnabled: boolean;
  diagnosticsEnabled: boolean;
  enhancementsEnabled: boolean;
  restorePointsEnabled: boolean;
  bridgeDeployEnabled: boolean;
  guardrails: "standard" | "none";
  promptControlEnabled: boolean;
  telemetry: {
    structural: boolean;
    logsForced: boolean;
  };
};

export type TierRecord = {
  id: UserTier;
  label: string;
  licensed: boolean;
  tagline: string;
  description: string;
  limits: TierLimits;
};

export const TIERS: Record<UserTier, TierRecord> = {
  limited: {
    id: "limited",
    label: "Limited",
    licensed: false,
    tagline: "Limited daily use with two project slots.",
    description: "Limited access for testing the system with capped RECORD runs and two project slots.",
    limits: {
      runsPerDay: 5,
      maxFilesPerRun: -1,
      maxLinesPerRun: -1,
      bridgeCallsPerDay: 50,
      maxProjects: 2,
      localAIEnabled: true,
      manifestAbstractionEnabled: false,
      chatEnabled: true,
      diagnosticsEnabled: true,
      enhancementsEnabled: false,
      restorePointsEnabled: true,
      bridgeDeployEnabled: false,
      guardrails: "standard",
      promptControlEnabled: false,
      telemetry: { structural: true, logsForced: true }
    }
  },

  unlimited: {
    id: "unlimited",
    label: "Unlimited",
    licensed: true,
    tagline: "Unlimited projects and unlimited daily use.",
    description: "Unlimited access for regular users, with unlimited project slots and unlimited RECORD runs.",
    limits: {
      runsPerDay: -1,
      maxFilesPerRun: -1,
      maxLinesPerRun: -1,
      bridgeCallsPerDay: -1,
      maxProjects: -1,
      localAIEnabled: true,
      manifestAbstractionEnabled: true,
      chatEnabled: true,
      diagnosticsEnabled: true,
      enhancementsEnabled: true,
      restorePointsEnabled: true,
      bridgeDeployEnabled: true,
      guardrails: "standard",
      promptControlEnabled: false,
      telemetry: { structural: true, logsForced: false }
    }
  },

  backstage: {
    id: "backstage",
    label: "Backstage",
    licensed: true,
    tagline: "System-root access for operators.",
    description: "Backstage is an access tier, not a project. It is the only tier allowed to open the System project and write under the system root.",
    limits: {
      runsPerDay: -1,
      maxFilesPerRun: -1,
      maxLinesPerRun: -1,
      bridgeCallsPerDay: -1,
      maxProjects: -1,
      localAIEnabled: true,
      manifestAbstractionEnabled: true,
      chatEnabled: true,
      diagnosticsEnabled: true,
      enhancementsEnabled: true,
      restorePointsEnabled: true,
      bridgeDeployEnabled: true,
      guardrails: "none",
      promptControlEnabled: true,
      telemetry: { structural: true, logsForced: false }
    }
  }
};

export function getTierRecord(tier: UserTier): TierRecord { return TIERS[tier] ?? TIERS.limited; }
export function getLimits(tier: UserTier): TierLimits { return getTierRecord(tier).limits; }
export function isLicensedTier(tier: UserTier): boolean { return getTierRecord(tier).licensed; }
export function canUseLocalAI(tier: UserTier): boolean { return getLimits(tier).localAIEnabled; }
export function canUseManifestAbstraction(tier: UserTier): boolean { return getLimits(tier).manifestAbstractionEnabled; }
export function canUseGarbler(tier: UserTier): boolean { return canUseManifestAbstraction(tier); }
export function canUseChat(tier: UserTier): boolean { return getLimits(tier).chatEnabled; }
export function canUseDiagnostics(tier: UserTier): boolean { return getLimits(tier).diagnosticsEnabled; }
export function canUseEnhancements(tier: UserTier): boolean { return getLimits(tier).enhancementsEnabled; }
export function canUseBridgeDeploy(tier: UserTier): boolean { return getLimits(tier).bridgeDeployEnabled; }
export function canControlPrompts(tier: UserTier): boolean { return getLimits(tier).promptControlEnabled; }
export function hasCustomGuardrails(_tier: UserTier): boolean { return false; }
export function hasStandardGuardrails(tier: UserTier): boolean { return getLimits(tier).guardrails === "standard"; }
export function isDataExchangeTier(_tier: UserTier): boolean { return false; }
export function isUnlimited(tier: UserTier): boolean {
  const l = getLimits(tier);
  return l.runsPerDay === -1 && l.maxProjects === -1;
}

export function tierSummary(tier: UserTier): Record<string, any> {
  const record = getTierRecord(tier);
  const l = record.limits;
  return {
    tier: record.id,
    accessTier: record.id,
    licensed: record.licensed,
    label: record.label,
    tagline: record.tagline,
    description: record.description,
    unlimited: isUnlimited(tier),
    limits: {
      runsPerDay: l.runsPerDay === -1 ? "unlimited" : l.runsPerDay,
      filesPerRun: l.maxFilesPerRun === -1 ? "unlimited" : l.maxFilesPerRun,
      linesPerRun: l.maxLinesPerRun === -1 ? "unlimited" : l.maxLinesPerRun,
      bridgePerDay: l.bridgeCallsPerDay === -1 ? "unlimited" : l.bridgeCallsPerDay,
      projects: l.maxProjects === -1 ? "unlimited" : l.maxProjects
    },
    features: {
      localAI: l.localAIEnabled,
      manifestAbstraction: l.manifestAbstractionEnabled,
      garbler: l.manifestAbstractionEnabled,
      chat: l.chatEnabled,
      diagnostics: l.diagnosticsEnabled,
      enhancements: l.enhancementsEnabled,
      restorePoints: l.restorePointsEnabled,
      bridgeDeploy: l.bridgeDeployEnabled,
      guardrails: l.guardrails,
      promptControl: l.promptControlEnabled,
      systemProject: tier === "backstage"
    },
    telemetry: {
      structural: l.telemetry.structural,
      logsForced: l.telemetry.logsForced
    }
  };
}
