/* eslint-disable react-hooks/exhaustive-deps */
'use client';

import EditEditor from '@/app/admin/(dashboard)/_com/editEditor';
import artikelKesehatanState from '@/app/admin/(dashboard)/_state/kesehatan/data_kesehatan_warga/artikelKesehatan';
import colors from '@/con/colors';
import ApiFetch from '@/lib/api-fetch';
import {
  Box,
  Button,
  Group,
  Image,
  Paper,
  Stack,
  Text,
  TextInput,
  Title
} from '@mantine/core';
import { Dropzone } from '@mantine/dropzone';
import { IconArrowBack, IconPhoto, IconUpload, IconX } from '@tabler/icons-react';
import { useParams, useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import { useProxy } from 'valtio/utils';

interface ArtikelKesehatanFormBase {
  title: string;
  content: string;
  imageId: string;
  introduction: { content: string };
  symptom: { title: string; content: string };
  prevention: { title: string; content: string };
  firstAid: { title: string; content: string };
  mythVsFact: { title: string; mitos: string; fakta: string };
  doctorSign: { content: string };
}

function EditArtikelKesehatan() {
  const stateArtikelKesehatan = useProxy(artikelKesehatanState);
  const router = useRouter();
  const params = useParams();

  const [previewImage, setPreviewImage] = useState<string | null>(null);
  const [file, setFile] = useState<File | null>(null);
  const [formData, setFormData] = useState<ArtikelKesehatanFormBase>({
    title: '',
    content: '',
    imageId: '',
    introduction: { content: '' },
    symptom: { title: '', content: '' },
    prevention: { title: '', content: '' },
    firstAid: { title: '', content: '' },
    mythVsFact: { title: '', mitos: '', fakta: '' },
    doctorSign: { content: '' },
  });

  // Load data artikel
  useEffect(() => {
    const loadArtikelKesehatan = async () => {
      const id = params?.id as string;
      if (!id) return;

      try {
        await stateArtikelKesehatan.edit.load(id);
        const { form } = stateArtikelKesehatan.edit;
        if (!form) return;

        setFormData({
          title: form.title || '',
          content: form.content || '',
          imageId: form.imageId || '',
          introduction: { content: form.introduction?.content || '' },
          symptom: { title: form.symptom?.title || '', content: form.symptom?.content || '' },
          prevention: { title: form.prevention?.title || '', content: form.prevention?.content || '' },
          firstAid: { title: form.firstAid?.title || '', content: form.firstAid?.content || '' },
          mythVsFact: {
            title: form.mythVsFact?.title || '',
            mitos: form.mythVsFact?.mitos || '',
            fakta: form.mythVsFact?.fakta || '',
          },
          doctorSign: { content: form.doctorSign?.content || '' },
        });

        if (form.imageId) {
          setPreviewImage(`${process.env.NEXT_PUBLIC_API_URL}/file/${form.imageId}`);
        }
      } catch (error) {
        console.error('Error loading artikel kesehatan:', error);
        toast.error('Gagal memuat data artikel kesehatan');
      }
    };

    loadArtikelKesehatan();
  }, [params?.id]);

  const handleFileChange = (files: File[]) => {
    const selectedFile = files[0];
    if (!selectedFile) return;
    setFile(selectedFile);
    setPreviewImage(URL.createObjectURL(selectedFile));
  };

  const handleSubmit = async () => {
    try {
      // Copy formData ke global state
      stateArtikelKesehatan.edit.form = { ...formData };

      // Upload gambar kalau ada
      if (file) {
        const res = await ApiFetch.api.fileStorage.create.post({ file, name: file.name });
        const uploaded = res.data?.data;
        if (!uploaded?.id) return toast.error('Gagal upload gambar');
        stateArtikelKesehatan.edit.form.imageId = uploaded.id;
      }

      const success = await stateArtikelKesehatan.edit.submit();
      if (success) {
        toast.success('Artikel kesehatan berhasil diperbarui!');
        router.push('/admin/kesehatan/data-kesehatan-warga/artikel_kesehatan');
      }
    } catch (error) {
      console.error('Error updating artikel kesehatan:', error);
      toast.error(error instanceof Error ? error.message : 'Gagal memperbarui data artikel kesehatan');
    }
  };

  const InputText = ({
    label,
    value,
    onChange,
    placeholder,
    required,
  }: {
    label: string;
    value: string;
    onChange: (v: string) => void;
    placeholder?: string;
    required?: boolean;
  }) => (
    <TextInput
      label={label}
      value={value}
      placeholder={placeholder}
      onChange={(e) => onChange(e.target.value)}
      required={required}
    />
  );

  return (
    <Box px={{ base: 0, md: 'lg' }} py="xs">
      {/* Header */}
      <Group mb="md">
        <Button variant="subtle" onClick={() => router.back()} p="xs" radius="md">
          <IconArrowBack color={colors['blue-button']} size={24} />
        </Button>
        <Title order={4} ml="sm" c="dark">
          Edit Artikel Kesehatan
        </Title>
      </Group>

      {/* Form */}
      <Paper
        w={{ base: '100%', md: '50%' }}
        bg={colors['white-1']}
        p="lg"
        radius="md"
        shadow="sm"
        style={{ border: '1px solid #e0e0e0' }}
      >
        <Stack gap="md">
          {/* Judul */}
          <InputText
            label="Judul"
            value={formData.title}
            onChange={(value) => setFormData((prev) => ({ ...prev, title: value }))}
            placeholder="Masukkan judul artikel"
            required
          />

          {/* Gambar */}
          <Box>
            <Text fw="bold" fz="sm" mb={6}>
              Gambar Artikel Kesehatan
            </Text>
            <Dropzone
              onDrop={handleFileChange}
              onReject={() => toast.error('File tidak valid, gunakan format gambar')}
              maxSize={5 * 1024 ** 2}
              accept={{ 'image/*': [] }}
              radius="md"
              p="xl"
            >
              <Group justify="center" gap="xl" mih={180}>
                <Dropzone.Accept>
                  <IconUpload size={48} color={colors['blue-button']} stroke={1.5} />
                </Dropzone.Accept>
                <Dropzone.Reject>
                  <IconX size={48} color="red" stroke={1.5} />
                </Dropzone.Reject>
                <Dropzone.Idle>
                  <IconPhoto size={48} color="#868e96" stroke={1.5} />
                </Dropzone.Idle>
                <Stack gap="xs" align="center">
                  <Text size="md" fw={500}>
                    Seret gambar atau klik untuk memilih file
                  </Text>
                  <Text size="sm" c="dimmed">
                    Maksimal 5MB, format gambar wajib
                  </Text>
                </Stack>
              </Group>
            </Dropzone>
            {previewImage && (
              <Box mt="sm" style={{ display: 'flex', justifyContent: 'center' }}>
                <Image
                  src={previewImage}
                  alt="Preview Gambar"
                  radius="md"
                  style={{
                    maxHeight: 220,
                    objectFit: 'contain',
                    border: `1px solid ${colors['blue-button']}`,
                  }}
                  loading="lazy"
                />
              </Box>
            )}
          </Box>

          {/* Konten */}
          <InputText
            label="Deskripsi"
            value={formData.content}
            onChange={(value) => setFormData((prev) => ({ ...prev, content: value }))}
            placeholder="Masukkan deskripsi artikel"
            required
          />

          {/* Pendahuluan */}
          <Box>
            <Text fw="bold">Pendahuluan</Text>
            <EditEditor
              value={formData.introduction.content}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, introduction: { ...prev.introduction, content: value } }))
              }
            />
          </Box>
          {/* Gejala */}
          <Box>
            <Text fw="bold">Gejala</Text>
            <Stack gap="xs">
              <InputText
                label="Judul Gejala"
                value={formData.symptom.title}
                onChange={(value) =>
                  setFormData((prev) => ({ ...prev, symptom: { ...prev.symptom, title: value } }))
                }
              />
              <EditEditor
                value={formData.symptom.content}
                onChange={(value) =>
                  setFormData((prev) => ({ ...prev, symptom: { ...prev.symptom, content: value } }))
                }
              />
            </Stack>
          </Box>

          {/* Pencegahan */}
          <Box>
            <Text fw="bold">Pencegahan</Text>
            <InputText
              label="Judul"
              value={formData.prevention.title}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, prevention: { ...prev.prevention, title: value } }))
              }
            />
            <EditEditor
              value={formData.prevention.content}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, prevention: { ...prev.prevention, content: value } }))
              }
            />
          </Box>

          {/* Pertolongan Pertama */}
          <Box>
            <Text fw="bold">Pertolongan Pertama</Text>
            <InputText
              label="Judul"
              value={formData.firstAid.title}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, firstAid: { ...prev.firstAid, title: value } }))
              }
            />
            <EditEditor
              value={formData.firstAid.content}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, firstAid: { ...prev.firstAid, content: value } }))
              }
            />
          </Box>

          {/* Mitos vs Fakta */}
          <Box>
            <Text fw="bold">Mitos vs Fakta</Text>
            <InputText
              label="Judul"
              value={formData.mythVsFact.title}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, mythVsFact: { ...prev.mythVsFact, title: value } }))
              }
            />
            <Text fw="500">Mitos</Text>
            <EditEditor
              value={formData.mythVsFact.mitos}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, mythVsFact: { ...prev.mythVsFact, mitos: value } }))
              }
            />
            <Text fw="500">Fakta</Text>
            <EditEditor
              value={formData.mythVsFact.fakta}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, mythVsFact: { ...prev.mythVsFact, fakta: value } }))
              }
            />
          </Box>

          {/* Dokter */}
          <Box>
            <Text fw="bold">Kapan Harus Ke Dokter</Text>
            <EditEditor
              value={formData.doctorSign.content}
              onChange={(value) =>
                setFormData((prev) => ({ ...prev, doctorSign: { content: value } }))
              }
            />
          </Box>

          {/* Save button */}
          <Group justify="right">
            <Button
              onClick={handleSubmit}
              radius="md"
              size="md"
              style={{
                background: `linear-gradient(135deg, ${colors['blue-button']}, #4facfe)`,
                color: '#fff',
                boxShadow: '0 4px 15px rgba(79, 172, 254, 0.4)',
              }}
            >
              Simpan
            </Button>
          </Group>
        </Stack>
      </Paper>
    </Box>
  );
}

export default EditArtikelKesehatan;
