'use client';

import React, { useState } from 'react';
import { Settings, Server, ShieldCheck, Lock } from 'lucide-react';
import SmartAuthCard from '@/components/ui/smartauth/SmartAuthCard';
import SmartAuthButton from '@/components/ui/smartauth/SmartAuthButton';
import SmartAuthBadge from '@/components/ui/smartauth/SmartAuthBadge';

export default function SuperAdminSettingsPage() {
  const [platformMode, setPlatformMode] = useState('SANDBOX');
  const [newPassword, setNewPassword] = useState('');
  const [saving, setSaving] = useState(false);
  const [msg, setMsg] = useState<string | null>(null);

  const handleSave = async (e: React.FormEvent) => {
    e.preventDefault();
    setSaving(true);
    setMsg(null);
    try {
      if (newPassword) {
        await fetch('/api/v1/account', {
          method: 'PATCH',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ password: newPassword }),
        });
      }
      setMsg('System configuration and password updated successfully.');
      setNewPassword('');
    } catch {
      setMsg('Failed to update settings.');
    } finally {
      setSaving(false);
    }
  };

  return (
    <div className="space-y-7 max-w-4xl animate-in fade-in duration-150">
      <div>
        <h1 className="text-2xl font-extrabold text-slate-900 tracking-tight">System Global Settings</h1>
        <p className="text-xs text-slate-500 mt-1">Configure platform-wide execution flags and root security credentials.</p>
      </div>

      {msg && (
        <div className="p-3 bg-emerald-50 border border-emerald-200 text-emerald-800 rounded-xl text-xs font-semibold">
          {msg}
        </div>
      )}

      <SmartAuthCard title="Global Platform Mode" subtitle="Switch verification behavior between Sandbox simulation and live gateways." headerIcon={Server}>
        <div className="space-y-4">
          <div className="flex flex-wrap items-center gap-3">
            <button
              onClick={() => setPlatformMode('SANDBOX')}
              className={`px-4 py-2 rounded-xl text-xs font-mono font-bold transition-colors ${
                platformMode === 'SANDBOX'
                  ? 'bg-emerald-700 text-white shadow-sm'
                  : 'bg-slate-100 text-slate-600 hover:bg-slate-200'
              }`}
            >
              SANDBOX (Simulated Registry)
            </button>
            <button
              onClick={() => setPlatformMode('PRODUCTION')}
              className={`px-4 py-2 rounded-xl text-xs font-mono font-bold transition-colors ${
                platformMode === 'PRODUCTION'
                  ? 'bg-emerald-700 text-white shadow-sm'
                  : 'bg-slate-100 text-slate-600 hover:bg-slate-200'
              }`}
            >
              PRODUCTION (Live NIMC & BVN)
            </button>
          </div>
          <p className="text-xs text-slate-500">
            Currently running in <strong className="font-mono text-emerald-800">{platformMode}</strong> mode.
          </p>
        </div>
      </SmartAuthCard>

      <SmartAuthCard title="Root Security Credentials" subtitle="Update Super Admin master credentials." headerIcon={Lock}>
        <form onSubmit={handleSave} className="space-y-4 max-w-md">
          <div className="space-y-1.5">
            <label className="text-xs font-bold text-slate-700">New Root Password</label>
            <input
              type="password"
              placeholder="••••••••"
              value={newPassword}
              onChange={(e) => setNewPassword(e.target.value)}
              className="w-full px-4 py-2.5 text-xs rounded-xl border border-slate-200 focus:outline-none focus:ring-2 focus:ring-emerald-500/20"
            />
          </div>

          <div className="pt-2">
            <SmartAuthButton type="submit" variant="primary" size="md" loading={saving}>
              Save System Settings
            </SmartAuthButton>
          </div>
        </form>
      </SmartAuthCard>
    </div>
  );
}
