import { Head, Link, useForm } from '@inertiajs/react';
import { RichText } from '@/components/admin/rich-text';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';

export default function TiersCreate() {
    const { data, setData, post, processing, errors } = useForm({
        name: '',
        slug: '',
        description: '',
        price_usd: '',
        duration: 'annual',
        sort_order: 0,
    });

    function handleNameChange(name: string) {
        setData((prev) => ({
            ...prev,
            name,
            slug: prev.slug === '' || prev.slug === slugify(prev.name) ? slugify(name) : prev.slug,
        }));
    }

    function slugify(val: string) {
        return val.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
    }

    function submit(e: React.FormEvent) {
        e.preventDefault();
        post('/admin/tiers');
    }

    return (
        <>
            <Head title="Add Membership Tier" />
            <form onSubmit={submit} className="space-y-0">
                <div className="sticky top-16 z-10 -mx-6 mb-6 flex items-center justify-between border-b bg-background px-6 pb-4 pt-2 lg:-mx-8 lg:px-8">
                    <div>
                        <div className="flex items-center gap-2 text-sm text-muted-foreground">
                            <Link href="/admin/tiers">Tiers</Link>
                            <span>/</span>
                            <span className="text-foreground">Add Membership Tier</span>
                        </div>
                        <h1 className="text-2xl font-bold">Add Membership Tier</h1>
                    </div>
                    <div className="flex items-center gap-3">
                        <Button type="button" variant="outline" asChild>
                            <Link href="/admin/tiers">Cancel</Link>
                        </Button>
                        <Button type="submit" disabled={processing}>{processing ? 'Saving...' : 'Create Tier'}</Button>
                    </div>
                </div>

                <div className="grid grid-cols-1 gap-6 lg:grid-cols-[1fr_320px]">
                    <div className="space-y-6">
                        <div className="rounded-xl border border-border bg-card p-6 space-y-4">
                            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                                <div className="space-y-1.5">
                                    <Label htmlFor="name">Name <span className="text-destructive">*</span></Label>
                                    <Input id="name" value={data.name} onChange={(e) => handleNameChange(e.target.value)} placeholder="e.g. Individual Annual" />
                                    {errors.name && <p className="text-sm text-destructive">{errors.name}</p>}
                                </div>
                                <div className="space-y-1.5">
                                    <Label htmlFor="slug">Slug <span className="text-destructive">*</span></Label>
                                    <Input id="slug" value={data.slug} onChange={(e) => setData('slug', slugify(e.target.value))} placeholder="individual-annual" />
                                    {errors.slug && <p className="text-sm text-destructive">{errors.slug}</p>}
                                </div>
                            </div>
                            <div className="space-y-1.5">
                                <Label>Description</Label>
                                <RichText value={data.description} onChange={(v) => setData('description', v)} height={200} />
                                {errors.description && <p className="text-sm text-destructive">{errors.description}</p>}
                            </div>
                        </div>
                    </div>

                    <div className="space-y-6">
                        <div className="rounded-xl border border-border bg-card p-6 space-y-4">
                            <div className="space-y-1.5">
                                <Label htmlFor="price_usd">Price (USD) <span className="text-destructive">*</span></Label>
                                <Input id="price_usd" type="number" min={0} step="0.01" value={data.price_usd} onChange={(e) => setData('price_usd', e.target.value)} placeholder="0.00" />
                                {errors.price_usd && <p className="text-sm text-destructive">{errors.price_usd}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="duration">Duration <span className="text-destructive">*</span></Label>
                                <Select value={data.duration} onValueChange={(v) => setData('duration', v)}>
                                    <SelectTrigger id="duration"><SelectValue /></SelectTrigger>
                                    <SelectContent>
                                        <SelectItem value="annual">Annual</SelectItem>
                                        <SelectItem value="lifetime">Lifetime</SelectItem>
                                        <SelectItem value="free">Free</SelectItem>
                                    </SelectContent>
                                </Select>
                                {errors.duration && <p className="text-sm text-destructive">{errors.duration}</p>}
                            </div>
                            <div className="space-y-1.5">
                                <Label htmlFor="sort_order">Sort Order</Label>
                                <Input id="sort_order" type="number" min={0} value={data.sort_order} onChange={(e) => setData('sort_order', Number(e.target.value))} />
                                {errors.sort_order && <p className="text-sm text-destructive">{errors.sort_order}</p>}
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </>
    );
}
