import { Module } from ".";
import { Type, User, userTypeLabels, userTypeLabelsShort } from "./user";

export interface ApprovalWorkflow {
    _id?: string,
    name: string,
    entityId: string,
    requester: User["id"],
    startDate: number,
    modules: Module[],
    examId?: string,
    status: ApprovalWorkflowStatus,
    steps: WorkflowStep[],
}

export interface EditableApprovalWorkflow extends Omit<ApprovalWorkflow, "_id" | "steps"> {
    id: string,
    steps: EditableWorkflowStep[],
}

export type StepType = "form-intake" | "approval-by";
export const StepTypeLabel: Record<StepType, string> = {
	"form-intake": "Form Intake",
    "approval-by": "Approval",
};

export interface WorkflowStep {
    stepType: StepType,
    stepNumber: number,
    completed: boolean,
    rejected?: boolean,
    completedBy?: User["id"],
    completedDate?: number,
    assignees: (User["id"])[];
    firstStep?: boolean,
    finalStep?: boolean,
    selected?: boolean,
    comments?: string,
    examChanges?: string[],
    onClick?: React.MouseEventHandler<HTMLDivElement>
}

export interface EditableWorkflowStep {
    key: number,
    stepType: StepType,
    stepNumber: number,
    completed: boolean,
    rejected?: boolean,
    completedBy?: User["id"],
    completedDate?: number,
    assignees: (User["id"] | null | undefined)[]; // bit of an hack, but allowing null or undefined values allows us to match one to one the select input components with the assignees array. And since select inputs allow undefined or null values, it is allowed here too, but must validate required input before form submission
    firstStep: boolean,
    finalStep?: boolean,
    onDelete?: () => void;
}

export function getUserTypeLabel(type: Type | undefined): string {
    if (type) return userTypeLabels[type];
    return '';
}
export function getUserTypeLabelShort(type: Type | undefined): string {
    if (type) return userTypeLabelsShort[type];
    return '';
}

export type ApprovalWorkflowStatus = "approved" | "pending" | "rejected";
export const ApprovalWorkflowStatusLabel: Record<ApprovalWorkflowStatus, string> = {
	approved: "Approved",
    pending: "Pending",
    rejected: "Rejected",
};
