import type { ContractPhase, ContractWithRelations } from "../../lib/api";

interface PhaseHintProps {
  contract: ContractWithRelations;
  onAdvancePhase: (phase: ContractPhase) => void;
}

const phaseOrder: ContractPhase[] = ["research", "specify", "plan", "execute", "review"];

interface HintConfig {
  condition: (contract: ContractWithRelations) => boolean;
  message: (contract: ContractWithRelations) => string;
  nextPhase: ContractPhase;
}

const phaseHints: Record<ContractPhase, HintConfig | null> = {
  research: {
    condition: (c) => c.files.length >= 1,
    message: (c) =>
      `You have ${c.files.length} file${c.files.length === 1 ? "" : "s"}. Ready to specify requirements?`,
    nextPhase: "specify",
  },
  specify: {
    condition: (c) => c.files.length >= 2,
    message: () => "Spec files ready. Create implementation plan?",
    nextPhase: "plan",
  },
  plan: {
    condition: (c) => c.files.length >= 1 && c.repositories.length >= 1,
    message: () => "Plan documented. Ready to create tasks?",
    nextPhase: "execute",
  },
  execute: {
    condition: (c) => {
      // Show hint only when all tasks are complete
      const doneTasks = c.tasks.filter(
        (t) => t.status === "done" || t.status === "merged"
      );
      return c.tasks.length > 0 && doneTasks.length === c.tasks.length;
    },
    message: (c) => {
      const doneTasks = c.tasks.filter(
        (t) => t.status === "done" || t.status === "merged"
      );
      return `All ${doneTasks.length} task${doneTasks.length === 1 ? "" : "s"} complete. Ready for review?`;
    },
    nextPhase: "review",
  },
  review: null, // No hint for review phase - it's the final phase
};

export function PhaseHint({ contract, onAdvancePhase }: PhaseHintProps) {
  const hintConfig = phaseHints[contract.phase];

  // No hint for this phase
  if (!hintConfig) return null;

  // Condition not met
  if (!hintConfig.condition(contract)) return null;

  return (
    <div className="flex items-center gap-3 p-3 bg-[rgba(117,170,252,0.05)] border border-[rgba(117,170,252,0.2)]">
      <span className="font-mono text-xs text-[#9bc3ff]">
        {hintConfig.message(contract)}
      </span>
      <button
        onClick={() => onAdvancePhase(hintConfig.nextPhase)}
        className="px-3 py-1 font-mono text-xs text-[#dbe7ff] bg-[#0f3c78] border border-[#3f6fb3] hover:bg-[#153667] transition-colors uppercase whitespace-nowrap"
      >
        Advance to {hintConfig.nextPhase}
      </button>
    </div>
  );
}

export function getNextPhase(currentPhase: ContractPhase): ContractPhase | null {
  const currentIndex = phaseOrder.indexOf(currentPhase);
  if (currentIndex === -1 || currentIndex === phaseOrder.length - 1) {
    return null;
  }
  return phaseOrder[currentIndex + 1];
}

export function getPreviousPhase(currentPhase: ContractPhase): ContractPhase | null {
  const currentIndex = phaseOrder.indexOf(currentPhase);
  if (currentIndex <= 0) {
    return null;
  }
  return phaseOrder[currentIndex - 1];
}
