import type { FastifyRequest, FastifyReply } from 'fastify';
import * as orderService from '../services/order.service.js';

export async function listHandler(request: FastifyRequest, _reply: FastifyReply) {
  const query = request.query as Record<string, unknown>;
  return orderService.list(query);
}

export async function statsHandler(_request: FastifyRequest, _reply: FastifyReply) {
  return orderService.getOrderStats();
}

export async function nextNoHandler(_request: FastifyRequest, _reply: FastifyReply) {
  const nextOrderNo = await orderService.getNextOrderNo();
  return { nextOrderNo };
}

export async function getHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  return orderService.get(id);
}

export async function createHandler(request: FastifyRequest, _reply: FastifyReply) {
  const data = request.body as Record<string, unknown>;
  return orderService.create(data, request.user.id);
}

export async function updateHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const data = request.body as Record<string, unknown>;
  return orderService.updateOrder(id, data, request.user.id);
}

export async function deleteHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  await orderService.remove(id, request.user.id);
  return { ok: true };
}

export async function stageHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id, productId } = request.params as { id: string; productId: string };
  const { completed_stages, remarks } = request.body as { completed_stages: number; remarks?: string };
  return orderService.advanceStage(id, productId, completed_stages, remarks ?? null, request.user.id, request.user.permissions);
}

export async function joNoHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id, productId } = request.params as { id: string; productId: string };
  const { jo_no } = request.body as { jo_no: string };
  return orderService.updateJoNo(id, productId, jo_no, request.user.id);
}

export async function dispatchHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const body = request.body as {
    products: { order_product_id: string; quantity: number; weight?: number }[]; dispatch_date: string;
    dispatched_by_employee_id?: string; dispatched_by_name?: string; received_by_name?: string;
  };
  return orderService.dispatchProducts(id, body.products, body.dispatch_date, body.dispatched_by_employee_id, request.user.id, body.dispatched_by_name, body.received_by_name);
}

export async function cancelHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const body = request.body as { products: { order_product_id: string; quantity: number; weight: number }[]; cancel_date: string; cancelled_by_employee_id: string; reason: string };
  return orderService.cancelProducts(id, body.products, body.cancel_date, body.cancelled_by_employee_id, body.reason, request.user.id);
}

export async function getCommentsHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  return orderService.getComments(id);
}

export async function addCommentHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const { comment } = request.body as { comment: string };
  return orderService.addComment(id, comment, request.user.id);
}

export async function getDispatchHistoryHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id, productId } = request.params as { id: string; productId: string };
  return orderService.getDispatchHistory(id, productId);
}

export async function getTimelineHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  return orderService.getOrderTimeline(id);
}

export async function editDispatchHistoryHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id, productId, historyId } = request.params as { id: string; productId: string; historyId: string };
  const body = request.body as { quantity: number; weight: number; dispatch_date: string; dispatched_by_employee_id?: string; dispatched_by_name?: string; received_by_name?: string };
  return orderService.editDispatchHistory(
    id,
    productId,
    historyId,
    {
      quantity: body.quantity, weight: body.weight, dispatchDate: body.dispatch_date,
      dispatchedByEmployeeId: body.dispatched_by_employee_id, dispatchedByName: body.dispatched_by_name,
      receivedByName: body.received_by_name,
    },
    request.user.id,
  );
}

export async function revertDispatchHistoryHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id, productId, historyId } = request.params as { id: string; productId: string; historyId: string };
  return orderService.revertDispatchHistory(id, productId, historyId, request.user.id);
}
