"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransportController = void 0;
const common_1 = require("@nestjs/common");
const throttler_1 = require("@nestjs/throttler");
const transport_service_1 = require("./transport.service");
const create_route_dto_1 = require("./dto/create-route.dto");
const update_route_dto_1 = require("./dto/update-route.dto");
const assign_transport_dto_1 = require("./dto/assign-transport.dto");
const record_transport_payment_dto_1 = require("./dto/record-transport-payment.dto");
const filter_transport_assignments_dto_1 = require("./dto/filter-transport-assignments.dto");
const verify_transport_payment_dto_1 = require("./dto/verify-transport-payment.dto");
const toggle_transport_dto_1 = require("./dto/toggle-transport.dto");
const mongoose_1 = require("mongoose");
const jwt_auth_guard_1 = require("../common/guards/jwt-auth.guard");
const roles_guard_1 = require("../common/guards/roles.guard");
const roles_decorator_1 = require("../common/decorators/roles.decorator");
const role_enum_1 = require("../common/roles/role.enum");
const current_user_decorator_1 = require("../common/decorators/current-user.decorator");
const public_decorator_1 = require("../common/decorators/public.decorator");
const mongo_id_pipe_1 = require("../common/pipes/mongo-id.pipe");
const request_context_1 = require("../common/context/request-context");
let TransportController = class TransportController {
    transportService;
    constructor(transportService) {
        this.transportService = transportService;
    }
    createRoute(dto, ctx) {
        return this.transportService.createRoute(dto, ctx);
    }
    getRoutes(academicYearId, activeOnly) {
        return this.transportService.getRoutes(academicYearId, activeOnly === 'true');
    }
    getRoute(id) {
        return this.transportService.getRoute(id);
    }
    updateRoute(id, dto, ctx) {
        return this.transportService.updateRoute(id, dto, ctx);
    }
    deleteRoute(id, ctx) {
        return this.transportService.deleteRoute(id, ctx);
    }
    assignStudents(dto, ctx) {
        return this.transportService.assignStudents(dto, ctx);
    }
    syncAssignments(routeId) {
        if (routeId && !(0, mongoose_1.isValidObjectId)(routeId)) {
            throw new common_1.BadRequestException('Invalid route ID');
        }
        return this.transportService.syncAssignments(routeId);
    }
    getAssignments(filter) {
        return this.transportService.getAssignments(filter);
    }
    recordOfflinePayment(id, dto, adminId) {
        return this.transportService.recordOfflinePayment(id, dto, adminId);
    }
    toggleTransport(studentId, dto, ctx) {
        return this.transportService.toggleTransport(studentId, dto, ctx);
    }
    removeTransport(studentId, routeId, academicYearId, ctx) {
        if (!routeId || !(0, mongoose_1.isValidObjectId)(routeId)) {
            throw new common_1.BadRequestException('Valid routeId query parameter is required');
        }
        if (!academicYearId || !(0, mongoose_1.isValidObjectId)(academicYearId)) {
            throw new common_1.BadRequestException('Valid academicYearId query parameter is required');
        }
        return this.transportService.removeTransport(studentId, routeId, academicYearId, ctx);
    }
    getStudentTransportStatus(studentId, academicYearId) {
        return this.transportService.getStudentTransportStatus(studentId, academicYearId);
    }
    getStudentDashboardAdmin(studentId, academicYearId) {
        return this.transportService.getStudentDashboard(studentId, academicYearId);
    }
    getMyDashboard(studentId, academicYearId) {
        return this.transportService.getStudentDashboard(studentId, academicYearId);
    }
    createOrder(id, studentId) {
        return this.transportService.createRazorpayOrder(id, studentId);
    }
    verifyPayment(id, studentId, body) {
        return this.transportService.verifyRazorpayPayment(id, studentId, body);
    }
    async verifyReceipt(receipt) {
        if (!receipt || typeof receipt !== 'string' || receipt.length > 50) {
            throw new common_1.BadRequestException('Invalid receipt number');
        }
        const result = await this.transportService.verifyReceipt(receipt.trim());
        if (!result)
            throw new common_1.NotFoundException('Receipt not found');
        return result;
    }
    handleWebhook(req) {
        const signature = req.headers['x-razorpay-signature'];
        if (!req.rawBody) {
            throw new common_1.BadRequestException('Raw body not available — ensure rawBody: true is set in NestJS bootstrap.');
        }
        return this.transportService.handleWebhook(signature, req.rawBody, req.body);
    }
};
exports.TransportController = TransportController;
__decorate([
    (0, common_1.Post)('routes'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 10, ttl: 60000 } }),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [create_route_dto_1.CreateRouteDto, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "createRoute", null);
