import { useState, useRef, useEffect, useCallback } from 'react';
import { router } from '@inertiajs/react';
import { AppLayout } from '@/Components/Layout/AppLayout';

type ChatMessage = { id: number; body: string; sender: string; role?: string; isOwn: boolean; createdAt: string };
type Thread = {
  id: number;
  subject: string;
  unread: number;
  lastBody: string;
  lastSender: string;
  lastAt: string;
  lastIsOwn: boolean;
  messages: ChatMessage[];
};
type Contact = { id: number; name: string; role: string };

type Props = {
  threads: Thread[];
  contacts: Contact[];
};

function csrf(): string {
  return (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement)?.content ?? '';
}

export default function StudentMessages({ threads: initialThreads, contacts }: Props) {
  const [threads, setThreads] = useState<Thread[]>(initialThreads);
  const [activeId, setActiveId] = useState<number | null>(threads[0]?.id ?? null);
  const [composing, setComposing] = useState(false);
  const [replyBody, setReplyBody] = useState('');
  const [sending, setSending] = useState(false);

  const [recipientIds, setRecipientIds] = useState<number[]>([]);
  const [subject, setSubject] = useState('');
  const [composeBody, setComposeBody] = useState('');
  const [composeProcessing, setComposeProcessing] = useState(false);
  const [searchContact, setSearchContact] = useState('');

  const chatEndRef = useRef<HTMLDivElement>(null);
  const replyRef = useRef<HTMLTextAreaElement>(null);

  const activeThread = threads.find(t => t.id === activeId) ?? null;
  const totalUnread = threads.reduce((s, t) => s + t.unread, 0);

  useEffect(() => {
    chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [activeId, activeThread?.messages.length]);

  const selectThread = useCallback((t: Thread) => {
    setActiveId(t.id);
    setComposing(false);
    if (t.unread > 0) {
      const unreadIds = t.messages.filter(m => !m.isOwn).map(m => m.id);
      unreadIds.forEach(id => {
        fetch(`/student/messages/${id}/read`, { method: 'PATCH', headers: { 'X-CSRF-TOKEN': csrf() } });
      });
      setThreads(prev => prev.map(x => x.id === t.id ? { ...x, unread: 0 } : x));
    }
    setTimeout(() => replyRef.current?.focus(), 100);
  }, []);

  const sendReply = useCallback(() => {
    if (!replyBody.trim() || !activeId || sending) return;
    setSending(true);
    router.post(
      `/student/messages/${activeId}/reply`,
      { body: replyBody },
      {
        preserveScroll: true,
        onSuccess: () => { setReplyBody(''); setSending(false); },
        onError: () => setSending(false),
      }
    );
  }, [replyBody, activeId, sending]);

  const sendCompose = useCallback((e: React.FormEvent) => {
    e.preventDefault();
    if (!composeBody.trim() || recipientIds.length === 0 || composeProcessing) return;
    setComposeProcessing(true);
    router.post(
      '/student/messages',
      { recipient_ids: recipientIds, subject, body: composeBody },
      {
        onSuccess: () => {
          setComposing(false);
          setRecipientIds([]);
          setSubject('');
          setComposeBody('');
          setComposeProcessing(false);
        },
        onError: () => setComposeProcessing(false),
      }
    );
  }, [composeBody, recipientIds, subject, composeProcessing]);

  const toggleContact = (id: number) => {
    setRecipientIds(prev => prev.includes(id) ? prev.filter(r => r !== id) : [...prev, id]);
  };

  const filteredContacts = searchContact.trim()
    ? contacts.filter(c => c.name.includes(searchContact) || c.role.includes(searchContact))
    : contacts;

  const handleReplyKey = (e: React.KeyboardEvent) => {
    if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) sendReply();
  };

  return (
    <AppLayout title="پیام‌رسانی">
      <div style={{ display: 'flex', height: 'calc(100vh - 120px)', minHeight: 520, gap: 0, borderRadius: 'var(--radius-lg)', overflow: 'hidden', border: '1px solid var(--color-border)', background: 'var(--color-surface)' }}>

        {/* ── Left: Thread list ── */}
        <div style={{ width: 300, minWidth: 260, borderLeft: '1px solid var(--color-border)', display: 'flex', flexDirection: 'column', background: 'var(--color-bg-soft)' }}>
          <div style={{ padding: '0.85rem 1rem', borderBottom: '1px solid var(--color-border)', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
            <span style={{ fontWeight: 700, fontSize: '0.97rem', flex: 1 }}>
              <i className="bi bi-chat-dots-fill" style={{ color: 'var(--color-primary)', marginLeft: '0.4rem' }} />
              پیام‌ها
              {totalUnread > 0 && (
                <span style={{ background: '#EF4444', color: '#fff', borderRadius: 999, fontSize: '0.68rem', padding: '0.06rem 0.4rem', marginRight: '0.4rem' }}>
                  {totalUnread}
                </span>
              )}
            </span>
            {contacts.length > 0 && (
              <button
                className="btn btn-primary btn-sm"
                onClick={() => { setComposing(true); setActiveId(null); }}
                title="پیام جدید"
              >
                <i className="bi bi-pencil-square" />
              </button>
            )}
          </div>

          <div style={{ flex: 1, overflowY: 'auto' }}>
            {threads.length === 0 ? (
              <div style={{ padding: '2rem 1rem', textAlign: 'center', color: 'var(--color-text-muted)', fontSize: '0.85rem' }}>
                <i className="bi bi-inbox" style={{ fontSize: '2rem', display: 'block', marginBottom: '0.5rem', opacity: 0.4 }} />
                هیچ پیامی وجود ندارد
              </div>
            ) : (
              threads.map(t => (
                <button
                  key={t.id}
                  onClick={() => selectThread(t)}
                  style={{
                    width: '100%',
                    textAlign: 'right',
                    padding: '0.75rem 1rem',
                    background: activeId === t.id ? 'var(--color-primary-soft)' : 'transparent',
                    cursor: 'pointer',
                    border: 'none',
                    borderBottom: '1px solid var(--color-border)',
                    borderRight: activeId === t.id ? '3px solid var(--color-primary)' : '3px solid transparent',
                    display: 'block',
                    fontFamily: 'inherit',
                    transition: 'background 0.15s',
                  }}
                >
                  <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', gap: '0.3rem' }}>
                    <span style={{ fontWeight: t.unread > 0 ? 700 : 500, fontSize: '0.875rem', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: 1, color: 'var(--color-text)' }}>
                      {t.subject}
                    </span>
                    <span style={{ fontSize: '0.68rem', color: 'var(--color-text-muted)', flexShrink: 0, marginTop: '0.1rem' }}>{t.lastAt}</span>
                  </div>
                  <div style={{ display: 'flex', alignItems: 'center', gap: '0.3rem', marginTop: '0.18rem' }}>
                    {t.lastIsOwn && <i className="bi bi-arrow-return-left" style={{ fontSize: '0.7rem', color: 'var(--color-text-muted)', flexShrink: 0 }} />}
                    <span style={{ fontSize: '0.78rem', color: 'var(--color-text-muted)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: 1 }}>
                      {t.lastIsOwn ? 'شما: ' : `${t.lastSender}: `}{t.lastBody}
                    </span>
                    {t.unread > 0 && (
                      <span style={{ background: 'var(--color-primary)', color: '#fff', borderRadius: 999, fontSize: '0.65rem', padding: '0.05rem 0.38rem', flexShrink: 0 }}>
                        {t.unread}
                      </span>
                    )}
                  </div>
                </button>
              ))
            )}
          </div>
        </div>

        {/* ── Right: Chat view ── */}
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0 }}>
          {composing ? (
            <form onSubmit={sendCompose} style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
              <div style={{ padding: '0.85rem 1.2rem', borderBottom: '1px solid var(--color-border)', display: 'flex', alignItems: 'center', gap: '0.75rem' }}>
                <button type="button" className="btn btn-ghost btn-sm" onClick={() => setComposing(false)}>
                  <i className="bi bi-arrow-right" />
                </button>
                <strong style={{ fontSize: '0.97rem' }}>پیام جدید</strong>
              </div>
              <div style={{ flex: 1, padding: '1.2rem', overflowY: 'auto', display: 'flex', flexDirection: 'column', gap: '1rem' }}>
                <div>
                  <label style={{ fontSize: '0.82rem', fontWeight: 600, color: 'var(--color-text-muted)', display: 'block', marginBottom: '0.3rem' }}>موضوع (اختیاری)</label>
                  <input
                    className="form-input"
                    value={subject}
                    onChange={e => setSubject(e.target.value)}
                    placeholder="موضوع پیام..."
                  />
                </div>
                <div>
                  <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '0.3rem' }}>
                    <label style={{ fontSize: '0.82rem', fontWeight: 600, color: 'var(--color-text-muted)' }}>
                      گیرنده
                      {recipientIds.length > 0 && <span style={{ marginRight: '0.4rem', color: 'var(--color-primary)', fontWeight: 700 }}>{recipientIds.length} نفر</span>}
                    </label>
                    {recipientIds.length > 0 && (
                      <button type="button" style={{ background: 'none', border: 'none', color: 'var(--color-danger)', fontSize: '0.78rem', cursor: 'pointer' }} onClick={() => setRecipientIds([])}>
                        پاک کردن
                      </button>
                    )}
                  </div>
                  <input
                    className="form-input"
                    value={searchContact}
                    onChange={e => setSearchContact(e.target.value)}
                    placeholder="جستجو..."
                    style={{ marginBottom: '0.5rem' }}
                  />
                  <div style={{ display: 'flex', flexDirection: 'column', gap: '0.3rem', maxHeight: 200, overflowY: 'auto' }}>
                    {filteredContacts.length === 0 ? (
                      <span style={{ fontSize: '0.82rem', color: 'var(--color-text-muted)', padding: '0.5rem' }}>کاربری یافت نشد</span>
                    ) : filteredContacts.map(c => {
                      const sel = recipientIds.includes(c.id);
                      return (
                        <label key={c.id} style={{
                          display: 'flex', alignItems: 'center', gap: '0.5rem',
                          background: sel ? 'var(--color-primary-soft)' : 'var(--color-surface)',
                          border: `1px solid ${sel ? 'var(--color-primary)' : 'var(--color-border)'}`,
                          borderRadius: 'var(--radius-sm)', padding: '0.4rem 0.7rem',
                          cursor: 'pointer', fontSize: '0.875rem',
                        }}>
                          <input type="checkbox" checked={sel} onChange={() => toggleContact(c.id)} style={{ width: 15, height: 15, accentColor: 'var(--color-primary)' }} />
                          <span style={{ flex: 1 }}>{c.name}</span>
                          <span style={{ fontSize: '0.72rem', color: 'var(--color-text-muted)', background: 'var(--color-bg-soft)', borderRadius: 4, padding: '0.1rem 0.4rem' }}>{c.role}</span>
                        </label>
                      );
                    })}
                  </div>
                </div>
                <div style={{ flex: 1 }}>
                  <label style={{ fontSize: '0.82rem', fontWeight: 600, color: 'var(--color-text-muted)', display: 'block', marginBottom: '0.3rem' }}>متن پیام</label>
                  <textarea
                    className="form-input"
                    rows={5}
                    value={composeBody}
                    onChange={e => setComposeBody(e.target.value)}
                    placeholder="پیام خود را بنویسید..."
                    required
                    style={{ resize: 'vertical', minHeight: 100 }}
                  />
                </div>
              </div>
              <div style={{ padding: '0.85rem 1.2rem', borderTop: '1px solid var(--color-border)', display: 'flex', gap: '0.5rem' }}>
                <button className="btn btn-primary" type="submit" disabled={composeProcessing || recipientIds.length === 0 || !composeBody.trim()}>
                  <i className="bi bi-send-fill" /> {composeProcessing ? 'در حال ارسال...' : 'ارسال'}
                </button>
                <button className="btn btn-ghost" type="button" onClick={() => setComposing(false)}>لغو</button>
              </div>
            </form>
          ) : activeThread ? (
            <>
              <div style={{ padding: '0.85rem 1.2rem', borderBottom: '1px solid var(--color-border)', display: 'flex', alignItems: 'center', gap: '0.75rem' }}>
                <i className="bi bi-chat-text" style={{ color: 'var(--color-primary)', fontSize: '1.1rem' }} />
                <div style={{ flex: 1 }}>
                  <div style={{ fontWeight: 700, fontSize: '0.97rem' }}>{activeThread.subject}</div>
                  <div style={{ fontSize: '0.75rem', color: 'var(--color-text-muted)' }}>{activeThread.messages.length} پیام</div>
                </div>
              </div>

              <div style={{ flex: 1, overflowY: 'auto', padding: '1rem 1.2rem', display: 'flex', flexDirection: 'column', gap: '0.65rem' }}>
                {activeThread.messages.map((msg, idx) => {
                  const prevMsg = idx > 0 ? activeThread.messages[idx - 1] : null;
                  const showSender = !prevMsg || prevMsg.isOwn !== msg.isOwn;
                  return (
                    <div key={msg.id} style={{ display: 'flex', justifyContent: msg.isOwn ? 'flex-end' : 'flex-start' }}>
                      <div style={{ maxWidth: '72%', display: 'flex', flexDirection: 'column', gap: '0.2rem', alignItems: msg.isOwn ? 'flex-end' : 'flex-start' }}>
                        {showSender && !msg.isOwn && (
                          <div style={{ display: 'flex', alignItems: 'center', gap: '0.35rem', paddingRight: '0.5rem' }}>
                            <span style={{ fontSize: '0.72rem', color: 'var(--color-text-muted)' }}>{msg.sender}</span>
                            {msg.role && (
                              <span style={{ fontSize: '0.65rem', background: 'var(--color-primary-soft)', color: 'var(--color-primary)', borderRadius: 4, padding: '0.05rem 0.3rem' }}>{msg.role}</span>
                            )}
                          </div>
                        )}
                        <div style={{
                          background: msg.isOwn ? 'var(--color-primary)' : 'var(--color-surface)',
                          color: msg.isOwn ? '#fff' : 'var(--color-text)',
                          border: msg.isOwn ? 'none' : '1px solid var(--color-border)',
                          borderRadius: msg.isOwn ? '18px 18px 4px 18px' : '18px 18px 18px 4px',
                          padding: '0.6rem 0.9rem',
                          fontSize: '0.875rem',
                          lineHeight: 1.65,
                          whiteSpace: 'pre-wrap',
                          wordBreak: 'break-word',
                          boxShadow: '0 1px 3px rgba(0,0,0,0.07)',
                        }}>
                          {msg.body}
                        </div>
                        <span style={{ fontSize: '0.68rem', color: 'var(--color-text-muted)', paddingRight: msg.isOwn ? 0 : '0.5rem', paddingLeft: msg.isOwn ? '0.5rem' : 0 }}>
                          {msg.createdAt}
                        </span>
                      </div>
                    </div>
                  );
                })}
                <div ref={chatEndRef} />
              </div>

              <div style={{ padding: '0.75rem 1.2rem', borderTop: '1px solid var(--color-border)', display: 'flex', gap: '0.6rem', alignItems: 'flex-end' }}>
                <textarea
                  ref={replyRef}
                  value={replyBody}
                  onChange={e => setReplyBody(e.target.value)}
                  onKeyDown={handleReplyKey}
                  placeholder="پاسخ بنویسید... (Ctrl+Enter برای ارسال)"
                  rows={2}
                  style={{
                    flex: 1, resize: 'none', border: '1px solid var(--color-border)',
                    borderRadius: 12, padding: '0.55rem 0.8rem', fontFamily: 'inherit',
                    fontSize: '0.875rem', background: 'var(--color-bg-soft)',
                    color: 'var(--color-text)', outline: 'none', lineHeight: 1.55,
                    transition: 'border-color 0.15s',
                  }}
                  onFocus={e => (e.target.style.borderColor = 'var(--color-primary)')}
                  onBlur={e => (e.target.style.borderColor = 'var(--color-border)')}
                />
                <button
                  className="btn btn-primary"
                  onClick={sendReply}
                  disabled={!replyBody.trim() || sending}
                  style={{ borderRadius: 10, height: 44, minWidth: 44, padding: '0 0.85rem' }}
                  title="ارسال (Ctrl+Enter)"
                >
                  {sending
                    ? <i className="bi bi-hourglass-split" />
                    : <i className="bi bi-send-fill" />
                  }
                </button>
              </div>
            </>
          ) : (
            <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', color: 'var(--color-text-muted)', gap: '0.75rem' }}>
              <i className="bi bi-chat-dots" style={{ fontSize: '3.5rem', opacity: 0.25 }} />
              <span style={{ fontSize: '0.9rem' }}>یک گفتگو انتخاب کنید یا پیام جدید بفرستید</span>
              {contacts.length > 0 && (
                <button className="btn btn-primary btn-sm" onClick={() => setComposing(true)}>
                  <i className="bi bi-pencil-square" /> پیام جدید
                </button>
              )}
            </div>
          )}
        </div>
      </div>
    </AppLayout>
  );
}
