import Link from 'next/link';
import AgentRunButton from '@/app/agents/agent-run-button';
import { AgentQuickControls } from './agent-quick-controls';
import { AgentActivityFeed } from './agent-activity-feed';
import { formatRelativeTime, humanizeStatus } from './format-helpers';

type ActivityItem = {
  id: string;
  kind: 'execution' | 'approval';
  status: string;
  title: string;
  risk?: string | null;
  at: string | null;
};

type Engine = {
  id: string;
  label: string;
  available?: boolean;
  default_model?: string;
  models?: { name: string }[];
};

type ToolDef = {
  name: string;
  title: string;
  description: string;
  category: string;
  risk_level: string;
  available: boolean;
  missing_env: string[];
};

type AgentLike = {
  id: string;
  name: string;
  mission?: string;
  status?: string;
  engine?: string;
  model?: string;
  last_run_at?: string;
  last_heartbeat?: string;
  default_space_id?: string | null;
};

type ServerAction = (formData: FormData) => Promise<void>;

export function AgentDetailSimple({
  agent,
  scheduleRaw,
  activity,
  spaces,
  engines,
  agentTools,
  availableTools,
  toolCategories,
  updateDefaultSpaceAction,
  updateModelAction,
  updateScheduleAction,
  updateToolsAction,
}: {
  agent: AgentLike;
  scheduleRaw: string;
  activity: ActivityItem[];
  spaces: { id: string; name: string }[];
  engines: Engine[];
  agentTools: string[];
  availableTools: ToolDef[];
  toolCategories: string[];
  updateDefaultSpaceAction: ServerAction;
  updateModelAction: ServerAction;
  updateScheduleAction: ServerAction;
  updateToolsAction: ServerAction;
}) {
  const status = humanizeStatus(agent.status);
  const lastRun = formatRelativeTime(agent.last_run_at || agent.last_heartbeat || null);

  return (
    <section className="ad-simple">
      {/* Status strip — the at-a-glance answer to "is this agent OK?" */}
      <div className="ad-statusbar">
        <span className={`ad-statusbar-state tone-${status.tone}`}>
          <span className="ad-dot" aria-hidden />
          {status.label}
        </span>
        <div className="ad-statusbar-actions">
          <AgentRunButton agentId={agent.id} />
          <Link className="admin-button secondary" href={`/agents/${agent.id}?view=details`}>
            Configuration
          </Link>
        </div>
      </div>

      <AgentQuickControls
        agentId={agent.id}
        engine={agent.engine || ''}
        model={agent.model || ''}
        engines={engines}
        scheduleRaw={scheduleRaw}
        lastRun={lastRun}
        statusLabel={status.label}
        statusTone={status.tone}
        defaultSpaceId={agent.default_space_id || null}
        spaces={spaces}
        agentTools={agentTools}
        availableTools={availableTools}
        toolCategories={toolCategories}
        updateModelAction={updateModelAction}
        updateScheduleAction={updateScheduleAction}
        updateToolsAction={updateToolsAction}
        updateDefaultSpaceAction={updateDefaultSpaceAction}
      />

      <AgentActivityFeed items={activity} />
    </section>
  );
}
