import { Head, useForm } from '@inertiajs/react';
import { FormEventHandler } from 'react';

import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import AdminLayout from '@/layouts/admin-layout';

export default function AdminCouponCreate() {
    const { data, setData, post, processing, errors } = useForm({
        code: '', type: 'fixed', value: '', min_order_amount: '0',
        max_discount: '', usage_limit: '', start_at: '', end_at: '', status: true,
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('admin.coupons.store'));
    };

    return (
        <AdminLayout title="Add Coupon">
            <Head title="Add Coupon" />
            <form onSubmit={submit} className="max-w-xl space-y-4">
                <Card>
                    <CardHeader><CardTitle className="text-base">Coupon Details</CardTitle></CardHeader>
                    <CardContent className="space-y-4">
                        <div className="grid gap-4 sm:grid-cols-2">
                            <div className="space-y-1.5">
                                <Label>Code *</Label>
                                <Input value={data.code} onChange={(e) => setData('code', e.target.value.toUpperCase())} placeholder="EG: SAVE20" className="uppercase" />
                                <InputError message={errors.code} />
                            </div>
                            <div className="space-y-1.5">
                                <Label>Type *</Label>
                                <Select value={data.type} onValueChange={(v) => setData('type', v)}>
                                    <SelectTrigger aria-label="Coupon type"><SelectValue /></SelectTrigger>
                                    <SelectContent>
                                        <SelectItem value="fixed">Fixed (৳)</SelectItem>
                                        <SelectItem value="percent">Percent (%)</SelectItem>
                                    </SelectContent>
                                </Select>
                            </div>
                        </div>
                        <div className="grid gap-4 sm:grid-cols-2">
                            <div className="space-y-1.5">
                                <Label>Discount Value *</Label>
                                <Input type="number" step="0.01" value={data.value} onChange={(e) => setData('value', e.target.value)} />
                                <InputError message={errors.value} />
                            </div>
                            <div className="space-y-1.5">
                                <Label>Min Order Amount (৳)</Label>
                                <Input type="number" step="0.01" value={data.min_order_amount} onChange={(e) => setData('min_order_amount', e.target.value)} />
                            </div>
                            <div className="space-y-1.5">
                                <Label>Max Discount (৳)</Label>
                                <Input type="number" step="0.01" value={data.max_discount} onChange={(e) => setData('max_discount', e.target.value)} placeholder="Unlimited" />
                            </div>
                            <div className="space-y-1.5">
                                <Label>Usage Limit</Label>
                                <Input type="number" value={data.usage_limit} onChange={(e) => setData('usage_limit', e.target.value)} placeholder="Unlimited" />
                            </div>
                            <div className="space-y-1.5">
                                <Label>Start Date</Label>
                                <Input type="datetime-local" value={data.start_at} onChange={(e) => setData('start_at', e.target.value)} />
                            </div>
                            <div className="space-y-1.5">
                                <Label>End Date</Label>
                                <Input type="datetime-local" value={data.end_at} onChange={(e) => setData('end_at', e.target.value)} />
                            </div>
                        </div>
                        <div className="flex items-center gap-2">
                            <Checkbox id="status" checked={data.status} onCheckedChange={(v) => setData('status', Boolean(v))} />
                            <Label htmlFor="status">Active</Label>
                        </div>
                    </CardContent>
                </Card>
                <Button type="submit" disabled={processing} className="bg-indigo-600 hover:bg-indigo-700">
                    {processing ? 'Saving…' : 'Create Coupon'}
                </Button>
            </form>
        </AdminLayout>
    );
}
