import React from 'react';
import { Globe, Power, Server, Shield, User, Layers, Lock, Wifi, RotateCcw } from 'lucide-react';
import { cn } from '@/lib/utils';

interface LdapManagerProps {
  ldapConfig: any;
  setLdapConfig: (config: any) => void;
  testingLdap: boolean;
  onTestLdap: () => void;
  onUpdateLdap: () => void;
}

export default function LdapManager({
  ldapConfig,
  setLdapConfig,
  testingLdap,
  onTestLdap,
  onUpdateLdap
}: LdapManagerProps) {
  return (
    <div className="level-1 p-4 md:p-6 space-y-3 transition-all group/ldap">
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-3">
          <div className="w-10 h-10 bg-primary/10 flex items-center justify-center text-primary border border-primary/20">
            <Globe size={20} />
          </div>
          <h2 className="text-xl font-normal text-white tracking-tight">Enterprise LDAP</h2>
        </div>
      </div>

      <div className="flex items-center justify-between p-4 lg:p-5 xl:p-6 lg:p-8 xl:p-10 level-0 border border-border/60 group transition-all hover:border-primary/30">
        <div className="flex items-center gap-3">
          <div className={cn(
            "w-8 h-8 flex items-center justify-center transition-all",
            ldapConfig.ldap_enabled ? "status-success" : "bg-surface-bright text-muted"
          )}>
            <Power size={14} />
          </div>
          <div>
            <p className="card-title leading-none">LDAP Authorization</p>
            <p className={cn("card-subtitle mt-1", ldapConfig.ldap_enabled ? "text-success/80" : "text-muted")}>
              {ldapConfig.ldap_enabled ? "External Directory Active" : "Internal Authentication Only"}
            </p>
          </div>
        </div>
        <button 
          onClick={() => setLdapConfig({...ldapConfig, ldap_enabled: !ldapConfig.ldap_enabled})}
          className={cn(
            "w-10 h-5 relative transition-all duration-300 border",
            ldapConfig.ldap_enabled ? "bg-primary border-primary" : "bg-surface-bright border-border"
          )}
        >
          <div className={cn(
            "absolute top-0.5 w-3.5 h-3.5 bg-black transition-all",
            ldapConfig.ldap_enabled ? "right-0.5" : "left-0.5"
          )} />
        </button>
      </div>

      <div className="grid sm:grid-cols-2 gap-3">
        <div className="space-y-1">
          <label className="text-sm font-normal text-secondary tracking-tight ml-1">LDAP URI</label>
          <div className="relative">
            <Server className="absolute left-3 top-1/2 -translate-y-1/2 text-muted" size={14} />
            <input
              type="text"
              placeholder="ldap://host:389"
              value={ldapConfig.server_uri}
              onChange={(e) => setLdapConfig({...ldapConfig, server_uri: e.target.value})}
              className="input-field w-full pl-9 text-xs font-mono"
            />
          </div>
        </div>
        <div className="space-y-1">
          <label className="text-sm font-normal text-secondary tracking-tight ml-1">Context DN</label>
          <div className="relative">
            <Shield className="absolute left-3 top-1/2 -translate-y-1/2 text-muted" size={14} />
            <input
              type="text"
              placeholder="dc=example,dc=com"
              value={ldapConfig.base_dn}
              onChange={(e) => setLdapConfig({...ldapConfig, base_dn: e.target.value})}
              className="input-field w-full pl-9 text-xs font-mono"
            />
          </div>
        </div>
      </div>

      <div className="grid sm:grid-cols-2 gap-3">
        <div className="space-y-1">
          <label className="text-sm font-normal text-secondary tracking-tight ml-1">User DN Template</label>
          <div className="relative">
            <User className="absolute left-3 top-1/2 -translate-y-1/2 text-muted" size={14} />
            <input
              type="text"
              placeholder="uid={username},ou=people..."
              value={ldapConfig.user_template}
              onChange={(e) => setLdapConfig({...ldapConfig, user_template: e.target.value})}
              className="input-field w-full pl-9 text-xs font-mono"
            />
          </div>
        </div>
        <div className="space-y-1">
          <label className="text-sm font-normal text-secondary tracking-tight ml-1">Groups Base DN</label>
          <div className="relative">
            <Layers className="absolute left-3 top-1/2 -translate-y-1/2 text-muted" size={14} />
            <input
              type="text"
              placeholder="ou=groups"
              value={ldapConfig.groups_dn}
              onChange={(e) => setLdapConfig({...ldapConfig, groups_dn: e.target.value})}
              className="input-field w-full pl-9 text-xs font-mono"
            />
          </div>
        </div>
      </div>

      <div className="flex items-center justify-between p-4 lg:p-5 xl:p-6 lg:p-8 xl:p-10 level-0 border border-border/60 group transition-all hover:border-primary/30">
        <div className="flex items-center gap-3">
          <div className={cn(
            "w-8 h-8 flex items-center justify-center transition-all",
            ldapConfig.use_tls ? "bg-primary/10 text-primary" : "bg-surface-bright text-muted"
          )}>
            <Lock size={14} />
          </div>
          <div>
            <p className="card-title leading-none">Use TLS</p>
            <p className={cn("card-subtitle mt-1", ldapConfig.use_tls ? "text-primary/80" : "text-muted")}>
              {ldapConfig.use_tls ? "Encrypted Channel" : "Standard Link"}
            </p>
          </div>
        </div>
        <button 
          onClick={() => setLdapConfig({...ldapConfig, use_tls: !ldapConfig.use_tls})}
          className={cn(
            "w-10 h-5 relative transition-all duration-300 border",
            ldapConfig.use_tls ? "bg-primary border-primary" : "bg-surface-bright border-border"
          )}
        >
          <div className={cn(
            "absolute top-0.5 w-3.5 h-3.5 bg-black transition-all",
            ldapConfig.use_tls ? "right-0.5" : "left-0.5"
          )} />
        </button>
      </div>

      <div className="flex gap-2 pt-0">
        <button
          onClick={onTestLdap}
          disabled={testingLdap}
          className="btn-secondary flex-1"
        >
          {testingLdap ? <RotateCcw size={14} className="animate-spin" /> : <Wifi size={14} />}
          Test Connection
        </button>
        <button
           onClick={onUpdateLdap}
           className="btn-primary flex-[2]"
        >
          <Shield size={14} /> Save LDAP Policy
        </button>
      </div>
    </div>
  );
}
