"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.AttendanceController = void 0;
const common_1 = require("@nestjs/common");
const permission_request_util_1 = require("../../common/access-control/permission-request.util");
const current_user_decorator_1 = require("../../common/decorators/current-user.decorator");
const zod_validation_pipe_1 = require("../../common/pipes/zod-validation.pipe");
const jwt_guard_1 = require("../owner/guards/jwt.guard");
const attendance_dto_1 = require("./attendance.dto");
const attendance_service_1 = require("./attendance.service");
function extractClientIp(req, fallbackIp) {
    const forwarded = req.headers['x-forwarded-for'];
    if (typeof forwarded === 'string') {
        return forwarded.split(',')[0].trim();
    }
    if (Array.isArray(forwarded) && forwarded.length > 0) {
        return forwarded[0].trim();
    }
    return req.ip || req.socket.remoteAddress || fallbackIp || '127.0.0.1';
}
let AttendanceController = class AttendanceController {
    constructor(service) {
        this.service = service;
    }
    async scanQr(body, owner_id, req, ip, res) {
        const user = req.user;
        const memberId = user?.sub || user?.id;
        const clientIp = extractClientIp(req, ip);
        const result = await this.service.scanQr(memberId, owner_id, body, clientIp);
        return res.status(result.status).json(result);
    }
    async checkIn(body, owner_id, req, ip, res) {
        const user = req.user;
        const memberId = user?.sub || user?.id;
        const clientIp = extractClientIp(req, ip);
        const result = await this.service.checkIn(memberId, owner_id, body, clientIp);
        return res.status(result.status).json(result);
    }
    async checkOut(body, owner_id, req, ip, res) {
        const user = req.user;
        const memberId = user?.sub || user?.id;
        const clientIp = extractClientIp(req, ip);
        const result = await this.service.checkOut(memberId, owner_id, body, clientIp);
        return res.status(result.status).json(result);
    }
    async getPrimaryQr(owner_id, res) {
        const result = await this.service.getPrimaryQrPoint(owner_id);
        return res.status(result.status).json(result);
    }
    async listQrPoints(owner_id, res) {
        const result = await this.service.listQrPoints(owner_id);
        return res.status(result.status).json(result);
    }
    async createQrPoint(body, owner_id, res) {
        const result = await this.service.createQrPoint(owner_id, body);
        return res.status(result.status).json(result);
    }
    async updateQrPoint(id, body, owner_id, res) {
        const result = await this.service.updateQrPoint(owner_id, id, body);
        return res.status(result.status).json(result);
    }
    async deleteQrPoint(id, owner_id, res) {
        const result = await this.service.deleteQrPoint(owner_id, id);
        return res.status(result.status).json(result);
    }
    async createOrUpdate(data, owner_id, req, res) {
        const result = await this.service.createOrUpdateAttendance(data, owner_id, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async getAll(query, owner_id, req, res) {
        const result = await this.service.getAttendances(query, owner_id, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async getToday(owner_id, query, req, res) {
        const result = await this.service.getTodayAttendances(owner_id, query, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async find(attendanceId, owner_id, req, res) {
        const result = await this.service.getAttendanceById(attendanceId, owner_id, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async findByMember(memberId, owner_id, query, req, res) {
        const result = await this.service.getAttendancesByMember(memberId, query, owner_id, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async findByTodayMember(memberId, date, owner_id, req, res) {
        const result = await this.service.getTodayAttendancesByMember(memberId, owner_id, date, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async getReport(query, owner_id, req, res) {
        const result = await this.service.getAttendanceReport(query, owner_id, (0, permission_request_util_1.getPermissionContext)(req.user));
        return res.status(common_1.HttpStatus.OK).json(result);
    }
};
exports.AttendanceController = AttendanceController;
__decorate([
    (0, common_1.Post)('scan-qr'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.scanQrAttendanceSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Ip)()),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "scanQr", null);
__decorate([
    (0, common_1.Post)('check-in'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.teacherCheckInSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Ip)()),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "checkIn", null);
__decorate([
    (0, common_1.Post)('check-out'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.teacherCheckOutSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Ip)()),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "checkOut", null);
__decorate([
    (0, common_1.Get)('qr/primary'),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "getPrimaryQr", null);
__decorate([
    (0, common_1.Get)('qr/list'),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "listQrPoints", null);
__decorate([
    (0, common_1.Post)('qr'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.createQrPointSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "createQrPoint", null);
__decorate([
    (0, common_1.Patch)('qr/:id'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.updateQrPointSchema)),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "updateQrPoint", null);
__decorate([
    (0, common_1.Delete)('qr/:id'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "deleteQrPoint", null);
__decorate([
    (0, common_1.Post)('create-or-update'),
    (0, common_1.Post)('admin/manual'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.adminManualAttendanceSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "createOrUpdate", null);
__decorate([
    (0, common_1.Get)('all'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.attendanceFilterSchema)),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "getAll", null);
__decorate([
    (0, common_1.Get)('today'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.attendanceFilterSchema)),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Query)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "getToday", null);
__decorate([
    (0, common_1.Get)('find/:attendanceId'),
    __param(0, (0, common_1.Param)('attendanceId')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "find", null);
__decorate([
    (0, common_1.Get)('member/:memberId'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.attendanceFilterSchema)),
    __param(0, (0, common_1.Param)('memberId')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Query)()),
    __param(3, (0, common_1.Req)()),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "findByMember", null);
__decorate([
    (0, common_1.Get)('today/:memberId'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.attendanceFilterSchema)),
    __param(0, (0, common_1.Param)('memberId')),
    __param(1, (0, common_1.Query)('date')),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __param(3, (0, common_1.Req)()),
    __param(4, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "findByTodayMember", null);
__decorate([
    (0, common_1.Get)('report'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(attendance_dto_1.attendanceReportFilterSchema)),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Req)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object, Object]),
    __metadata("design:returntype", Promise)
], AttendanceController.prototype, "getReport", null);
exports.AttendanceController = AttendanceController = __decorate([
    (0, common_1.Controller)('attendance'),
    (0, common_1.UseGuards)(jwt_guard_1.JwtAuthGuard),
    __metadata("design:paramtypes", [attendance_service_1.AttendanceService])
], AttendanceController);
//# sourceMappingURL=attendance.controller.js.map