import { useState, useDeferredValue } from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { LocalTask, LocalFileEntry, SubtaskActivity, Visibility, fetchMyOrgs, updateLocalTask } from '../../api';
import { relativeTime } from '../../lib/utils';
import { ownerDisplayName } from '../../lib/owner';
import { DetailTabId } from '../../lib/urlState';
import { DetailHeader } from './DetailHeader';
import { useVisibleDetailTabs } from './detailTabs';
import { ContinueWithPieceDialog } from './ContinueWithPieceDialog';
import { SkeletonDetailPanel } from '../shared/Skeleton';
import { OverviewTab } from './tabs/OverviewTab';
import { ProgressTab } from './tabs/ProgressTab';
import { FilesTab } from './tabs/FilesTab';
import { TraceTab } from './tabs/TraceTab';
import { BrowserTab } from './tabs/BrowserTab';
import { ConsoleTab } from './tabs/ConsoleTab';
import { BrowserSessionPanel } from '../browser/BrowserSessionPanel';
import { useAuthState } from '../../App';
import type { SubtaskFilePreviewHandler } from './tabs/SubtasksPanel';

interface LocalDetailPanelProps {
  task: LocalTask | null;
  taskId: number;
  section: 'workspace' | 'input' | 'output' | 'logs';
  currentPath: string;
  entries: LocalFileEntry[];
  pathSegments: string[];
  loading: boolean;
  detailTab: DetailTabId;
  detailWidth: 'normal' | 'focused';
  showWidthToggle: boolean;
  onTabChange: (tab: DetailTabId) => void;
  onWidthToggle: () => void;
  onClose: () => void;
  onDelete?: () => Promise<void>;
  onSectionChange: (section: 'workspace' | 'input' | 'output' | 'logs') => void;
  onNavigate: (path: string) => void;
  onPreview: (path: string, name: string) => void;
  onViewFullLog: () => void;
  onRefresh?: () => void;
  isRefreshing?: boolean;
  subtaskActivities?: SubtaskActivity[];
  onSubtaskFilePreview?: SubtaskFilePreviewHandler;
  shareToken?: string | null;
  onShareChange?: () => void;
}


