import { useEffect, useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { LocalTask, MissionBrief, SubtaskActivity, putFeedback, updateMissionBrief } from '../../../api';
import { StatusBadge } from '../../shared/StatusBadge';
import { SubtasksPanel, type SubtaskFilePreviewHandler } from './SubtasksPanel';
import { ContextUsageGauge } from '../ContextUsageGauge';
import { ReflectionBadge } from '../ReflectionBadge';

const GOOD_TAGS = ['出力の精度が高い', 'フォーマットが適切', '指示をよく理解していた', '速度が適切だった'];
const BAD_TAGS = ['出力の精度が低い', 'フォーマットが不適切', '指示と違う結果になった', '不要な作業をしていた', '途中で止まった / ASKが多すぎた'];

function FeedbackPanel({ task }: { task: LocalTask }) {
  const qc = useQueryClient();
  const isComplete = task.latestJob?.status === 'succeeded' || task.latestJob?.status === 'failed';
  const hasFeedback = !!task.feedbackRating;

  const [rating, setRating] = useState<'good' | 'bad' | null>(task.feedbackRating ?? null);
  const [selectedTags, setSelectedTags] = useState<string[]>(task.feedbackTags ?? []);
  const [comment, setComment] = useState(task.feedbackComment ?? '');
  const [editing, setEditing] = useState(!hasFeedback);

  const mutation = useMutation({
    mutationFn: (fb: { rating: 'good' | 'bad'; tags: string[]; comment?: string }) =>
      putFeedback(task.id, fb),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['localTasks'] });
      qc.invalidateQueries({ queryKey: ['localTaskDetail', task.id] });
      setEditing(false);
    },
  });

  if (!isComplete) return null;

  const tags = rating === 'good' ? GOOD_TAGS : rating === 'bad' ? BAD_TAGS : [];
  const toggleTag = (tag: string) => {
    setSelectedTags(prev => prev.includes(tag) ? prev.filter(t => t !== tag) : [...prev, tag]);
  };
  const handleSubmit = () => {
    if (!rating) return;
    mutation.mutate({ rating, tags: selectedTags, comment: comment || undefined });
  };
  const handleRatingClick = (r: 'good' | 'bad') => {
    setRating(r);
    setSelectedTags([]);
    setEditing(true);
  };

  if (!editing && hasFeedback) {
    return (
      <div className="bg-white border border-slate-200 rounded-xl p-4 shadow-sm">
        <div className="flex items-center justify-between">
          <div className="flex items-center gap-2">
            <span className="text-sm font-semibold text-slate-700">フィードバック</span>
            <span className={`text-lg ${task.feedbackRating === 'good' ? 'text-green-500' : 'text-red-500'}`}>
              {task.feedbackRating === 'good' ? '👍' : '👎'}
            </span>
          </div>
          <button
            onClick={() => setEditing(true)}
            className="text-xs text-slate-400 hover:text-slate-600"
          >
            変更
          </button>
        </div>
        {task.feedbackTags && task.feedbackTags.length > 0 && (
          <div className="flex flex-wrap gap-1.5 mt-2">
            {task.feedbackTags.map(tag => (
              <span key={tag} className="px-2 py-0.5 rounded-full text-2xs bg-slate-100 text-slate-600">{tag}</span>
            ))}
          </div>
        )}
        {task.feedbackComment && (
          <div className="mt-2 text-xs text-slate-500">{task.feedbackComment}</div>
        )}
      </div>
    );
  }

  return (
    <div className="bg-white border border-slate-200 rounded-xl p-4 shadow-sm">
      <div className="text-sm font-semibold text-slate-700 mb-2">フィードバック</div>
      <div className="flex gap-2 mb-3">
        <button
          onClick={() => handleRatingClick('good')}
          className={`px-3 py-1.5 rounded-lg text-sm border transition-colors ${
            rating === 'good' ? 'bg-green-50 border-green-300 text-green-700' : 'border-slate-200 text-slate-500 hover:border-slate-300'
          }`}
        >
          👍 良かった
        </button>
        <button
          onClick={() => handleRatingClick('bad')}
          className={`px-3 py-1.5 rounded-lg text-sm border transition-colors ${
            rating === 'bad' ? 'bg-red-50 border-red-300 text-red-700' : 'border-slate-200 text-slate-500 hover:border-slate-300'
          }`}
        >
          👎 改善が必要
        </button>
      </div>

      {rating && (
        <>
          <div className="flex flex-wrap gap-1.5 mb-3">
            {tags.map(tag => (
              <button
                key={tag}
                onClick={() => toggleTag(tag)}
                className={`px-2 py-0.5 rounded-full text-2xs border transition-colors ${
                  selectedTags.includes(tag)
                    ? 'bg-accent-soft border-accent text-accent'
                    : 'border-slate-200 text-slate-500 hover:border-slate-300'
                }`}
              >
                {tag}
              </button>
            ))}
          </div>
          <textarea
            value={comment}
            onChange={e => setComment(e.target.value)}
            placeholder="コメント（任意）"
            maxLength={1000}
            rows={2}
            className="w-full px-3 py-2 text-xs border border-slate-200 rounded-lg resize-none focus:outline-none focus:ring-1 focus:ring-accent-ring mb-2"
          />
          <div className="flex justify-end gap-2">
            {hasFeedback && (
              <button
                onClick={() => { setEditing(false); setRating(task.feedbackRating ?? null); setSelectedTags(task.feedbackTags ?? []); setComment(task.feedbackComment ?? ''); }}
                className="px-3 py-1 text-xs text-slate-400 hover:text-slate-600"
              >
                キャンセル
              </button>
            )}
            <button
              onClick={handleSubmit}
              disabled={mutation.isPending}
              className="px-3 py-1 text-xs bg-accent text-accent-fg rounded-lg hover:bg-accent-deep disabled:opacity-50"
            >
              {mutation.isPending ? '送信中...' : '送信'}
            </button>
          </div>
        </>
      )}
    </div>
  );
}

