import { Request, Response } from "express";
import { asyncHandler } from "@/core/asyncHandler";
import {
    listLogisticsRoutes,
    getLogisticsRouteById,
    createLogisticsRoute,
    updateLogisticsRoute,
    deleteLogisticsRoute,
} from "./logisticsRoute.service";

export const getLogisticsRoutes = asyncHandler(async (req: Request, res: Response) => {
    const shopId = req.shop!.id;
    const routes = await listLogisticsRoutes(shopId);
    res.json(routes);
});

export const getLogisticsRoute = asyncHandler(async (req: Request, res: Response) => {
    const shopId = req.shop!.id;
    const id = req.params.id as string;
    const route = await getLogisticsRouteById(shopId, id);
    res.json(route);
});

export const postLogisticsRoute = asyncHandler(async (req: Request, res: Response) => {
    const shopId = req.shop!.id;
    const actorId = req.auth?.sub ?? null;
    const route = await createLogisticsRoute(shopId, actorId, req.body);
    res.status(201).json(route);
});

export const patchLogisticsRoute = asyncHandler(async (req: Request, res: Response) => {
    const shopId = req.shop!.id;
    const actorId = req.auth?.sub ?? null;
    const id = req.params.id as string;
    const route = await updateLogisticsRoute(shopId, id, actorId, req.body);
    res.json(route);
});

export const deleteLogisticsRouteHandler = asyncHandler(async (req: Request, res: Response) => {
    const shopId = req.shop!.id;
    const actorId = req.auth?.sub ?? null;
    const id = req.params.id as string;
    await deleteLogisticsRoute(shopId, id, actorId);
    res.status(204).send();
});