export function LocalDetailPanel({
  task, taskId, section, currentPath, entries, pathSegments,
  loading, detailTab, detailWidth, showWidthToggle,
  onTabChange, onWidthToggle, onClose, onDelete, onSectionChange, onNavigate, onPreview, onViewFullLog,
  onRefresh, isRefreshing, subtaskActivities, onSubtaskFilePreview,
  shareToken, onShareChange,
}: LocalDetailPanelProps) {
  const { t } = useTranslation('detail');
  // Deferred tab id for content rendering. The tab indicator (DetailHeader)
  // uses `detailTab` (immediate) so the underline jumps on click. The heavy
  // content area below uses `deferredDetailTab` so expensive panels
  // (ProgressTab / TraceTab) don't block the click → indicator paint.
  // When detailTab !== deferredDetailTab we know a transition is in flight.
  const deferredDetailTab = useDeferredValue(detailTab);
  const tabTransitionPending = detailTab !== deferredDetailTab;
  const [deleting, setDeleting] = useState(false);
  const [continueOpen, setContinueOpen] = useState(false);
  const [editingVisibility, setEditingVisibility] = useState(false);
  const [savingVisibility, setSavingVisibility] = useState(false);
  const [editVisibility, setEditVisibility] = useState<Visibility>('private');
  const [editScopeOrgId, setEditScopeOrgId] = useState<string | null>(null);
  const [editError, setEditError] = useState<string | null>(null);
  const qc = useQueryClient();
  const authState = useAuthState();
  const currentUserId = authState.mode === 'authenticated' ? authState.user.id : null;
  const currentUserRole = authState.mode === 'authenticated' ? authState.user.role : null;
  const canEditVisibility = task
    ? (currentUserRole === 'admin' || (currentUserId !== null && task.ownerId === currentUserId))
    : false;
  const { data: orgs = [] } = useQuery({
    queryKey: ['my-orgs'],
    queryFn: fetchMyOrgs,
    staleTime: 5 * 60 * 1000,
    enabled: editingVisibility,
  });

  // Visible tabs (incl. Browser/SSH only while their session is live). Shared
  // with the tablet chat header via useVisibleDetailTabs so the two agree.
  const visibleTabs = useVisibleDetailTabs(task?.id ?? null);

  const handleStartEdit = () => {
    if (!task) return;
    setEditVisibility((task.visibility as Visibility) ?? 'private');
    setEditScopeOrgId(task.visibilityScopeOrgId ?? null);
    setEditError(null);
    setEditingVisibility(true);
  };

  const handleSaveVisibility = async () => {
    if (!task) return;
    setEditError(null);
    setSavingVisibility(true);
    try {
      await updateLocalTask(task.id, {
        visibility: editVisibility,
        visibilityScopeOrgId: editVisibility === 'org' ? editScopeOrgId : null,
      });
      await qc.invalidateQueries({ queryKey: ['localTaskDetail', task.id] });
      setEditingVisibility(false);
    } catch (err) {
      setEditError(err instanceof Error ? err.message : String(err));
    } finally {
      setSavingVisibility(false);
    }
  };

  const handleDelete = async () => {
    if (!onDelete) return;
    if (!window.confirm(t('panel.deleteConfirm'))) return;
    setDeleting(true);
    try {
      await onDelete();
    } finally {
      setDeleting(false);
    }
  };

  const jobStatus = task?.latestJob?.status;
  const isActiveJob = jobStatus === 'running' || jobStatus === 'dispatching';

  return (
    <div className="flex flex-col h-full overflow-hidden bg-surface">
      <DetailHeader
        title={`Task #${taskId}`}
        subtitle={t('panel.subtitle')}
        tabs={visibleTabs}
        activeTab={detailTab}
        tabTransitionPending={tabTransitionPending}
        onTabChange={onTabChange}
        onClose={onClose}
        detailWidth={detailWidth}
        onWidthToggle={showWidthToggle ? onWidthToggle : undefined}
        taskId={taskId}
        shareToken={shareToken}
        onShareChange={onShareChange}
        latestJobStatus={task?.latestJob?.status ?? null}
        onContinue={task?.latestJob ? () => setContinueOpen(true) : undefined}
      />
      {continueOpen && task?.latestJob && (
        <ContinueWithPieceDialog
          taskId={taskId}
          prevJob={{
            id: task.latestJob.id,
            // task.pieceName tracks last-piece-wins after each Continue, so
            // it equals the latest job's piece.
            pieceName: task.pieceName,
            status: task.latestJob.status,
          }}
          onClose={() => setContinueOpen(false)}
        />
      )}
      <div className={`flex-1 min-h-0 p-3 ${detailTab === 'ssh' ? 'overflow-hidden flex flex-col' : 'overflow-y-auto'}`}>
        {loading && !task && <SkeletonDetailPanel />}
        {task && (
          <>
            <div className="mb-2 flex items-center gap-2 text-2xs text-slate-500 flex-wrap">
              <span>{t('panel.author')}: <b>{ownerDisplayName(task.ownerId, task.ownerName)}</b></span>
              <span>·</span>
              <span>{relativeTime(task.createdAt)}</span>
              {task.visibility === 'private' && <span>· 🔒 {t('visibility.private')}</span>}
              {task.visibility === 'org' && <span>· 🏢 {task.visibilityScopeOrgName ?? 'org'}</span>}
              {task.visibility === 'public' && <span>· 🌐 {t('visibility.public')}</span>}
              {canEditVisibility && !editingVisibility && (
                <button
                  className="ml-2 underline text-slate-500 hover:text-slate-700"
                  onClick={handleStartEdit}
                >
                  {t('panel.change')}
                </button>
              )}
            </div>
            {editingVisibility && (
              <div className="mb-3 p-2.5 border border-hairline rounded-md bg-canvas text-xs">
                <div className="flex gap-3 flex-wrap">
                  <label className="flex items-center gap-1">
                    <input
                      type="radio"
                      checked={editVisibility === 'private'}
                      onChange={() => setEditVisibility('private')}
                    />
                    🔒 {t('visibility.private')}
                  </label>
                  <label className="flex items-center gap-1">
                    <input
                      type="radio"
                      checked={editVisibility === 'org'}
                      onChange={() => {
                        setEditVisibility('org');
                        if (!editScopeOrgId && orgs.length > 0) setEditScopeOrgId(orgs[0].orgId);
                      }}
                      disabled={orgs.length === 0}
                    />
                    🏢 {t('visibility.org')}
                  </label>
                  <label className="flex items-center gap-1">
                    <input
                      type="radio"
                      checked={editVisibility === 'public'}
                      onChange={() => setEditVisibility('public')}
                    />
                    🌐 {t('visibility.public')}
                  </label>
                </div>
                {editVisibility === 'org' && orgs.length > 1 && (
                  <select
                    className="mt-2 px-2 h-7 border border-hairline rounded-md text-xs bg-canvas focus:outline-none focus:ring-2 focus:ring-accent-ring"
                    value={editScopeOrgId ?? ''}
                    onChange={e => setEditScopeOrgId(e.target.value)}
                  >
                    {orgs.map(o => <option key={o.orgId} value={o.orgId}>{o.orgName}</option>)}
                  </select>
                )}
                {editVisibility === 'org' && orgs.length === 1 && (
                  <div className="mt-1 text-2xs text-slate-500">{t('visibility.sharedWith', { org: orgs[0].orgName })}</div>
                )}
                {editVisibility === 'org' && orgs.length === 0 && (
                  <div className="mt-1 text-2xs text-slate-400">{t('visibility.orgLoginHint')}</div>
                )}
                {editError && <div className="mt-1 text-2xs text-red-600">{editError}</div>}
                <div className="mt-2 flex gap-2">
                  <button
                    disabled={savingVisibility}
                    onClick={() => void handleSaveVisibility()}
                    className="px-3 h-7 bg-accent text-accent-fg rounded-md text-xs font-semibold disabled:opacity-50 hover:bg-accent-deep transition-colors"
                  >
                    {savingVisibility ? t('panel.saving') : t('panel.save')}
                  </button>
                  <button
                    disabled={savingVisibility}
                    onClick={() => { setEditingVisibility(false); setEditError(null); }}
                    className="px-3 h-7 border border-hairline rounded-md text-xs text-slate-600 hover:bg-surface transition-colors"
                  >
                    {t('panel.cancel')}
                  </button>
                </div>
              </div>
            )}
            {task?.latestJob?.status === 'waiting_human' && task?.latestJob?.waitReason === 'browser_login' && (
              <BrowserSessionPanel />
            )}
            {deferredDetailTab === 'overview' && <OverviewTab task={task} subtaskActivities={subtaskActivities} onSubtaskFilePreview={onSubtaskFilePreview} />}
            {deferredDetailTab === 'activity' && <ProgressTab task={task} onViewFullLog={onViewFullLog} subtaskActivities={subtaskActivities} />}
            {deferredDetailTab === 'files' && <FilesTab section={section} currentPath={currentPath} entries={entries} pathSegments={pathSegments} taskId={taskId} onSectionChange={onSectionChange} onNavigate={onNavigate} onPreview={onPreview} onRefresh={onRefresh} isRefreshing={isRefreshing} />}
            {deferredDetailTab === 'trace' && <TraceTab taskId={taskId} />}
            {deferredDetailTab === 'browser' && <BrowserTab taskId={taskId} />}
            {deferredDetailTab === 'ssh' && <ConsoleTab taskId={taskId} />}
          </>
        )}
      </div>
      {!loading && task && (
        <div className="flex-shrink-0 border-t border-hairline bg-canvas px-3 py-2.5">
          <div className="flex gap-2 items-center">
            {onDelete && !isActiveJob ? (
              <button
                disabled={deleting}
                onClick={handleDelete}
                className="px-3 h-7 bg-canvas border border-red-200 text-red-700 dark:text-red-300 rounded-md text-xs font-medium disabled:opacity-50 hover:bg-red-50 dark:hover:bg-red-500/15 transition-colors"
              >
                {deleting ? t('panel.deleting') : t('panel.delete')}
              </button>
            ) : null}
          </div>
        </div>
      )}
    </div>
  );
}
