import React, { useEffect, useState } from 'react';
import { Archive, Pin, RefreshCw, Sparkles } from 'lucide-react';
import { researchBridge } from '../../lib/researchBridge';
import type {
  GhostNote,
  NoteEmbedInput,
  NoteAuditTrail,
  NoteType,
  ResearchNote,
  ThesisStatus,
} from '../../types/research';
import {
  ConfidenceBadge,
  EvidenceBadge,
  FreshnessBadge,
  NoteTypeBadge,
  TagChip,
} from './primitives/Badges';
import { GHOST_CLASS_LABELS, NOTE_TYPE_LABELS } from './primitives/researchMeta';
import { ResearchEditor } from './primitives/ResearchEditor';

interface ResearchInspectorPaneProps {
  note: ResearchNote | null;
  ghost: GhostNote | null;
  hiddenMessage?: string | null;
  auditTrail: NoteAuditTrail | null;
  isLoadingAuditTrail: boolean;
  isUpdatingNote?: boolean;
  isArchivingNote?: boolean;
  isPromotingNote?: boolean;
  isReviewingGhost?: boolean;
  error?: string | null;
  onSelectNote?: (noteId: string) => void;
  onRefreshAuditTrail: () => void;
  onUpdateNote: (noteId: string, patch: {
    rawText?: string;
    bodyHtml?: string;
    embeds?: NoteEmbedInput[];
    title?: string;
    noteType?: NoteType;
    pinned?: boolean;
    thesisStatus?: ThesisStatus;
  }) => Promise<unknown>;
  onArchiveNote: (noteId: string, archived: boolean) => Promise<unknown>;
  onPromoteNote: (noteId: string, thesisStatus?: ThesisStatus, noteType?: NoteType) => Promise<unknown>;
  onReviewGhost: (ghostId: string, action: 'accept' | 'ignore' | 'dismiss' | 'pin') => Promise<unknown>;
}

const editableNoteTypes: NoteType[] = [
  'fact',
  'management_signal',
  'claim',
  'thesis',
  'sub_thesis',
  'risk',
  'catalyst',
  'valuation_point',
  'question',
  'contradiction',
  'channel_check',
  'mosaic_insight',
];