/**
 * Mission Brief card. Per-task pinned memo with goal / done / open /
 * clarifications. The LLM updates these via the MissionUpdate tool;
 * the user can edit them here to anchor or correct the agent. Always
 * shown so the user can guide the agent before the conversation drifts.
 */
const MISSION_FIELDS: Array<{
  key: keyof MissionBrief;
  label: string;
  placeholder: string;
  emptyHint: string;
}> = [
  { key: 'goal', label: '目標', placeholder: 'このタスクの本質的な目標 (Markdown 可)', emptyHint: '未設定 — エージェントが最初に書きます' },
  { key: 'done', label: '完了', placeholder: '完了したマイルストーン (Markdown 箇条書き推奨)', emptyHint: 'まだ何も完了していません' },
  { key: 'open', label: '残タスク', placeholder: '残っている作業 / ブロッカー', emptyHint: '残タスク未記入' },
  { key: 'clarifications', label: '補足・制約', placeholder: '途中で追加された制約・補足', emptyHint: '補足なし' },
];

const EMPTY_MISSION: MissionBrief = { goal: '', done: '', open: '', clarifications: '' };

function MissionCard({ task }: { task: LocalTask }) {
  const qc = useQueryClient();
  const current = task.missionBrief ?? EMPTY_MISSION;
  const [editing, setEditing] = useState(false);
  const [draft, setDraft] = useState<MissionBrief>(current);
  const [error, setError] = useState<string | null>(null);

  // Keep draft in sync with server-side updates (e.g. LLM writes via
  // MissionUpdate while we're not editing). Don't clobber an active edit.
  useEffect(() => {
    if (!editing) setDraft(task.missionBrief ?? EMPTY_MISSION);
  }, [task.missionBrief, editing]);

  const mutation = useMutation({
    mutationFn: () => updateMissionBrief(task.id, draft),
    onSuccess: () => {
      setEditing(false);
      setError(null);
      qc.invalidateQueries({ queryKey: ['localTaskDetail', task.id] });
      qc.invalidateQueries({ queryKey: ['localTasks'] });
    },
    onError: (err: unknown) => {
      setError(err instanceof Error ? err.message : 'Failed to save mission brief');
    },
  });

  const isEmpty = !current.goal && !current.done && !current.open && !current.clarifications;

  return (
    <div className="bg-white border border-hairline rounded-md p-3.5">
      <div className="flex items-center justify-between mb-2.5">
        <div className="flex items-center gap-1.5">
          <svg className="w-3.5 h-3.5 text-slate-500" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round">
            <path d="M3 2v12M3 2h7l-1 2 1 2H3" />
          </svg>
          <span className="section-label">Mission Brief</span>
          <span className="text-[10px] text-slate-400">— 固定メモ</span>
        </div>
        {!editing ? (
          <button
            type="button"
            onClick={() => { setDraft(current); setEditing(true); setError(null); }}
            className="px-2 h-7 text-2xs font-medium border border-hairline bg-white text-slate-700 hover:bg-surface rounded-md transition-colors"
          >
            編集
          </button>
        ) : null}
      </div>

      {editing ? (
        <div className="flex flex-col gap-2.5">
          {MISSION_FIELDS.map(({ key, label, placeholder }) => (
            <div key={key}>
              <label className="block text-[10px] font-mono uppercase tracking-wider text-slate-500 mb-1">{label}</label>
              <textarea
                value={draft[key] ?? ''}
                onChange={(e) => setDraft({ ...draft, [key]: e.target.value })}
                placeholder={placeholder}
                rows={key === 'goal' ? 2 : 3}
                className="w-full px-2.5 py-1.5 text-xs border border-hairline rounded-md focus:outline-none focus:ring-2 focus:ring-accent-ring focus:border-accent transition-shadow font-mono leading-snug"
              />
            </div>
          ))}
          {error && <div className="text-2xs text-red-600">{error}</div>}
          <div className="flex justify-end gap-1.5">
            <button
              type="button"
              onClick={() => { setEditing(false); setError(null); setDraft(current); }}
              disabled={mutation.isPending}
              className="px-3 h-7 text-xs rounded-md border border-hairline bg-white text-slate-700 hover:bg-surface transition-colors disabled:opacity-50"
            >
              キャンセル
            </button>
            <button
              type="button"
              onClick={() => mutation.mutate()}
              disabled={mutation.isPending}
              className="px-3 h-7 text-xs font-semibold rounded-md bg-accent text-accent-fg hover:bg-accent-deep transition-colors disabled:opacity-50"
            >
              {mutation.isPending ? '保存中...' : '保存'}
            </button>
          </div>
        </div>
      ) : isEmpty ? (
        <div className="text-xs text-slate-500 leading-relaxed">
          まだ Mission Brief は設定されていません。エージェントが必要に応じて自動で書き込みますが、
          手動で目標 / 進捗 / 残タスクをここに固定しておくことで、長い会話の途中でも本質を見失わないように誘導できます。
        </div>
      ) : (
        <div className="flex flex-col gap-2.5">
          {MISSION_FIELDS.map(({ key, label, emptyHint }) => {
            const value = current[key];
            return (
              <div key={key}>
                <div className="text-[10px] font-mono uppercase tracking-wider text-slate-500 mb-0.5">{label}</div>
                {value ? (
                  <div className="text-xs text-slate-800 whitespace-pre-wrap leading-snug font-mono">{value}</div>
                ) : (
                  <div className="text-2xs text-slate-400 italic">{emptyHint}</div>
                )}
              </div>
            );
          })}
        </div>
      )}
    </div>
  );
}