__decorate([
    (0, common_1.Get)('routes'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Query)('academicYearId')),
    __param(1, (0, common_1.Query)('activeOnly')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getRoutes", null);
__decorate([
    (0, common_1.Get)('routes/:id'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getRoute", null);
__decorate([
    (0, common_1.Put)('routes/:id'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, update_route_dto_1.UpdateRouteDto, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "updateRoute", null);
__decorate([
    (0, common_1.Delete)('routes/:id'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "deleteRoute", null);
__decorate([
    (0, common_1.Post)('assign'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 5, ttl: 60000 } }),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [assign_transport_dto_1.AssignTransportDto, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "assignStudents", null);
__decorate([
    (0, common_1.Post)('sync-assignments'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 2, ttl: 60000 } }),
    __param(0, (0, common_1.Query)('routeId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "syncAssignments", null);
__decorate([
    (0, common_1.Get)('assignments'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Query)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [filter_transport_assignments_dto_1.FilterTransportAssignmentsDto]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getAssignments", null);
__decorate([
    (0, common_1.Post)('assignments/:id/record-payment'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 20, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, record_transport_payment_dto_1.RecordTransportPaymentDto, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "recordOfflinePayment", null);
__decorate([
    (0, common_1.Patch)('students/:studentId/toggle'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 20, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('studentId', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, toggle_transport_dto_1.ToggleTransportDto, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "toggleTransport", null);
__decorate([
    (0, common_1.Delete)('students/:studentId/remove'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 10, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('studentId', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Query)('routeId')),
    __param(2, (0, common_1.Query)('academicYearId')),
    __param(3, (0, request_context_1.ReqCtx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "removeTransport", null);
__decorate([
    (0, common_1.Get)('students/:studentId/status'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    (0, throttler_1.Throttle)({ default: { limit: 30, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('studentId', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Query)('academicYearId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getStudentTransportStatus", null);
__decorate([
    (0, common_1.Get)('student/:studentId/dashboard'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.ADMIN),
    __param(0, (0, common_1.Param)('studentId', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, common_1.Query)('academicYearId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getStudentDashboardAdmin", null);
__decorate([
    (0, common_1.Get)('my-dashboard'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.STUDENT),
    __param(0, (0, current_user_decorator_1.CurrentUser)('id')),
    __param(1, (0, common_1.Query)('academicYearId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "getMyDashboard", null);
__decorate([
    (0, common_1.Post)('assignments/:id/create-order'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.STUDENT),
    (0, throttler_1.Throttle)({ default: { limit: 5, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, current_user_decorator_1.CurrentUser)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "createOrder", null);
__decorate([
    (0, common_1.Post)('assignments/:id/verify-payment'),
    (0, roles_decorator_1.Roles)(role_enum_1.Role.STUDENT),
    (0, throttler_1.Throttle)({ default: { limit: 10, ttl: 60000 } }),
    __param(0, (0, common_1.Param)('id', mongo_id_pipe_1.MongoIdPipe)),
    __param(1, (0, current_user_decorator_1.CurrentUser)('id')),
    __param(2, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, verify_transport_payment_dto_1.VerifyTransportPaymentDto]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "verifyPayment", null);
__decorate([
    (0, common_1.Get)('verify-receipt'),
    (0, public_decorator_1.Public)(),
    (0, throttler_1.Throttle)({ default: { limit: 10, ttl: 60000 } }),
    __param(0, (0, common_1.Query)('receipt')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], TransportController.prototype, "verifyReceipt", null);
__decorate([
    (0, common_1.Post)('webhook'),
    (0, public_decorator_1.Public)(),
    (0, throttler_1.Throttle)({ default: { limit: 60, ttl: 60000 } }),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], TransportController.prototype, "handleWebhook", null);
exports.TransportController = TransportController = __decorate([
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, roles_guard_1.RolesGuard),
    (0, common_1.Controller)('transport'),
    __metadata("design:paramtypes", [transport_service_1.TransportService])
], TransportController);
//# sourceMappingURL=transport.controller.js.map