import { api } from "@/lib/api/client";

export type HrFlowRequestType =
	| "confirmation"
	| "promotion"
	| "transfer"
	| "demotion"
	| "resignation"
	| "disciplinary";

export type HrFlowRequestStatus =
	| "pending"
	| "approved"
	| "rejected"
	| "cancelled";

export type ExitType = "resignation" | "termination" | "retirement";

export type HrFlowRequest = {
	id: string;
	employeeId: string;
	employeeName: string;
	employeeCode: string;
	type: HrFlowRequestType;
	status: HrFlowRequestStatus;
	effectiveDate: string;
	reason: string;
	payload: Record<string, unknown>;
	approverId: string | null;
	approverEmail: string | null;
	decidedAt: string | null;
	decisionNote: string | null;
	requestedByUserId: string | null;
	requestedByEmail: string | null;
	createdAt: string;
	updatedAt: string;
};

export type EmployeeTimelineEvent = {
	id: string;
	employeeId: string;
	type: HrFlowRequestType;
	title: string;
	summary: string;
	effectiveDate: string;
	metadata: Record<string, unknown> | null;
	sourceRequestId: string | null;
	createdByEmail: string | null;
	createdAt: string;
};

export function getHrFlowRequests(scope: "mine" | "pending" | "all" = "mine") {
	return api.get<{ items: HrFlowRequest[] }>(
		`/hr-flow/requests?scope=${scope}`,
	);
}

export function getHrFlowRequest(id: string) {
	return api.get<HrFlowRequest>(`/hr-flow/requests/${id}`);
}

export function getPendingHrFlowCount() {
	return api.get<{ count: number }>("/hr-flow/requests/pending-count");
}

export function createHrFlowRequest(data: {
	employeeId?: string;
	type: HrFlowRequestType;
	effectiveDate: string;
	reason?: string;
	payload: Record<string, unknown>;
}) {
	return api.post<HrFlowRequest>("/hr-flow/requests", data);
}

export function approveHrFlowRequest(id: string) {
	return api.patch<HrFlowRequest>(`/hr-flow/requests/${id}/approve`, {});
}

export function rejectHrFlowRequest(id: string, decisionNote?: string) {
	return api.patch<HrFlowRequest>(`/hr-flow/requests/${id}/reject`, {
		decisionNote,
	});
}

export function cancelHrFlowRequest(id: string) {
	return api.patch<HrFlowRequest>(`/hr-flow/requests/${id}/cancel`, {});
}

export function getEmployeeTimeline(employeeId: string) {
	return api.get<{ items: EmployeeTimelineEvent[] }>(
		`/hr-flow/timeline/${employeeId}`,
	);
}
