'use client';

import React, { useState } from 'react';
import { BookOpen, Copy, Check, Terminal, ShieldCheck, Zap, Code2 } from 'lucide-react';
import SmartAuthCard from '@/components/ui/smartauth/SmartAuthCard';
import SmartAuthBadge from '@/components/ui/smartauth/SmartAuthBadge';
import SmartAuthButton from '@/components/ui/smartauth/SmartAuthButton';

export default function ApiDocumentationPage() {
  const [copiedSection, setCopiedSection] = useState<string | null>(null);

  const copyCode = (id: string, code: string) => {
    navigator.clipboard.writeText(code);
    setCopiedSection(id);
    setTimeout(() => setCopiedSection(null), 2000);
  };

  const ninCurl = `curl -X POST https://smartauth.ng/api/v1/verification/nin \\
  -H "Authorization: Bearer sk_live_your_key" \\
  -H "Content-Type: application/json" \\
  -d '{
    "nin": "12345678901"
  }'`;

  const phoneCurl = `curl -X POST https://smartauth.ng/api/v1/verification/phone-nin \\
  -H "Authorization: Bearer sk_live_your_key" \\
  -H "Content-Type: application/json" \\
  -d '{
    "phone": "08012345678",
    "nin": "12345678901"
  }'`;

  const responseJson = `{
  "success": true,
  "data": {
    "requestId": "VRF-NIN-2026-992140",
    "verificationType": "NIN",
    "status": "SUCCESS",
    "matched": true,
    "confidenceScore": 100,
    "identity": {
      "nin": "1234****901",
      "firstName": "Chinedu",
      "lastName": "Okafor",
      "gender": "M",
      "dateOfBirth": "1992-04-14",
      "phone": "0801****678",
      "stateOfOrigin": "Anambra"
    },
    "unitsCharged": 10,
    "latencyMs": 142
  }
}`;

  return (
    <div className="space-y-7 max-w-5xl animate-in fade-in duration-150">
      {/* Header */}
      <div>
        <h1 className="text-2xl font-extrabold text-slate-900 tracking-tight">API Documentation & Reference</h1>
        <p className="text-xs text-slate-500 mt-1">
          Complete REST API reference for programmatic integration with the SmartAuth identity verification engine.
        </p>
      </div>

      {/* Base URL & Authentication Card */}
      <SmartAuthCard title="Authentication & Base URL" headerIcon={Zap}>
        <div className="space-y-4 text-xs text-slate-700">
          <p>
            All API requests must be sent over <strong className="text-slate-900 font-mono">HTTPS</strong> and authenticated using Bearer Token authorization header:
          </p>
          <div className="p-3 bg-slate-900 text-emerald-400 font-mono rounded-xl border border-slate-800">
            Authorization: Bearer sk_live_your_api_key_here
          </div>
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-2">
            <div className="p-3 bg-slate-50 border border-slate-200 rounded-xl">
              <span className="font-bold text-slate-900 font-mono block">Sandbox URL:</span>
              <span className="font-mono text-emerald-800 text-[11px]">https://smartauth.ng/api/v1</span>
            </div>
            <div className="p-3 bg-slate-50 border border-slate-200 rounded-xl">
              <span className="font-bold text-slate-900 font-mono block">Production URL:</span>
              <span className="font-mono text-emerald-800 text-[11px]">https://smartauth.ng/api/v1</span>
            </div>
          </div>
        </div>
      </SmartAuthCard>

      {/* Endpoint 1: NIN Verification */}
      <SmartAuthCard
        title="1. NIN Identity Lookup"
        subtitle="POST /api/v1/verification/nin (Cost: 10 Units)"
        headerIcon={ShieldCheck}
        action={
          <button
            onClick={() => copyCode('nin', ninCurl)}
            className="px-3 py-1 bg-slate-100 hover:bg-slate-200 text-slate-800 font-bold text-xs rounded-lg inline-flex items-center gap-1.5 transition-colors"
          >
            {copiedSection === 'nin' ? <Check className="w-3.5 h-3.5 text-emerald-600" /> : <Copy className="w-3.5 h-3.5" />}
            <span>Copy cURL</span>
          </button>
        }
      >
        <pre className="p-4 rounded-xl bg-slate-900 text-emerald-400 font-mono text-xs overflow-x-auto leading-relaxed border border-slate-800">
          {ninCurl}
        </pre>
      </SmartAuthCard>

      {/* Endpoint 2: Phone to NIN Verification */}
      <SmartAuthCard
        title="2. Phone-to-NIN Verification"
        subtitle="POST /api/v1/verification/phone-nin (Cost: 15 Units)"
        headerIcon={ShieldCheck}
        action={
          <button
            onClick={() => copyCode('phone', phoneCurl)}
            className="px-3 py-1 bg-slate-100 hover:bg-slate-200 text-slate-800 font-bold text-xs rounded-lg inline-flex items-center gap-1.5 transition-colors"
          >
            {copiedSection === 'phone' ? <Check className="w-3.5 h-3.5 text-emerald-600" /> : <Copy className="w-3.5 h-3.5" />}
            <span>Copy cURL</span>
          </button>
        }
      >
        <pre className="p-4 rounded-xl bg-slate-900 text-emerald-400 font-mono text-xs overflow-x-auto leading-relaxed border border-slate-800">
          {phoneCurl}
        </pre>
      </SmartAuthCard>

      {/* Sample Response */}
      <SmartAuthCard title="Standard JSON Response" subtitle="NDPR-compliant masked identity schema" headerIcon={Code2}>
        <pre className="p-4 rounded-xl bg-slate-900 text-slate-300 font-mono text-xs overflow-x-auto leading-relaxed border border-slate-800">
          {responseJson}
        </pre>
      </SmartAuthCard>
    </div>
  );
}
