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

export type CaptureModeDTO = "manual" | "auto" | "hybrid";

export type CaptureStatus = "pending" | "in_progress" | "captured" | "stale" | "error";

export type CaptureAutomationRunStatusDTO = "queued" | "running" | "success" | "failed";

export type CaptureEvidenceKindDTO = "photo" | "document" | "link";

export type CaptureViewerContext = {
  userId: string;
  role?: UserRole;
  department?: DepartmentKey | null;
};

export type CaptureRowDTO = {
  id: string;
  weekStart: string;
  weekEnd: string;
  department: DepartmentKey;
  sectionKey: string;
  sectionLabel: string;
  rowKey: string;
  responsibility: string;
  ownerTeamLabel: string | null;
  objectiveIndicator: string | null;
  quantityQuality: string | null;
  compliance: string | null;
  compliancePct: number | null;
  dueCommitment: string | null;
  dueDate: string | null;
  kpiStatus: "on_track" | "watch" | "risk" | "no_score";
  captureMode: CaptureModeDTO;
  automationSource: string | null;
  ownerUserId: string | null;
  ownerUserName: string | null;
  freshnessSlaHours: number;
  captureNote: string | null;
  lastCapturedAt: string | null;
  lastCapturedById: string | null;
  lastCapturedByName: string | null;
  lastAutomationAt: string | null;
  lastEvidenceAt: string | null;
  evidenceCount: number;
  status: CaptureStatus;
  canEdit: boolean;
  canEditCore: boolean;
};

export type CaptureEvidenceDTO = {
  id: string;
  department: DepartmentKey;
  weekStart: string;
  sectionKey: string;
  rowKey: string;
  rowId: string | null;
  initiativeId: string | null;
  initiativeName: string | null;
  taskId: string | null;
  taskTitle: string | null;
  uploadedById: string;
  uploaderName: string;
  kind: CaptureEvidenceKindDTO;
  title: string;
  note: string | null;
  url: string | null;
  hasFile: boolean;
  mimeType: string | null;
  sizeBytes: number | null;
  createdAt: string;
  updatedAt: string;
};

export type CaptureAutomationRunDTO = {
  id: string;
  department: DepartmentKey;
  weekStart: string;
  source: string;
  status: CaptureAutomationRunStatusDTO;
  rowsTouched: number;
  errorMessage: string | null;
  triggeredById: string | null;
  triggeredByName: string | null;
  startedAt: string;
  completedAt: string | null;
};

export type CaptureAssigneeDTO = {
  id: string;
  name: string;
  department: DepartmentKey;
};

export type CaptureBoardSummary = {
  totalRows: number;
  capturedRows: number;
  pendingRows: number;
  inProgressRows: number;
  staleRows: number;
  errorRows: number;
  atRiskRows: number;
  capturePct: number;
};

export type CaptureBoardResponse = {
  weekStart: string;
  weekEnd: string;
  department: DepartmentKey;
  summary: CaptureBoardSummary;
  rows: CaptureRowDTO[];
  evidence: CaptureEvidenceDTO[];
  automationRuns: CaptureAutomationRunDTO[];
};

export type OwnerCaptureRollupDepartment = {
  department: DepartmentKey;
  label: string;
  totalRows: number;
  capturedRows: number;
  pendingRows: number;
  inProgressRows: number;
  staleRows: number;
  errorRows: number;
  atRiskRows: number;
  capturePct: number;
  automationHealth: "healthy" | "attention" | "down";
  lastAutomationAt: string | null;
};

export type OwnerCaptureRollupDTO = {
  weekStart: string;
  weekEnd: string;
  departments: OwnerCaptureRollupDepartment[];
};

export type CaptureBoardFilters = {
  initiativeId?: string;
  taskId?: string;
  uploadedById?: string;
  from?: string;
  to?: string;
  query?: string;
};