interface OverviewTabProps {
  task: LocalTask;
  subtaskActivities?: SubtaskActivity[];
  onSubtaskFilePreview?: SubtaskFilePreviewHandler;
}

export function OverviewTab({ task, subtaskActivities, onSubtaskFilePreview }: OverviewTabProps) {
  const status = task.latestJob?.status ?? 'queued';

  return (
    <div className="flex flex-col gap-3">
      <div className="bg-white border border-slate-200 rounded-xl p-4 shadow-sm">
        <div className="text-lg font-extrabold text-slate-900">{task.title}</div>
        <div className="flex flex-wrap gap-2 mt-2">
          <StatusBadge status={status} />
          <span className="inline-flex items-center px-2 py-0.5 rounded-full text-2xs bg-slate-100 text-slate-600">{task.pieceName}</span>
          <span className="inline-flex items-center px-2 py-0.5 rounded-full text-2xs bg-slate-100 text-slate-600">{task.priority}</span>
        </div>
        <div className="mt-3 text-[13px] text-slate-600 whitespace-pre-wrap leading-relaxed">{task.body || '(no body)'}</div>
      </div>

      <MissionCard task={task} />

      <ContextUsageGauge
        promptTokens={task.latestJob?.contextPromptTokens}
        limitTokens={task.latestJob?.contextLimitTokens}
        jobStatus={task.latestJob?.status}
      />

      <FeedbackPanel task={task} />

      <ReflectionBadge taskId={task.id} />

      {task.subtasks && task.subtasks.length > 0 && (
        <SubtasksPanel
          taskId={task.id}
          subtasks={task.subtasks}
          subtaskCount={task.subtaskCount ?? task.subtasks.length}
          subtaskCompleted={task.subtaskCompleted ?? 0}
          subtaskActivities={subtaskActivities}
          onFilePreview={onSubtaskFilePreview}
        />
      )}
    </div>
  );
}
