﻿import React from 'react';
import { Head, useForm } from '@inertiajs/react';
import { StaffLayout } from '@/Layouts/StaffLayout';
import { Settings as SettingsIcon, Save, Shield, CreditCard, DollarSign } from 'lucide-react';

interface Props {
    settings: { [key: string]: string };
}

export default function AdminSettings({ settings }: Props) {
    const form = useForm({
        hotel_name: settings?.hotel_name || 'Primon Hotel',
        hotel_tagline: settings?.hotel_tagline || 'Luxury Boutique Hotel & Spa',
        hotel_address: settings?.hotel_address || 'Naalya, Kampala, Uganda, Uganda',
        hotel_phone: settings?.hotel_phone || '0782 195 634',
        hotel_whatsapp: settings?.hotel_whatsapp || '0782 195 634',
        hotel_email: settings?.hotel_email || 'reservations@primonhotel.com',
        // Finance & Currency
        exchange_rate_ugx: settings?.exchange_rate_ugx || '3800',
        vat_percentage: settings?.vat_percentage || '18',
        service_charge_percentage: settings?.service_charge_percentage || '5',
        // Pesapal 3.0 Credentials
        pesapal_env: settings?.pesapal_env || 'sandbox',
        pesapal_consumer_key: settings?.pesapal_consumer_key || '',
        pesapal_consumer_secret: settings?.pesapal_consumer_secret || '',
        pesapal_ipn_id: settings?.pesapal_ipn_id || '',
        // Operations
        check_in_time: settings?.check_in_time || '14:00',
        check_out_time: settings?.check_out_time || '11:00',
    });

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        form.post('/ssadmin/settings', { preserveScroll: true });
    };

    return (
        <StaffLayout title="System Configuration & Settings">
            <Head title="Settings Center — Primon Hotel" />

            <form onSubmit={handleSubmit} className="space-y-8 max-w-5xl">
                <div className="flex items-center justify-between">
                    <div>
                        <h2 className="font-serif text-2xl font-medium text-[#1A1A1A]">Property Configuration Center</h2>
                        <p className="text-xs text-gray-500">Manage localization, rates, payment keys, and operational rules.</p>
                    </div>

                    <button
                        type="submit"
                        disabled={form.processing}
                        className="bg-[#C5A880] hover:bg-[#DFC7A5] text-[#1A1A1A] px-6 py-2.5 rounded-full text-xs font-bold uppercase tracking-widest transition-all shadow flex items-center gap-2"
                    >
                        <Save className="h-4 w-4" />
                        <span>Save All Changes</span>
                    </button>
                </div>

                {/* Section 1: Property Identity */}
                <div className="bg-[#FBF9F5] p-8 rounded-3xl border border-[#C5A880]/20 shadow-sm space-y-6">
                    <h3 className="font-serif text-xl font-medium text-[#1A1A1A]">Property Details & Contact</h3>

                    <div className="grid grid-cols-1 sm:grid-cols-2 gap-6 text-xs">
                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">Hotel Legal Name</label>
                            <input
                                type="text"
                                value={form.data.hotel_name}
                                onChange={(e) => form.setData('hotel_name', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">Brand Tagline</label>
                            <input
                                type="text"
                                value={form.data.hotel_tagline}
                                onChange={(e) => form.setData('hotel_tagline', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                        </div>

                        <div className="sm:col-span-2">
                            <label className="font-semibold uppercase text-gray-600 block mb-1">Physical Address</label>
                            <input
                                type="text"
                                value={form.data.hotel_address}
                                onChange={(e) => form.setData('hotel_address', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">Reception Phone</label>
                            <input
                                type="text"
                                value={form.data.hotel_phone}
                                onChange={(e) => form.setData('hotel_phone', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">WhatsApp Concierge</label>
                            <input
                                type="text"
                                value={form.data.hotel_whatsapp}
                                onChange={(e) => form.setData('hotel_whatsapp', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                        </div>
                    </div>
                </div>

                {/* Section 2: Financial & Localization */}
                <div className="bg-[#FBF9F5] p-8 rounded-3xl border border-[#C5A880]/20 shadow-sm space-y-6">
                    <h3 className="font-serif text-xl font-medium text-[#1A1A1A]">Currencies & Statutory Taxes</h3>

                    <div className="grid grid-cols-1 sm:grid-cols-3 gap-6 text-xs">
                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">USD to UGX Exchange Rate</label>
                            <input
                                type="number"
                                value={form.data.exchange_rate_ugx}
                                onChange={(e) => form.setData('exchange_rate_ugx', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 font-bold"
                            />
                            <span className="text-[10px] text-gray-500 mt-1 block">e.g. 1 USD = 3,800 UGX</span>
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">VAT Percentage (%)</label>
                            <input
                                type="number"
                                value={form.data.vat_percentage}
                                onChange={(e) => form.setData('vat_percentage', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                            <span className="text-[10px] text-gray-500 mt-1 block">Uganda Standard: 18%</span>
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-gray-600 block mb-1">Service Charge (%)</label>
                            <input
                                type="number"
                                value={form.data.service_charge_percentage}
                                onChange={(e) => form.setData('service_charge_percentage', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5"
                            />
                            <span className="text-[10px] text-gray-500 mt-1 block">Hospitality Standard: 5%</span>
                        </div>
                    </div>
                </div>

                {/* Section 3: Pesapal 3.0 API */}
                <div className="bg-[#1A1A1A] text-[#FBF9F5] p-8 rounded-3xl border border-[#C5A880]/30 shadow-md space-y-6">
                    <div>
                        <span className="text-[10px] uppercase text-[#C5A880] font-semibold block">Payment Gateway</span>
                        <h3 className="font-serif text-xl font-medium text-white">Pesapal 3.0 API Integration</h3>
                    </div>

                    <div className="grid grid-cols-1 sm:grid-cols-2 gap-6 text-xs">
                        <div className="sm:col-span-2">
                            <label className="font-semibold uppercase text-[#C5A880] block mb-1">API Environment</label>
                            <select
                                value={form.data.pesapal_env}
                                onChange={(e) => form.setData('pesapal_env', e.target.value)}
                                className="w-full bg-white/10 border border-white/20 text-white rounded-xl px-3.5 py-2.5"
                            >
                                <option value="sandbox" className="bg-[#1A1A1A]">Sandbox (Testing)</option>
                                <option value="production" className="bg-[#1A1A1A]">Live / Production</option>
                            </select>
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-[#C5A880] block mb-1">Pesapal Consumer Key</label>
                            <input
                                type="text"
                                value={form.data.pesapal_consumer_key}
                                onChange={(e) => form.setData('pesapal_consumer_key', e.target.value)}
                                className="w-full bg-white/10 border border-white/20 text-white rounded-xl px-3.5 py-2.5 font-mono"
                            />
                        </div>

                        <div>
                            <label className="font-semibold uppercase text-[#C5A880] block mb-1">Pesapal Consumer Secret</label>
                            <input
                                type="password"
                                value={form.data.pesapal_consumer_secret}
                                onChange={(e) => form.setData('pesapal_consumer_secret', e.target.value)}
                                className="w-full bg-white/10 border border-white/20 text-white rounded-xl px-3.5 py-2.5 font-mono"
                            />
                        </div>
                    </div>
                </div>
            </form>
        </StaffLayout>
    );
}
