'use client';

import { useAdmin } from '@/hooks/useAdmin';
import PageShell from '@/components/PageShell';
import { Shield, LogOut } from 'lucide-react';

import IdentityManager from '@/components/admin/IdentityManager';
import LdapManager from '@/components/admin/LdapManager';
import AiManager from '@/components/admin/AiManager';
import DatabaseManager from '@/components/admin/DatabaseManager';
import CategoryManager from '@/components/admin/CategoryManager';

export default function AdminPage() {
  const admin = useAdmin();

  if (admin.loading) {
    return (
      <PageShell>
        <div className="flex items-center justify-center min-h-[60vh]">
          <div className="w-12 h-12 border-4 border-indigo-500/20 border-t-indigo-500 rounded-full animate-spin" />
        </div>
      </PageShell>
    );
  }

  return (
    <PageShell>
      <div className="p-3 md:p-8 max-w-7xl mx-auto space-y-6 md:space-y-10 mb-20">
        <header className="flex items-center gap-4 mb-8 md:mb-12">
          <div className="p-3 md:p-4 bg-indigo-500/10 rounded-2xl text-indigo-400 border border-indigo-500/20 shadow-xl shadow-indigo-500/5">
            <Shield size={28} className="md:w-8 md:h-8" />
          </div>
          <div>
            <h1 className="text-2xl md:text-3xl font-black tracking-tight text-white leading-tight">Admin Control</h1>
            <p className="text-[10px] md:text-xs text-slate-500 font-bold tracking-tight mt-0.5">System Configuration & Security</p>
          </div>
          <button 
             onClick={admin.handleLogout}
             className="ml-auto p-3 bg-slate-900 border border-slate-800 text-slate-500 hover:text-rose-500 rounded-2xl transition-all active:scale-95"
             title="Secure Logout"
          >
            <LogOut size={20} />
          </button>
        </header>

        <div className="grid lg:grid-cols-2 gap-6 md:gap-8 items-start">
          {/* Left Column: Identity & Integrity */}
          <section className="space-y-6 md:space-y-8">
            <DatabaseManager 
              dbStats={admin.dbStats}
              isBackingUp={admin.isBackingUp}
              onCreateBackup={admin.handleCreateBackup}
              dbSettings={admin.dbSettings}
              onUpdateDbSettings={admin.handleUpdateDbSettings}
              backups={admin.backups}
              onRestore={admin.handleRestore}
              onExport={admin.handleExportDb}
              onImport={admin.handleImportDb}
            />

            <IdentityManager 
              users={admin.users}
              onAddUser={admin.handleAddUser}
              onDeleteUser={admin.handleDeleteUser}
              editingUser={admin.editingUser}
              setEditingUser={admin.setEditingUser}
              editUserForm={admin.editUserForm}
              setEditUserForm={admin.setEditUserForm}
              onUpdateUserSubmit={admin.handleUpdateUserSubmit}
            />
          </section>

          {/* Right Column: Infrastructure */}
          <section className="space-y-6 md:space-y-8">
            <LdapManager 
              ldapConfig={admin.ldapConfig}
              setLdapConfig={admin.setLdapConfig}
              testingLdap={admin.testingLdap}
              onTestLdap={admin.handleTestLdap}
              onUpdateLdap={admin.handleUpdateLdap}
            />
          </section>
        </div>

        {/* Full Width: Category Groups */}
        <CategoryManager 
          categories={admin.categories}
          onAddCategory={admin.handleAddCategory}
          onDeleteCategory={admin.handleDeleteCategory}
          setEditingCategory={admin.setEditingCategory}
          setEditCatForm={admin.setEditCatForm}
          editCatForm={admin.editCatForm}
          editingCategory={admin.editingCategory}
          onUpdateCategorySubmit={admin.handleUpdateCategorySubmit}
        />

        {/* Full Width: AI Intelligence */}
        <AiManager 
          aiConfig={admin.aiConfig}
          handleUpdateAiProvider={admin.handleUpdateAiProvider}
          aiKeys={admin.aiKeys}
          setAiKeys={admin.setAiKeys}
          isSavingKeys={admin.isSavingKeys}
          onSaveAiKeys={admin.handleSaveAiKeys}
          isTestingKeys={admin.isTestingKeys}
          onTestAiKey={admin.handleTestAiKey}
          aiPrompt={admin.aiPrompt}
          setAiPrompt={admin.setAiPrompt}
          isSavingPrompt={admin.isSavingPrompt}
          onUpdatePrompt={admin.handleUpdatePrompt}
        />
      </div>
    </PageShell>
  );
}
