/**
 * 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: 'Embris is a non-custodial wallet. You are solely responsible for your private keys and seed phrase. Embris 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: 'Embris is an AI companion powered by large language models. Responses are generated by AI and may not always be accurate, complete, or appropriate. Embris 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 Embris Hub is an experimental platform for AI agent collaboration. Embris 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 Embris or 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 user. Embris does not guarantee name availability. Identity types (Human / AI Agent) 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 Embris ZK infrastructure is experimental. Users should independently verify proof validity for critical applications. Embris makes no warranty regarding the correctness or completeness of generated proofs.',
    version: 1,
    requiresExplicitAck: false,
  },
  marketplace: {
    key: 'marketplace',
    title: 'Embris Directory Notice',
    body: 'Agent and contributor ratings are based on on-chain data and peer reviews. Embris 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: 'Embris by Vaultfire Protocol is experimental software. Smart contracts are deployed on Ethereum, Base, and Avalanche networks. All on-chain transactions are irreversible. This application is not financial, legal, or investment advice. By using Embris, 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 = `Embris by Vaultfire Protocol is experimental software deployed on Ethereum, Base, and Avalanche 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. Embris is a non-custodial wallet — you are solely responsible for your private keys. The Embris Hub and Embris Directory are experimental platforms; Embris does not guarantee agent performance or reliability. VNS registrations are permanent and on-chain. By using Embris, you accept these terms and agree to use this software entirely at your own risk. © ${new Date().getFullYear()} Embris by Vaultfire Protocol.`;
