"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.VisitorsController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const client_1 = require("@prisma/client");
const current_user_decorator_1 = require("../common/current-user.decorator");
const require_role_decorator_1 = require("../auth/require-role.decorator");
const feature_catalog_1 = require("../features/feature-catalog");
const require_feature_decorator_1 = require("../features/require-feature.decorator");
const swagger_docs_1 = require("../common/swagger-docs");
const create_visitor_dto_1 = require("./dto/create-visitor.dto");
const create_visitor_pass_dto_1 = require("./dto/create-visitor-pass.dto");
const update_visitor_risk_dto_1 = require("./dto/update-visitor-risk.dto");
const visitors_service_1 = require("./visitors.service");
let VisitorsController = class VisitorsController {
    visitorsService;
    constructor(visitorsService) {
        this.visitorsService = visitorsService;
    }
    findAll(user, estateId, q) {
        return this.visitorsService.findVisitors(user, estateId, q);
    }
    createVisitor(dto, user) {
        return this.visitorsService.createVisitor(dto, user);
    }
    findPasses(user, estateId) {
        return this.visitorsService.findPasses(user, estateId);
    }
    createPass(dto, user) {
        return this.visitorsService.createPass(dto, user);
    }
    revokePass(id, user) {
        return this.visitorsService.revokePass(id, user);
    }
    getHistory(id, user) {
        return this.visitorsService.getVisitorHistory(id, user);
    }
    updateRisk(id, dto, user) {
        return this.visitorsService.updateRisk(id, dto, user);
    }
};
exports.VisitorsController = VisitorsController;
__decorate([
    (0, common_1.Get)(),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.SECURITY_SUPERVISOR, client_1.UserRole.GUARD, client_1.UserRole.RESIDENT),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITORS_VIEW),
    (0, swagger_1.ApiOperation)({
        summary: 'List or search estate visitors',
        description: 'Returns visitor records scoped to the estate. Use q to search name, phone, or vehicle plate.'
    }),
    (0, swagger_1.ApiQuery)({ name: 'estateId', required: false, example: swagger_docs_1.ESTATE_ID_EXAMPLE, description: 'Estate scope. Required for SUPER_ADMIN.' }),
    (0, swagger_1.ApiQuery)({ name: 'q', required: false, example: 'John', description: 'Optional visitor search text.' }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Visitors returned.',
        schema: {
            example: [
                {
                    id: swagger_docs_1.VISITOR_ID_EXAMPLE,
                    estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                    name: 'Ade Okafor',
                    phone: '+2348111111111',
                    vehiclePlate: 'LND-123-AB',
                    photoUrl: null
                }
            ]
        }
    }),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Query)('estateId')),
    __param(2, (0, common_1.Query)('q')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "findAll", null);
__decorate([
    (0, common_1.Post)(),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.RESIDENT),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITORS_CREATE),
    (0, swagger_1.ApiOperation)({
        summary: 'Create visitor',
        description: 'Creates an estate-scoped visitor record that can later receive visitor passes.'
    }),
    (0, swagger_1.ApiBody)({ type: create_visitor_dto_1.CreateVisitorDto }),
    (0, swagger_1.ApiResponse)({
        status: 201,
        description: 'Visitor created.',
        schema: {
            example: {
                id: swagger_docs_1.VISITOR_ID_EXAMPLE,
                estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                name: 'Ade Okafor',
                phone: '+2348111111111',
                vehiclePlate: 'LND-123-AB',
                photoUrl: null
            }
        }
    }),
    (0, swagger_docs_1.ApiValidationError)(),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [create_visitor_dto_1.CreateVisitorDto, Object]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "createVisitor", null);
