'use client';

import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { motion } from 'framer-motion';
import { useTranslation } from 'react-i18next';
import { ShieldAlert, LogOut, MessageSquare, Send, Loader2 } from 'lucide-react';
import toast, { Toaster } from 'react-hot-toast';
import AuthService from '../services/AuthService';
import { sendGeneralReport } from '../utils/api';

export default function BlockedPage() {
  const { t } = useTranslation();
  const router = useRouter();
  const [form, setForm] = useState({ subject: '', body: '' });
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [isSent, setIsSent] = useState(false);

  const handleLogout = () => {
    AuthService.deleteToken();
    router.replace('/');
  };

  const updateField = (field, value) => {
    setForm((current) => ({ ...current, [field]: value }));
    if (isSent) setIsSent(false);
  };

  const handleSubmit = async (event) => {
    event.preventDefault();

    if (!form.subject.trim() || !form.body.trim()) {
      toast.error(t('blocked.fillAllFields'));
      return;
    }

    setIsSubmitting(true);

    try {
      await sendGeneralReport(form.subject.trim(), form.body.trim());
      setIsSent(true);
      setForm({ subject: '', body: '' });
      toast.success(t('blocked.reportSent'));
    } catch (error) {
      toast.error(t('blocked.sendError'));
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <div className="min-h-screen bg-gradient-to-b from-red-50 via-white to-amber-50 flex items-center justify-center p-4" dir="rtl">
      <Toaster position="top-center" reverseOrder={false} />

      <motion.div
        initial={{ opacity: 0, y: 24 }}
        animate={{ opacity: 1, y: 0 }}
        className="w-full max-w-5xl"
      >
        <div className="text-center mb-10">
          <motion.div
            initial={{ scale: 0.9 }}
            animate={{ scale: 1 }}
            className="w-24 h-24 bg-red-100 rounded-3xl flex items-center justify-center mx-auto mb-6 shadow-lg shadow-red-100"
          >
            <ShieldAlert className="w-12 h-12 text-red-600" />
          </motion.div>
          <h1 className="text-4xl font-bold text-gray-900 mb-3">{t('blocked.title')}</h1>
          <p className="text-gray-600 text-lg max-w-2xl mx-auto">
            {t('blocked.description')}
          </p>
        </div>

        <div className="grid md:grid-cols-2 gap-6">
          <motion.div
            initial={{ opacity: 0, x: -24 }}
            animate={{ opacity: 1, x: 0 }}
            transition={{ delay: 0.1 }}
            className="bg-white rounded-3xl shadow-sm border border-gray-200 p-8 flex flex-col justify-between"
          >
            <div>
              <div className="w-14 h-14 bg-red-50 rounded-2xl flex items-center justify-center mb-6">
                <LogOut className="w-7 h-7 text-red-600" />
              </div>
              <h2 className="text-2xl font-bold text-gray-900 mb-3">{t('blocked.logoutTitle')}</h2>
              <p className="text-gray-600 leading-7">
                {t('blocked.logoutDesc')}
              </p>
            </div>

            <button
              type="button"
              onClick={handleLogout}
              className="mt-8 w-full bg-red-600 hover:bg-red-700 text-white rounded-2xl py-4 font-bold transition-colors flex items-center justify-center gap-3"
            >
              <LogOut className="w-5 h-5" />
              {t('blocked.logoutButton')}
            </button>
          </motion.div>

          <motion.div
            initial={{ opacity: 0, x: 24 }}
            animate={{ opacity: 1, y: 0 }}
            transition={{ delay: 0.2 }}
            className="bg-white rounded-3xl shadow-sm border border-gray-200 p-8"
          >
            <div className="flex items-center gap-4 mb-6">
              <div className="w-14 h-14 bg-amber-50 rounded-2xl flex items-center justify-center">
                <MessageSquare className="w-7 h-7 text-amber-600" />
              </div>
              <div>
                <h2 className="text-2xl font-bold text-gray-900">{t('blocked.contactSupportTitle')}</h2>
                <p className="text-gray-600 mt-1">{t('blocked.contactSupportDesc')}</p>
              </div>
            </div>

            <form onSubmit={handleSubmit} className="space-y-5">
              <div>
                <label className="block text-sm font-bold text-gray-700 mb-2">{t('subject')}</label>
                <input
                  type="text"
                  value={form.subject}
                  onChange={(event) => updateField('subject', event.target.value)}
                  placeholder={t('blocked.subjectPlaceholder')}
                  className="w-full px-4 py-3 bg-white border border-gray-200 rounded-2xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent text-gray-900 placeholder-gray-400"
                />
              </div>

              <div>
                <label className="block text-sm font-bold text-gray-700 mb-2">{t('blocked.messageLabel')}</label>
                <textarea
                  value={form.body}
                  onChange={(event) => updateField('body', event.target.value)}
                  rows={6}
                  placeholder={t('blocked.messagePlaceholder')}
                  className="w-full px-4 py-3 bg-white border border-gray-200 rounded-2xl focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent text-gray-900 placeholder-gray-400 resize-none"
                />
              </div>

              <button
                type="submit"
                disabled={isSubmitting}
                className="w-full bg-amber-500 hover:bg-amber-600 text-white rounded-2xl py-4 font-bold transition-colors flex items-center justify-center gap-3 disabled:opacity-50 disabled:cursor-not-allowed"
              >
                {isSubmitting ? (
                  <>
                    <Loader2 className="w-5 h-5 animate-spin" />
                    {t('blocked.sending')}
                  </>
                ) : isSent ? (
                  <>
                    <Send className="w-5 h-5" />
                    {t('blocked.sentButton')}
                  </>
                ) : (
                  <>
                    <Send className="w-5 h-5" />
                    {t('blocked.sendButton')}
                  </>
                )}
              </button>
            </form>
          </motion.div>
        </div>
      </motion.div>
    </div>
  );
}