import React from 'react';
import { useForm } from '@inertiajs/react';
import { AdminLayout } from '../AdminLayout';
import { Download, Upload } from 'lucide-react';

export default function Index() {
    const { data, setData, post, processing, errors } = useForm({
        module: 'services',
        file: null as File | null,
    });

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        if (!data.file) return;
        post('/admin/content-import', {
            preserveScroll: true,
        });
    };

    return (
        <AdminLayout title="Content JSON/CSV Importer">
            <div className="w-full space-y-6">
                <form onSubmit={handleSubmit} className="glass-card rounded-2xl p-6 sm:p-8 space-y-6">
                    <div>
                        <h3 className="text-base font-bold text-white flex items-center gap-2">
                            <Download className="w-5 h-5 text-[#00F2FE]" /> Import CMS Records
                        </h3>
                        <p className="text-xs text-slate-400">Batch import services, projects, team profiles, FAQs, or blog posts via JSON.</p>
                    </div>

                    <div>
                        <label className="text-xs font-bold text-slate-300 uppercase block mb-2">Target Module *</label>
                        <select
                            value={data.module}
                            onChange={(e) => setData('module', e.target.value)}
                            className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 text-sm text-white focus:outline-none focus:border-[#00F2FE]"
                        >
                            <option value="services">Services</option>
                            <option value="projects">Projects / Case Studies</option>
                            <option value="team">Team Members</option>
                            <option value="blog">Blog Articles</option>
                            <option value="faqs">FAQs</option>
                            <option value="pages">CMS Pages</option>
                        </select>
                    </div>

                    <div>
                        <label className="text-xs font-bold text-slate-300 uppercase block mb-2">Select JSON Data File *</label>
                        <input
                            type="file"
                            accept=".json,.csv"
                            onChange={(e) => setData('file', e.target.files ? e.target.files[0] : null)}
                            required
                            className="w-full bg-[#060B18] border border-slate-800 rounded-xl px-4 py-2.5 text-xs text-slate-300 focus:outline-none focus:border-[#00F2FE]"
                        />
                        {errors.file && <span className="text-xs text-rose-400 mt-1 block">{errors.file}</span>}
                    </div>

                    <button
                        type="submit"
                        disabled={processing || !data.file}
                        className="btn-aqua px-6 py-3 rounded-xl font-bold text-sm flex items-center gap-2"
                    >
                        <Upload className="w-4 h-4" /> Run Batch Import
                    </button>
                </form>
            </div>
        </AdminLayout>
    );
}
