"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleCarrierWebhook = exports.addTrackingEvent = exports.updateShipment = exports.getCarriers = exports.trackPublicOrder = void 0;
const tracking_service_1 = require("../services/tracking.service");
const response_utils_1 = require("../utils/response.utils");
// 1. Public Track Endpoint (No Auth required)
const trackPublicOrder = async (req, res) => {
    try {
        const query = (req.params.query || req.query.q || req.body.query);
        if (!query) {
            return (0, response_utils_1.sendError)(res, 'Please provide an Order Number or Tracking Number', null, 400);
        }
        const trackingData = await tracking_service_1.TrackingService.trackPublic(query);
        return (0, response_utils_1.sendSuccess)(res, 'Tracking details retrieved successfully', trackingData);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, error.message || 'Tracking information not found', null, 404);
    }
};
exports.trackPublicOrder = trackPublicOrder;
// 2. Get Supported Carriers List
const getCarriers = async (_req, res) => {
    try {
        const carriers = tracking_service_1.TrackingService.getSupportedCarriers();
        return (0, response_utils_1.sendSuccess)(res, 'Supported carriers retrieved', { carriers });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to get carrier list', error.message, 500);
    }
};
exports.getCarriers = getCarriers;
// 3. Create or Update Shipment (Admin Only)
const updateShipment = async (req, res) => {
    try {
        const { orderId, trackingNumber, carrier, carrierSlug, carrierTrackingUrl, status, currentLocation, estimatedDelivery, shippedAt } = req.body;
        const shipment = await tracking_service_1.TrackingService.createOrUpdateShipment({
            orderId,
            trackingNumber,
            carrier,
            carrierSlug,
            carrierTrackingUrl,
            status,
            currentLocation,
            estimatedDelivery: estimatedDelivery ? new Date(estimatedDelivery) : undefined,
            shippedAt: shippedAt ? new Date(shippedAt) : undefined,
        });
        return (0, response_utils_1.sendSuccess)(res, 'Shipment & tracking number assigned successfully', { shipment });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update shipment', error.message, 500);
    }
};
exports.updateShipment = updateShipment;
// 4. Add Manual Tracking Event (Admin Only)
const addTrackingEvent = async (req, res) => {
    try {
        const { shipmentId, status, title, description, location, timestamp, source } = req.body;
        const event = await tracking_service_1.TrackingService.addTrackingEvent({
            shipmentId,
            status,
            title,
            description,
            location,
            timestamp: timestamp ? new Date(timestamp) : undefined,
            source: source || 'ADMIN',
        });
        return (0, response_utils_1.sendSuccess)(res, 'Tracking event added to timeline', { event });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to add tracking event', error.message, 500);
    }
};
exports.addTrackingEvent = addTrackingEvent;
// 5. Carrier Webhook Receiver (Delhivery, Shiprocket, BlueDart, DHL, IndiaPost)
const handleCarrierWebhook = async (req, res) => {
    try {
        const carrierSlug = req.params.carrier;
        const payload = req.body;
        if (!carrierSlug) {
            return (0, response_utils_1.sendError)(res, 'Carrier identifier missing in webhook route', null, 400);
        }
        const result = await tracking_service_1.TrackingService.processCarrierWebhook(carrierSlug, payload);
        return (0, response_utils_1.sendSuccess)(res, 'Carrier webhook processed successfully', result);
    }
    catch (error) {
        // Return 200/400 gracefully so webhook retries or logs don't cascade crash
        return (0, response_utils_1.sendError)(res, `Webhook processing error: ${error.message}`, null, 400);
    }
};
exports.handleCarrierWebhook = handleCarrierWebhook;
//# sourceMappingURL=tracking.controller.js.map