__decorate([
    (0, common_1.Get)('passes'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.SECURITY_SUPERVISOR, client_1.UserRole.GUARD, client_1.UserRole.RESIDENT),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITOR_PASSES_VIEW),
    (0, swagger_1.ApiOperation)({
        summary: 'List visitor passes',
        description: 'Returns recent visitor passes for an estate, including visitor and host resident context.'
    }),
    (0, swagger_1.ApiQuery)({ name: 'estateId', required: false, example: swagger_docs_1.ESTATE_ID_EXAMPLE, description: 'Estate scope. Required for SUPER_ADMIN.' }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Visitor passes returned.',
        schema: {
            example: [
                {
                    id: swagger_docs_1.VISITOR_PASS_ID_EXAMPLE,
                    estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                    visitorId: swagger_docs_1.VISITOR_ID_EXAMPLE,
                    hostResidentId: swagger_docs_1.RESIDENT_ID_EXAMPLE,
                    visitorCode: 'ABC12345',
                    status: 'ACTIVE',
                    passType: 'SINGLE_USE',
                    maxEntries: 1,
                    entryCount: 0
                }
            ]
        }
    }),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Query)('estateId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "findPasses", null);
__decorate([
    (0, common_1.Post)('passes'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.RESIDENT),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITOR_PASSES_CREATE),
    (0, swagger_1.ApiOperation)({
        summary: 'Create visitor pass',
        description: 'Creates a visitor pass with a QR token and manual visitor code. The visitor and host resident must both belong to the estate.'
    }),
    (0, swagger_1.ApiBody)({ type: create_visitor_pass_dto_1.CreateVisitorPassDto }),
    (0, swagger_1.ApiResponse)({
        status: 201,
        description: 'Visitor pass created with QR payload.',
        schema: {
            example: {
                id: swagger_docs_1.VISITOR_PASS_ID_EXAMPLE,
                estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                visitorId: swagger_docs_1.VISITOR_ID_EXAMPLE,
                hostResidentId: swagger_docs_1.RESIDENT_ID_EXAMPLE,
                visitorCode: 'ABC12345',
                qrToken: 'QRtokenExample123',
                qrPayload: { token: 'QRtokenExample123' },
                passType: 'SINGLE_USE',
                status: 'ACTIVE',
                maxEntries: 1,
                entryCount: 0
            }
        }
    }),
    (0, swagger_docs_1.ApiValidationError)(),
    (0, swagger_docs_1.ApiNotFoundError)('Host resident or visitor'),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [create_visitor_pass_dto_1.CreateVisitorPassDto, Object]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "createPass", null);
__decorate([
    (0, common_1.Post)('passes/:id/revoke'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.SECURITY_SUPERVISOR),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITOR_PASSES_REVOKE),
    (0, swagger_1.ApiOperation)({
        summary: 'Revoke visitor pass',
        description: 'Marks an existing visitor pass as REVOKED and records revokedAt.'
    }),
    (0, swagger_1.ApiParam)({ name: 'id', example: swagger_docs_1.VISITOR_PASS_ID_EXAMPLE, description: 'Visitor pass ID to revoke.' }),
    (0, swagger_1.ApiResponse)({
        status: 201,
        description: 'Visitor pass revoked.',
        schema: {
            example: {
                id: swagger_docs_1.VISITOR_PASS_ID_EXAMPLE,
                estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                status: 'REVOKED',
                revokedAt: '2026-05-16T12:00:00.000Z'
            }
        }
    }),
    (0, swagger_1.ApiNotFoundResponse)({
        description: 'Visitor pass was not found.',
        schema: { example: { statusCode: 404, message: 'Visitor pass not found', error: 'Not Found' } }
    }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "revokePass", null);
__decorate([
    (0, common_1.Get)(':id/history'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.SECURITY_SUPERVISOR, client_1.UserRole.GUARD, client_1.UserRole.RESIDENT),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITOR_INTELLIGENCE_VIEW),
    (0, swagger_1.ApiOperation)({
        summary: 'View visitor intelligence history',
        description: 'Returns visit sessions, gates used, residents visited, exit reliability, and risk history for one visitor.'
    }),
    (0, swagger_1.ApiParam)({ name: 'id', example: swagger_docs_1.VISITOR_ID_EXAMPLE }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Visitor history returned.' }),
    (0, swagger_docs_1.ApiNotFoundError)('Visitor'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "getHistory", null);
__decorate([
    (0, common_1.Patch)(':id/risk'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.ESTATE_ADMIN, client_1.UserRole.SECURITY_SUPERVISOR),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.VISITOR_INTELLIGENCE_MANAGE),
    (0, swagger_1.ApiOperation)({
        summary: 'Update visitor risk',
        description: 'Flags, restricts, blacklists, scores, or annotates a visitor record for estate-internal risk tracking.'
    }),
    (0, swagger_1.ApiParam)({ name: 'id', example: swagger_docs_1.VISITOR_ID_EXAMPLE }),
    (0, swagger_1.ApiBody)({ type: update_visitor_risk_dto_1.UpdateVisitorRiskDto }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Visitor risk updated.' }),
    (0, swagger_docs_1.ApiNotFoundError)('Visitor'),
    (0, swagger_docs_1.ApiValidationError)(),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, update_visitor_risk_dto_1.UpdateVisitorRiskDto, Object]),
    __metadata("design:returntype", void 0)
], VisitorsController.prototype, "updateRisk", null);
exports.VisitorsController = VisitorsController = __decorate([
    (0, swagger_1.ApiTags)('Visitors'),
    (0, swagger_docs_1.ApiProtectedEndpoint)(),
    (0, common_1.Controller)({ path: 'visitors', version: '1' }),
    __metadata("design:paramtypes", [visitors_service_1.VisitorsService])
], VisitorsController);
//# sourceMappingURL=visitors.controller.js.map