import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { type InboxProps } from '@/pages/inbox';
import { type SharedData } from '@/types';
import { useForm, usePage } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { FormEventHandler, useState } from 'react';

export function NewTicketDialog() {
    const { auth, groups, priorities } = usePage<InboxProps & SharedData>().props;
    const [open, setOpen] = useState(false);
    const form = useForm({
        requester_name: '',
        requester_email: '',
        subject: '',
        body: '',
        priority: 'normal',
        group_id: '' as string | number,
    });

    if (!auth.user.permissions.includes('tickets.create')) return null;

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        form.transform((d) => ({ ...d, group_id: d.group_id || null }));
        form.post('/tickets', {
            onSuccess: () => {
                form.reset();
                setOpen(false);
            },
        });
    };

    return (
        <Dialog open={open} onOpenChange={setOpen}>
            <DialogTrigger asChild>
                <Button size="sm" variant="outline">
                    <Plus className="size-4" /> New
                </Button>
            </DialogTrigger>
            <DialogContent className="sm:max-w-lg">
                <form onSubmit={submit} className="space-y-4">
                    <DialogHeader>
                        <DialogTitle>New ticket</DialogTitle>
                        <DialogDescription>Log a request on a customer's behalf, for example from a phone call.</DialogDescription>
                    </DialogHeader>

                    <div className="grid gap-3 sm:grid-cols-2">
                        <div className="space-y-1.5">
                            <Label htmlFor="requester_name">Customer name</Label>
                            <Input id="requester_name" value={form.data.requester_name} onChange={(e) => form.setData('requester_name', e.target.value)} />
                        </div>
                        <div className="space-y-1.5">
                            <Label htmlFor="requester_email">Customer email</Label>
                            <Input
                                id="requester_email"
                                type="email"
                                required
                                value={form.data.requester_email}
                                onChange={(e) => form.setData('requester_email', e.target.value)}
                            />
                            <InputError message={form.errors.requester_email} />
                        </div>
                    </div>

                    <div className="space-y-1.5">
                        <Label htmlFor="subject">Subject</Label>
                        <Input id="subject" required value={form.data.subject} onChange={(e) => form.setData('subject', e.target.value)} />
                        <InputError message={form.errors.subject} />
                    </div>

                    <div className="space-y-1.5">
                        <Label htmlFor="body">What did the customer say?</Label>
                        <Textarea id="body" required value={form.data.body} onChange={(e) => form.setData('body', e.target.value)} />
                        <InputError message={form.errors.body} />
                    </div>

                    <div className="grid gap-3 sm:grid-cols-2">
                        <div className="space-y-1.5">
                            <Label htmlFor="priority">Priority</Label>
                            <select
                                id="priority"
                                className="border-input bg-background h-9 w-full rounded-md border px-2 text-sm capitalize"
                                value={form.data.priority}
                                onChange={(e) => form.setData('priority', e.target.value)}
                            >
                                {priorities.map((p) => (
                                    <option key={p} value={p}>
                                        {p}
                                    </option>
                                ))}
                            </select>
                        </div>
                        <div className="space-y-1.5">
                            <Label htmlFor="group_id">Group</Label>
                            <select
                                id="group_id"
                                className="border-input bg-background h-9 w-full rounded-md border px-2 text-sm"
                                value={form.data.group_id}
                                onChange={(e) => form.setData('group_id', e.target.value)}
                            >
                                <option value="">No group</option>
                                {groups.map((g) => (
                                    <option key={g.id} value={g.id}>
                                        {g.name}
                                    </option>
                                ))}
                            </select>
                        </div>
                    </div>

                    <DialogFooter>
                        <Button type="submit" disabled={form.processing}>
                            Create ticket
                        </Button>
                    </DialogFooter>
                </form>
            </DialogContent>
        </Dialog>
    );
}