export const ResearchInspectorPane: React.FC<ResearchInspectorPaneProps> = ({
  note,
  ghost,
  hiddenMessage,
  auditTrail,
  isLoadingAuditTrail,
  isUpdatingNote = false,
  isArchivingNote = false,
  isPromotingNote = false,
  isReviewingGhost = false,
  error,
  onSelectNote,
  onRefreshAuditTrail,
  onUpdateNote,
  onArchiveNote,
  onPromoteNote,
  onReviewGhost,
}) => {
  const [draftTitle, setDraftTitle] = useState('');
  const [draftBody, setDraftBody] = useState('');
  const [draftBodyHtml, setDraftBodyHtml] = useState('');
  const [draftEmbeds, setDraftEmbeds] = useState<NoteEmbedInput[]>([]);
  const [draftType, setDraftType] = useState<NoteType>('claim');

  useEffect(() => {
    if (!note) {
      return;
    }

    setDraftTitle(note.title ?? '');
    setDraftBody(note.rawText);
    setDraftBodyHtml(note.bodyHtml ?? note.rawText);
    setDraftEmbeds(note.embeds ?? []);
    setDraftType(note.noteType);
  }, [note]);

  if (!note && !ghost) {
    return (
      <div className="flex h-full flex-col justify-between p-4 sm:p-5">
        <div>
          <h2 className="text-base font-semibold text-[var(--research-text-strong)] sm:text-lg">
            Inspector
          </h2>
          <p className="mt-2 text-xs leading-6 text-[var(--research-text-muted)] sm:mt-2.5 sm:text-sm">
            Select a note or ghost note to inspect provenance, links, evidence, and promotion controls.
          </p>
        </div>
      </div>
    );
  }

  return (
    <div className="space-y-4 p-4 sm:space-y-5 sm:p-5">
      {hiddenMessage ? (
        <div className="rounded-2xl border border-warn/40 bg-warn/10 px-3 py-2 text-xs text-warn">
          {hiddenMessage}
        </div>
      ) : null}
      {error ? (
        <div className="rounded-2xl border border-negative/40 bg-negative/10 px-3 py-2 text-xs text-negative">
          {error}
        </div>
      ) : null}

      {ghost ? (
        <>
          <div>
            <div className="text-[10px] uppercase tracking-[0.2em] text-term-text-tertiary">
              Ghost Note
            </div>
            <h2 className="mt-2 text-lg font-semibold text-term-text sm:text-xl">
              {ghost.headline}
            </h2>
            <p className="mt-2.5 text-sm leading-6 text-term-text-muted sm:mt-3 sm:leading-7">
              {ghost.body}
            </p>
          </div>

          <div className="flex flex-wrap gap-2">
            <TagChip label={GHOST_CLASS_LABELS[ghost.ghostClass]} />
            <ConfidenceBadge confidence={ghost.confidence} />
            <FreshnessBadge timestamp={ghost.updatedAt} />
          </div>

          {ghost.ghostClass === 'missing_evidence_prompt' &&
          ghost.supportingNoteIds.length > 0 &&
          onSelectNote ? (
            <div className="rounded-2xl border border-info/30 bg-info/5 px-3 py-2">
              <div className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                Referenced claim
              </div>
              <button
                type="button"
                onClick={() => onSelectNote(ghost.supportingNoteIds[0])}
                className="mt-1 text-sm text-info underline decoration-info/40 underline-offset-2 hover:text-term-text"
              >
                View unsupported claim
              </button>
            </div>
          ) : null}

          <div className="grid gap-2.5 sm:gap-3">
            <button
              type="button"
              disabled={isReviewingGhost}
              onClick={() => {
                void onReviewGhost(ghost.id, 'accept');
              }}
              className="w-full rounded-2xl border border-info/40 bg-info/10 px-4 py-2.5 text-sm text-info transition-colors hover:border-info hover:text-term-text disabled:cursor-not-allowed disabled:opacity-50 sm:py-3"
            >
              Accept ghost
            </button>
            <button
              type="button"
              disabled={isReviewingGhost}
              onClick={() => {
                void onReviewGhost(ghost.id, 'pin');
              }}
              className="w-full rounded-2xl border border-term-border bg-term-surface px-4 py-2.5 text-sm text-term-text transition-colors hover:border-info/50 disabled:cursor-not-allowed disabled:opacity-50 sm:py-3"
            >
              Pin ghost
            </button>
            <button
              type="button"
              disabled={isReviewingGhost}
              onClick={() => {
                void onReviewGhost(ghost.id, 'dismiss');
              }}
              className="w-full rounded-2xl border border-negative/40 bg-negative/10 px-4 py-2.5 text-sm text-negative transition-colors hover:border-negative/70 disabled:cursor-not-allowed disabled:opacity-50 sm:py-3"
            >
              Dismiss ghost
            </button>
          </div>
        </>
      ) : null}

      {note ? (
        <>
          <div className="flex flex-wrap items-center gap-2">
            <NoteTypeBadge noteType={note.noteType} />
            <EvidenceBadge status={note.evidenceStatus} />
            <ConfidenceBadge confidence={note.confidence} />
            <FreshnessBadge timestamp={note.updatedAt} />
          </div>

          <div className="space-y-3">
            <label className="flex flex-col gap-2">
              <span className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                Title
              </span>
              <input
                value={draftTitle}
                onChange={(event) => setDraftTitle(event.target.value)}
                className="w-full rounded-2xl border border-term-border bg-term-bg px-3 py-2.5 text-sm text-term-text outline-none transition-colors focus:border-info sm:py-3"
              />
            </label>
            <label className="flex flex-col gap-2">
              <span className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                Body
              </span>
              <ResearchEditor
                content={draftBodyHtml}
                embeds={draftEmbeds}
                minRows={6}
                className="rounded-2xl"
                onPreviewLink={async (url) => {
                  const result = await researchBridge.previewNoteLink({ url, kind: 'article' });
                  return result.embed;
                }}
                onChange={(html, text, embeds) => {
                  setDraftBodyHtml(html);
                  setDraftBody(text);
                  setDraftEmbeds(embeds);
                }}
              />
            </label>
            <label className="flex flex-col gap-2">
              <span className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                Note Type
              </span>
              <select
                value={draftType}
                onChange={(event) => setDraftType(event.target.value as NoteType)}
                className="w-full rounded-2xl border border-term-border bg-term-bg px-3 py-2.5 text-sm text-term-text outline-none transition-colors focus:border-info sm:py-3"
              >
                {editableNoteTypes.map((noteType) => (
                  <option key={noteType} value={noteType}>
                    {NOTE_TYPE_LABELS[noteType]}
                  </option>
                ))}
              </select>
            </label>
            <div className="grid grid-cols-2 gap-2.5 sm:gap-3">
              <button
                type="button"
                disabled={isUpdatingNote}
                onClick={() => {
                  void onUpdateNote(note.id, {
                    title: draftTitle,
                    rawText: draftBody,
                    bodyHtml: draftBodyHtml,
                    embeds: draftEmbeds,
                    noteType: draftType,
                  });
                }}
                className="rounded-2xl border border-info/40 bg-info/10 px-3 py-2.5 text-sm text-info transition-colors hover:border-info hover:text-term-text disabled:cursor-not-allowed disabled:opacity-50 sm:px-4 sm:py-3"
              >
                {isUpdatingNote ? 'Saving...' : 'Save note'}
              </button>
              <button
                type="button"
                disabled={isPromotingNote}
                onClick={() => {
                  void onPromoteNote(
                    note.id,
                    'accepted_core',
                    note.noteType === 'thesis' ? 'thesis' : 'sub_thesis',
                  );
                }}
                className="inline-flex items-center justify-center gap-1.5 rounded-2xl border border-positive/35 bg-positive/10 px-3 py-2.5 text-sm text-positive transition-colors hover:border-positive/60 disabled:cursor-not-allowed disabled:opacity-50 sm:gap-2 sm:px-4 sm:py-3"
              >
                <Sparkles className="h-3.5 w-3.5 sm:h-4 sm:w-4" />
                {isPromotingNote ? 'Promoting...' : 'Promote'}
              </button>
            </div>
            <div className="grid grid-cols-2 gap-2.5 sm:gap-3">
              <button
                type="button"
                disabled={isUpdatingNote}
                onClick={() => {
                  void onUpdateNote(note.id, { pinned: !note.pinned });
                }}
                className="inline-flex items-center justify-center gap-1.5 rounded-2xl border border-term-border bg-term-surface px-3 py-2.5 text-sm text-term-text transition-colors hover:border-info/50 disabled:cursor-not-allowed disabled:opacity-50 sm:gap-2 sm:px-4 sm:py-3"
              >
                <Pin className="h-3.5 w-3.5 sm:h-4 sm:w-4" />
                {note.pinned ? 'Unpin' : 'Pin'}
              </button>
              <button
                type="button"
                disabled={isArchivingNote}
                onClick={() => {
                  void onArchiveNote(note.id, !note.archived);
                }}
                className="inline-flex items-center justify-center gap-1.5 rounded-2xl border border-negative/40 bg-negative/10 px-3 py-2.5 text-sm text-negative transition-colors hover:border-negative/70 disabled:cursor-not-allowed disabled:opacity-50 sm:gap-2 sm:px-4 sm:py-3"
              >
                <Archive className="h-3.5 w-3.5 sm:h-4 sm:w-4" />
                {isArchivingNote ? 'Saving...' : note.archived ? 'Restore' : 'Archive'}
              </button>
            </div>
          </div>

          <div className="space-y-3 rounded-2xl border border-term-border bg-term-surface p-3 sm:p-4">
            <div className="flex items-center justify-between gap-3">
              <div className="min-w-0 flex-1">
                <div className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                  Audit Trail
                </div>
                <div className="mt-1 truncate text-sm text-term-text">
                  {isLoadingAuditTrail
                    ? 'Loading evidence trace...'
                    : `${auditTrail?.links.length ?? 0} links, ${auditTrail?.sources.length ?? 0} sources`}
                </div>
              </div>
              <button
                type="button"
                onClick={onRefreshAuditTrail}
                className="inline-flex shrink-0 items-center gap-1.5 rounded-xl border border-term-border bg-term-bg px-2.5 py-2 text-xs uppercase tracking-[0.18em] text-term-text-tertiary transition-colors hover:border-info/60 hover:text-term-text sm:gap-2 sm:px-3"
              >
                <RefreshCw className="h-3.5 w-3.5" />
                <span className="hidden sm:inline">Refresh</span>
              </button>
            </div>

            {auditTrail ? (
              <div className="space-y-3 text-sm text-term-text-muted">
                <div>
                  <div className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                    Sources
                  </div>
                  <ul className="mt-2 space-y-2">
                    {auditTrail.sources.slice(0, 4).map((source) => (
                      <li key={source.id} className="text-xs sm:text-sm">
                        <div className="flex gap-3">
                          {typeof source.metadataJson.imageUrl === 'string' ? (
                            <img
                              src={source.metadataJson.imageUrl}
                              alt=""
                              className="h-16 w-16 flex-shrink-0 rounded object-cover"
                            />
                          ) : null}
                          <div className="min-w-0 flex-1">
                            <div className="truncate text-term-text">{source.title}</div>
                            <div className="truncate text-term-text-tertiary">
                              {[source.publisher, source.kind].filter(Boolean).join(' • ')}
                            </div>
                            {source.url ? (
                              <a
                                href={source.url}
                                className="block truncate text-info underline decoration-info/40 underline-offset-2"
                              >
                                {source.url}
                              </a>
                            ) : null}
                            {typeof source.metadataJson.excerpt === 'string' ? (
                              <p className="mt-1 line-clamp-2 text-term-text-muted">
                                {source.metadataJson.excerpt}
                              </p>
                            ) : null}
                          </div>
                        </div>
                      </li>
                    ))}
                  </ul>
                </div>
                <div>
                  <div className="text-[10px] uppercase tracking-[0.18em] text-term-text-tertiary">
                    Audit Events
                  </div>
                  <ul className="mt-2 space-y-2">
                    {auditTrail.auditEvents.slice(0, 5).map((event) => (
                      <li key={event.id} className="text-xs sm:text-sm">
                        <span className="text-term-text">{event.action}</span>
                        <span className="text-term-text-tertiary"> • {event.createdAt}</span>
                      </li>
                    ))}
                  </ul>
                </div>
              </div>
            ) : (
              <p className="text-sm text-term-text-tertiary">
                Select a note to inspect its evidence chain.
              </p>
            )}
          </div>
        </>
      ) : null}
    </div>
  );
};
