import FormInput from '@/Components/FormInput';
import NotificationChannelsSelect from '@/Components/NotificationChannelsSelect';
import SidebarForm from '@/Components/SidebarForm';
import TextInput from '@/Components/TextInput';
import { Contact, SidebarFormBodyProps } from '@/types';
import React from 'react';

interface ContactEditFormProps {
    contact: Contact;
    children: React.ReactNode;
    availableNotificationChannels: string[];
}

type ContactFormData = {
    label: string;
    email: string;
    phone: string | null;
    notification_channels: string[];
};

export default function ContactEditForm({
    contact,
    children: trigger,
    availableNotificationChannels,
}: ContactEditFormProps) {
    const initialData = {
        label: contact.label,
        email: contact.email,
        phone: contact.phone,
        notification_channels: contact.notification_channels,
    };

    const Body = (
        bodyProps: Partial<SidebarFormBodyProps<ContactFormData>>,
    ) => {
        const { data, setData, errors } =
            bodyProps as SidebarFormBodyProps<ContactFormData>;
        return (
            <>
                <FormInput
                    label="Utente"
                    id="user_id"
                    className="mt-8 block w-full"
                >
                    <TextInput
                        value={`${contact.user.name} ${contact.user.surname}`}
                        disabled
                    />
                </FormInput>
                <FormInput
                    label="Etichetta"
                    id="label"
                    className="mt-8 block w-full"
                    error={errors.label}
                >
                    <TextInput
                        value={data.label}
                        onChange={(e) => setData('label', e.target.value)}
                    />
                </FormInput>
                <FormInput
                    label="Email"
                    id="email"
                    className="mt-8 block w-full"
                    error={errors.email}
                >
                    <TextInput
                        value={data.email}
                        onChange={(e) => setData('email', e.target.value)}
                    />
                </FormInput>
                <FormInput
                    label="Numero di telefono"
                    id="phone"
                    className="my-4 block w-full"
                    error={errors.phone}
                >
                    <TextInput
                        value={data.phone}
                        onChange={(e) => setData('phone', e.target.value)}
                    />
                </FormInput>
                <FormInput
                    label="Notifica via"
                    id="notification_channels"
                    className="my-4 block w-full"
                    error={errors.notification_channels}
                >
                    <NotificationChannelsSelect
                        onChange={(channels) =>
                            setData('notification_channels', channels)
                        }
                        value={data.notification_channels}
                        availableValues={availableNotificationChannels}
                    />
                </FormInput>
            </>
        );
    };

    return (
        <SidebarForm
            title="Modifica contatto"
            trigger={trigger}
            submitMethod={'put'}
            submitRoute={route('admin-contacts.update', {
                contact: contact.id,
            })}
            initialData={initialData}
        >
            <Body />
        </SidebarForm>
    );
}
