import AdminLayout from '@/Layouts/AdminLayout';
import { Head, useForm, Link } from '@inertiajs/react';
import { Save, Plus, X, Calendar, Clock, DollarSign, Mail, ArrowLeft } from 'lucide-react';
import { useState } from 'react';

interface Props {
    settings: {
        booking_slots: string[];
        booking_duration: number;
        audit_fee_ngn: number;
        audit_fee_gbp: number;
        blackout_dates: string[];
    };
}

export default function Settings({ settings }: Props) {
    const { data, setData, patch, processing, errors } = useForm({
        booking_slots: settings.booking_slots,
        booking_duration: settings.booking_duration,
        audit_fee_ngn: settings.audit_fee_ngn,
        audit_fee_gbp: settings.audit_fee_gbp,
        blackout_dates: settings.blackout_dates,
    });

    const [newSlot, setNewSlot] = useState('');
    const [newBlackoutDate, setNewBlackoutDate] = useState('');

    function addSlot() {
        if (newSlot && !data.booking_slots.includes(newSlot)) {
            setData('booking_slots', [...data.booking_slots, newSlot].sort());
            setNewSlot('');
        }
    }

    function removeSlot(slot: string) {
        setData('booking_slots', data.booking_slots.filter(s => s !== slot));
    }

    function addBlackoutDate() {
        if (newBlackoutDate && !data.blackout_dates.includes(newBlackoutDate)) {
            setData('blackout_dates', [...data.blackout_dates, newBlackoutDate].sort());
            setNewBlackoutDate('');
        }
    }

    function removeBlackoutDate(date: string) {
        setData('blackout_dates', data.blackout_dates.filter(d => d !== date));
    }

    function submit(e: React.FormEvent) {
        e.preventDefault();
        patch(route('admin.settings.bookings.update'), {
            preserveScroll: true,
        });
    }

    return (
        <AdminLayout>
            <Head title="Booking Settings" />

            <div className="mb-8">
                <Link 
                    href={route('admin.settings.index')}
                    className="inline-flex items-center gap-2 text-[11px] font-bold uppercase tracking-widest text-[#8C92AC] hover:text-ink transition-colors mb-4"
                >
                    <ArrowLeft className="h-3 w-3" />
                    Back to Settings
                </Link>
                <h1 className="font-display text-[1.625rem] font-normal tracking-[-0.02em] text-ink leading-none">
                    Booking Settings
                </h1>
                <p className="mt-2.5 text-[0.9375rem] text-[#8C92AC]">
                    Manage availability, pricing, and notification preferences.
                </p>
            </div>

            <form onSubmit={submit} className="max-w-4xl space-y-6">
                
                {/* Availability Section */}
                <div className="bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                    <div className="px-6 py-4 border-b border-[#F0EDE8] bg-[#FAFAF8]">
                        <h2 className="text-[14px] font-semibold text-ink flex items-center gap-2">
                            <Clock className="h-4 w-4 text-[#8C92AC]" />
                            Daily Time Slots
                        </h2>
                    </div>
                    <div className="p-6 space-y-6">
                        <div>
                            <label className="text-[12px] font-semibold text-[#8C92AC] uppercase tracking-wider block mb-3">
                                Available Hours
                            </label>
                            <div className="flex flex-wrap gap-2 mb-4">
                                {data.booking_slots.map(slot => (
                                    <span key={slot} className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-[#F0EDE8] text-ink text-[13px] font-medium rounded-lg group">
                                        {slot}
                                        <button 
                                            type="button" 
                                            onClick={() => removeSlot(slot)}
                                            className="text-[#8C92AC] hover:text-red-500"
                                        >
                                            <X className="h-3.5 w-3.5" />
                                        </button>
                                    </span>
                                ))}
                            </div>
                            <div className="flex gap-2 max-w-xs">
                                <input 
                                    type="time" 
                                    value={newSlot}
                                    onChange={e => setNewSlot(e.target.value)}
                                    className="flex-1 px-4 py-2 bg-white border border-[#E8E6DF] rounded-lg text-[13px] outline-none focus:border-primary"
                                />
                                <button 
                                    type="button" 
                                    onClick={addSlot}
                                    className="px-4 py-2 bg-ink text-white rounded-lg text-[13px] font-semibold flex items-center gap-2 hover:bg-primary transition-colors"
                                >
                                    <Plus className="h-4 w-4" /> Add
                                </button>
                            </div>
                        </div>

                        <div>
                            <label className="text-[12px] font-semibold text-[#8C92AC] uppercase tracking-wider block mb-3">
                                Session Duration (Minutes)
                            </label>
                            <input 
                                type="number" 
                                value={data.booking_duration}
                                onChange={e => setData('booking_duration', parseInt(e.target.value))}
                                className="w-32 px-4 py-2 bg-white border border-[#E8E6DF] rounded-lg text-[13px] outline-none focus:border-primary"
                            />
                        </div>
                    </div>
                </div>

                {/* Blackout Dates Section */}
                <div className="bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                    <div className="px-6 py-4 border-b border-[#F0EDE8] bg-[#FAFAF8]">
                        <h2 className="text-[14px] font-semibold text-ink flex items-center gap-2">
                            <Calendar className="h-4 w-4 text-[#8C92AC]" />
                            Blackout Dates
                        </h2>
                    </div>
                    <div className="p-6">
                        <label className="text-[12px] font-semibold text-[#8C92AC] uppercase tracking-wider block mb-3">
                            Blocked Dates (Holidays, Time Off)
                        </label>
                        <div className="flex flex-wrap gap-2 mb-4">
                            {data.blackout_dates.map(date => (
                                <span key={date} className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-red-50 text-red-700 text-[13px] font-medium rounded-lg">
                                    {new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}
                                    <button 
                                        type="button" 
                                        onClick={() => removeBlackoutDate(date)}
                                        className="text-red-400 hover:text-red-600"
                                    >
                                        <X className="h-3.5 w-3.5" />
                                    </button>
                                </span>
                            ))}
                            {data.blackout_dates.length === 0 && (
                                <span className="text-[13px] text-[#8C92AC] italic">No dates blocked yet.</span>
                            )}
                        </div>
                        <div className="flex gap-2 max-w-xs">
                            <input 
                                type="date" 
                                value={newBlackoutDate}
                                onChange={e => setNewBlackoutDate(e.target.value)}
                                className="flex-1 px-4 py-2 bg-white border border-[#E8E6DF] rounded-lg text-[13px] outline-none focus:border-primary"
                            />
                            <button 
                                type="button" 
                                onClick={addBlackoutDate}
                                className="px-4 py-2 bg-ink text-white rounded-lg text-[13px] font-semibold flex items-center gap-2 hover:bg-primary transition-colors"
                            >
                                <Plus className="h-4 w-4" /> Block
                            </button>
                        </div>
                    </div>
                </div>

                {/* Pricing Section */}
                <div className="bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                    <div className="px-6 py-4 border-b border-[#F0EDE8] bg-[#FAFAF8]">
                        <h2 className="text-[14px] font-semibold text-ink flex items-center gap-2">
                            <DollarSign className="h-4 w-4 text-[#8C92AC]" />
                            Audit Report Pricing
                        </h2>
                    </div>
                    <div className="p-6 grid grid-cols-1 sm:grid-cols-2 gap-6">
                        <div>
                            <label className="text-[12px] font-semibold text-[#8C92AC] uppercase tracking-wider block mb-3">
                                Price (NGN)
                            </label>
                            <div className="relative">
                                <span className="absolute left-4 top-1/2 -translate-y-1/2 text-[13px] text-[#8C92AC]">₦</span>
                                <input 
                                    type="number" 
                                    value={data.audit_fee_ngn}
                                    onChange={e => setData('audit_fee_ngn', parseInt(e.target.value))}
                                    className="w-full pl-8 pr-4 py-2 bg-white border border-[#E8E6DF] rounded-lg text-[13px] outline-none focus:border-primary"
                                />
                            </div>
                        </div>
                        <div>
                            <label className="text-[12px] font-semibold text-[#8C92AC] uppercase tracking-wider block mb-3">
                                Price (GBP)
                            </label>
                            <div className="relative">
                                <span className="absolute left-4 top-1/2 -translate-y-1/2 text-[13px] text-[#8C92AC]">£</span>
                                <input 
                                    type="number" 
                                    value={data.audit_fee_gbp}
                                    onChange={e => setData('audit_fee_gbp', parseInt(e.target.value))}
                                    className="w-full pl-8 pr-4 py-2 bg-white border border-[#E8E6DF] rounded-lg text-[13px] outline-none focus:border-primary"
                                />
                            </div>
                        </div>
                    </div>
                </div>

                {/* Save Button */}
                <div className="flex justify-end pt-4">
                    <button 
                        type="submit" 
                        disabled={processing}
                        className="px-8 py-3 bg-ink text-white rounded-xl text-[14px] font-semibold flex items-center gap-2 hover:bg-primary transition-all shadow-sm active:scale-95 disabled:opacity-50"
                    >
                        <Save className="h-4 w-4" />
                        {processing ? 'Saving Changes...' : 'Save Settings'}
                    </button>
                </div>
            </form>
        </AdminLayout>
    );
}
