import type { StaffingRequirement, BlueprintFieldDefinition } from "@nexus/shared";
import { toDateInputValue } from "~/lib/format.js";
import { uuid } from "~/lib/uuid.js";

// ─── Constants ────────────────────────────────────────────────────────────────

export const ORDER_TYPE_OPTIONS = [
  { value: "BD", label: "BD" },
  { value: "CHARGEABLE", label: "Chargeable" },
  { value: "INTERNAL", label: "Internal" },
  { value: "OVERHEAD", label: "Overhead" },
] as const;

export const ALLOCATION_TYPE_OPTIONS = [
  { value: "INT", label: "INT" },
  { value: "EXT", label: "EXT" },
] as const;

export const BTN_PRIMARY =
  "px-5 py-2 bg-brand-600 text-white rounded-lg hover:bg-brand-700 text-sm font-medium disabled:opacity-50 transition-colors";

export const BTN_SECONDARY =
  "px-5 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium transition-colors";

export const STEPS = [
  "Blueprint & Identity",
  "Timeline & Budget",
  "Staffing Demand",
  "Suggestions",
  "Review & Create",
];

// ─── Types ────────────────────────────────────────────────────────────────────

export interface Assignment {
  requirementId: string;
  resourceId: string;
  resourceName: string;
  role: string;
}

export interface WizardState {
  blueprintId: string | null;
  shortCode: string;
  name: string;
  orderType: string;
  allocationType: string;
  startDate: string;
  endDate: string;
  budgetEur: string;
  winProbability: number;
  responsiblePerson: string;
  staffingReqs: StaffingRequirement[];
  assignments: Assignment[];
  saveAsDraft: boolean;
  dynamicFields: Record<string, unknown>;
  blueprintName: string | null;
  blueprintFieldDefs: BlueprintFieldDefinition[];
}

export type SuggestionItem = {
  resourceId: string;
  resourceName: string;
  eid: string;
  score: number;
  currentUtilization: number;
  availabilityConflicts: string[];
  estimatedDailyCostCents: number;
  valueScore?: number;
};

// ─── Factory functions ────────────────────────────────────────────────────────

export function makeDefaultState(): WizardState {
  const today = toDateInputValue(new Date());
  return {
    blueprintId: null,
    shortCode: "",
    name: "",
    orderType: "CHARGEABLE",
    allocationType: "INT",
    startDate: today,
    endDate: today,
    budgetEur: "",
    winProbability: 100,
    responsiblePerson: "",
    staffingReqs: [],
    assignments: [],
    saveAsDraft: true,
    dynamicFields: {},
    blueprintName: null,
    blueprintFieldDefs: [],
  };
}

export function makeReq(): StaffingRequirement {
  return {
    id: uuid(),
    role: "",
    requiredSkills: [],
    preferredSkills: [],
    hoursPerDay: 8,
    headcount: 1,
  };
}
