import type { DepartmentKey, UserRole } from "@/lib/types";

export type KpiRowStatus = "on_track" | "watch" | "risk" | "no_score";

export type KpiViewerContext = {
  role?: UserRole | "lead";
  department?: DepartmentKey | null;
};

export type KpiRowDTO = {
  id: string;
  rowKey: string;
  responsibility: string;
  objectiveIndicator: string | null;
  quantityQuality: string | null;
  compliance: string | null;
  dueCommitment: string | null;
  targetValue: number | null;
  quantityValue: number | null;
  compliancePct: number | null;
  dueDate: string | null;
  status: KpiRowStatus;
  sortOrder: number;
};

export type KpiSectionDTO = {
  id: string;
  sectionKey: string;
  rawSectionLabel: string;
  mappedDepartment: DepartmentKey | null;
  ownerTeamLabel: string | null;
  sortOrder: number;
  rows: KpiRowDTO[];
};

export type KpiBoardSummary = {
  totalRows: number;
  scoredRows: number;
  onTrackRows: number;
  onTrackPct: number;
  atRiskRows: number;
  dueSoonRows: number;
};

export type KpiBoardCoverage = {
  mappedSections: number;
  ownerAssignedSections: number;
  totalSections: number;
  mappedPct: number;
  ownerAssignedPct: number;
};

export type KpiBoardResponse = {
  weekStart: string;
  weekEnd: string;
  source: string;
  lastUpdatedAt: string | null;
  coverage: KpiBoardCoverage;
  sections: KpiSectionDTO[];
  summary: KpiBoardSummary;
  previousWeekSummary?: KpiBoardSummary;
};

export type WeeklyKpiIngestRowInput = {
  rowKey?: string;
  responsibility: string;
  objectiveIndicator?: string | null;
  quantityQuality?: string | null;
  compliance?: string | null;
  dueCommitment?: string | null;
  sortOrder?: number;
};

export type WeeklyKpiIngestSectionInput = {
  sectionKey?: string;
  rawSectionLabel: string;
  mappedDepartment?: DepartmentKey | null;
  ownerTeamLabel?: string | null;
  sortOrder?: number;
  rows: WeeklyKpiIngestRowInput[];
};

export type WeeklyKpiIngestPayload = {
  weekStart?: string;
  weekEnd?: string;
  source?: string;
  sections: WeeklyKpiIngestSectionInput[];
};

export type WeeklyKpiPatchPayload = {
  responsibility?: string;
  objectiveIndicator?: string | null;
  quantityQuality?: string | null;
  compliance?: string | null;
  dueCommitment?: string | null;
};

export type DepartmentKpiPublishResult = {
  department: DepartmentKey;
  weekStart: string;
  sectionKey: string;
  rowCount: number;
  source: string;
};
