import { PrismaClient } from '@prisma/client';
import { saveSettings } from './actions';

export const dynamic = 'force-dynamic'; // Never statically prerender - requires DB


const prisma = new PrismaClient();

export default async function AdminSettingsPage() {
  const settings = await prisma.setting.findMany();
  
  const getVal = (key: string) => settings.find((s: { key: string; value: string }) => s.key === key)?.value || '';

  return (
    <div className="max-w-4xl mx-auto p-6 bg-white rounded-lg shadow mt-10">
      <h1 className="text-2xl font-bold mb-6 text-foreground">Super Admin - White-Label Settings</h1>
      
      <form action={saveSettings} className="space-y-6">
        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
          
          {/* Branding */}
          <div className="space-y-4">
            <h2 className="text-xl font-semibold border-b pb-2">Branding</h2>
            <div>
              <label className="block text-sm font-medium mb-1">Company Name</label>
              <input type="text" name="companyName" defaultValue={getVal('company_name')} className="w-full border rounded p-2" />
            </div>
            <div className="flex gap-4">
              <div className="flex-1">
                <label className="block text-sm font-medium mb-1">Primary Color</label>
                <input type="color" name="primaryColor" defaultValue={getVal('theme_primary') || '#2563eb'} className="w-full h-10 cursor-pointer" />
              </div>
              <div className="flex-1">
                <label className="block text-sm font-medium mb-1">Secondary Color</label>
                <input type="color" name="secondaryColor" defaultValue={getVal('theme_secondary') || '#10b981'} className="w-full h-10 cursor-pointer" />
              </div>
              <div className="flex-1">
                <label className="block text-sm font-medium mb-1">Accent Color</label>
                <input type="color" name="accentColor" defaultValue={getVal('theme_accent') || '#f59e0b'} className="w-full h-10 cursor-pointer" />
              </div>
            </div>
          </div>

          {/* Integrations */}
          <div className="space-y-4">
            <h2 className="text-xl font-semibold border-b pb-2">Integrations</h2>
            <div>
              <label className="block text-sm font-medium mb-1">BenefitPay IBAN</label>
              <input type="text" name="benefitIban" defaultValue={getVal('benefit_iban')} className="w-full border rounded p-2" />
            </div>
            <div>
              <label className="block text-sm font-medium mb-1">Gemini API Key</label>
              <input type="password" name="geminiApiKey" defaultValue={getVal('gemini_api_key')} className="w-full border rounded p-2" placeholder="AI-..." />
            </div>
          </div>
          
        </div>
        
        <button type="submit" className="bg-primary text-white px-6 py-2 rounded hover:opacity-90 transition">
          Save Settings
        </button>
      </form>
    </div>
  );
}
