/**
 * Vaultfire Disclaimers System
 *
 * Centralizes all legal disclaimer text for the web and mobile apps.
 * Tracks which disclaimers have been acknowledged by the user (localStorage).
 * Each disclaimer has a unique key — once dismissed, it stays dismissed
 * unless the version changes (forcing re-acknowledgment).
 */

const DISCLAIMER_PREFIX = 'vaultfire_disclaimer_ack_';

export type DisclaimerKey =
  | 'wallet'
  | 'companion'
  | 'agent_hub'
  | 'vns'
  | 'zk_proofs'
  | 'marketplace'
  | 'general';

export interface Disclaimer {
  key: DisclaimerKey;
  title: string;
  body: string;
  version: number; // increment to force re-acknowledgment
  requiresExplicitAck: boolean; // true = must tap "I understand" button; false = auto-dismiss on scroll
}

/* ── Disclaimer Content ── */

export const DISCLAIMERS: Record<DisclaimerKey, Disclaimer> = {
  wallet: {
    key: 'wallet',
    title: 'Non-Custodial Wallet',
    body: 'Vaultfire provides non-custodial wallet connectivity. You are solely responsible for your private keys and seed phrase. Vaultfire cannot recover lost keys under any circumstances. All on-chain transactions are irreversible. This is not financial advice. Use at your own risk.',
    version: 1,
    requiresExplicitAck: true,
  },
  companion: {
    key: 'companion',
    title: 'AI Companion Notice',
    body: 'Vaultfire integrates AI-powered features. Responses generated by AI may not always be accurate, complete, or appropriate. Vaultfire is not a substitute for professional medical, legal, financial, or other expert advice. Conversations are stored locally on your device and are not transmitted to external servers without your consent.',
    version: 1,
    requiresExplicitAck: false,
  },
  agent_hub: {
    key: 'agent_hub',
    title: 'Experimental Platform',
    body: 'The Vaultfire Agent Hub is an experimental platform for AI agent collaboration. Vaultfire does not guarantee the performance, reliability, or accuracy of any registered agent. All accountability bonds and transactions are on-chain and irreversible. Users interact with agents at their own risk. Agent-generated content does not represent the views of Vaultfire Protocol.',
    version: 1,
    requiresExplicitAck: false,
  },
  vns: {
    key: 'vns',
    title: 'VNS Registration Notice',
    body: 'VNS names are registered on-chain and cannot be modified or deleted once created. Registration requires a gas fee paid by the registering agent. Vaultfire does not guarantee name availability. Agent identities are permanent and immutable after registration.',
    version: 1,
    requiresExplicitAck: false,
  },
  zk_proofs: {
    key: 'zk_proofs',
    title: 'Zero-Knowledge Proofs',
    body: 'Zero-knowledge proofs are cryptographic tools provided as-is. The Vaultfire ZK infrastructure is experimental. Users should independently verify proof validity for critical applications. Vaultfire makes no warranty regarding the correctness or completeness of generated proofs.',
    version: 1,
    requiresExplicitAck: false,
  },
  marketplace: {
    key: 'marketplace',
    title: 'Vaultfire Directory Notice',
    body: 'Agent and contributor ratings are based on on-chain data and peer reviews. Vaultfire does not endorse, guarantee, or take responsibility for any agent\'s or contributor\'s services, outputs, or conduct. All directory transactions are peer-to-peer and on-chain.',
    version: 1,
    requiresExplicitAck: false,
  },
  general: {
    key: 'general',
    title: 'Terms of Use',
    body: 'Vaultfire Protocol is experimental software. Smart contracts are deployed on Base, Avalanche, Arbitrum, and Polygon networks. All on-chain transactions are irreversible. This application is not financial, legal, or investment advice. By using Vaultfire, you accept full responsibility for your actions and agree to use this software at your own risk.',
    version: 1,
    requiresExplicitAck: false,
  },
};

/* ── Acknowledgment Persistence ── */

function getAckKey(key: DisclaimerKey, version: number): string {
  return `${DISCLAIMER_PREFIX}${key}_v${version}`;
}

export function isDisclaimerAcknowledged(key: DisclaimerKey): boolean {
  if (typeof window === 'undefined') return false;
  const disclaimer = DISCLAIMERS[key];
  const storageKey = getAckKey(key, disclaimer.version);
  try {
    return localStorage.getItem(storageKey) === 'true';
  } catch {
    return false;
  }
}

export function acknowledgeDisclaimer(key: DisclaimerKey): void {
  if (typeof window === 'undefined') return;
  const disclaimer = DISCLAIMERS[key];
  const storageKey = getAckKey(key, disclaimer.version);
  try {
    localStorage.setItem(storageKey, 'true');
  } catch { /* ignore */ }
}

export function resetDisclaimer(key: DisclaimerKey): void {
  if (typeof window === 'undefined') return;
  const disclaimer = DISCLAIMERS[key];
  const storageKey = getAckKey(key, disclaimer.version);
  try {
    localStorage.removeItem(storageKey);
  } catch { /* ignore */ }
}

export function resetAllDisclaimers(): void {
  if (typeof window === 'undefined') return;
  try {
    const keys = Object.keys(localStorage);
    for (const k of keys) {
      if (k.startsWith(DISCLAIMER_PREFIX)) {
        localStorage.removeItem(k);
      }
    }
  } catch { /* ignore */ }
}

export function getAcknowledgedCount(): number {
  if (typeof window === 'undefined') return 0;
  let count = 0;
  for (const key of Object.keys(DISCLAIMERS) as DisclaimerKey[]) {
    if (isDisclaimerAcknowledged(key)) count++;
  }
  return count;
}

/* ── General disclaimer text (for footer / settings) ── */

export const GENERAL_DISCLAIMER_SHORT =
  'Experimental software. Not financial advice. On-chain transactions are irreversible. Use at your own risk.';

export const GENERAL_DISCLAIMER_FULL = `Vaultfire Protocol is experimental software deployed on Base, Avalanche, Arbitrum, and Polygon networks. All on-chain transactions are irreversible. Smart contracts may contain bugs or vulnerabilities. This application does not constitute financial, legal, investment, or professional advice of any kind. Vaultfire provides non-custodial wallet connectivity — you are solely responsible for your private keys. The Vaultfire Agent Hub and Vaultfire Directory are experimental platforms; Vaultfire does not guarantee agent performance or reliability. VNS registrations are permanent and on-chain. By using Vaultfire, you accept these terms and agree to use this software entirely at your own risk. © ${new Date().getFullYear()} Vaultfire Protocol.`;
