"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.AIActionsController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const ai_actions_service_1 = require("./ai-actions.service");
const core_entities_1 = require("../core-entities");
const ai_action_batch_service_1 = require("./ai-action-batch.service");
let AIActionsController = class AIActionsController {
    constructor(aiActionsService, aiActionBatchService) {
        this.aiActionsService = aiActionsService;
        this.aiActionBatchService = aiActionBatchService;
    }
    async findAllByInputId(inputId) {
        return this.aiActionsService.findAllByInputId(inputId);
    }
    async findOne(actionId) {
        return this.aiActionsService.findOne(actionId);
    }
    async approveAction(actionId) {
        return this.aiActionsService.approveAction(actionId);
    }
    async discardAction(actionId) {
        return this.aiActionsService.discardAction(actionId);
    }
    async confirmKeepAction(actionId) {
        return this.aiActionsService.confirmKeepAction(actionId);
    }
    async revertAction(actionId) {
        return this.aiActionsService.revertAction(actionId);
    }
    async executeAction(actionId) {
        return this.aiActionsService.reExecuteAction(actionId);
    }
    async applyApprovedActions(inputId, response) {
        const results = await this.aiActionBatchService.applyApprovedActions(inputId);
        const allSucceeded = results.every((r) => r.status === ai_actions_service_1.AIActionStatus.CONFIRMED_KEPT);
        const someFailed = results.some((r) => r.status === ai_actions_service_1.AIActionStatus.EXECUTION_FAILED);
        if (results.length === 0) {
            response.status(common_1.HttpStatus.OK).json({
                message: 'No actions were in "approved_for_apply" state to process.',
                results: [],
            });
            return;
        }
        if (allSucceeded) {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
        else if (someFailed) {
            response.status(207).json({ results });
        }
        else {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
    }
    async discardAllActions(inputId, response) {
        const results = await this.aiActionBatchService.discardAllActionsForInput(inputId);
        if (results.length === 0) {
            response.status(common_1.HttpStatus.OK).json({
                message: 'No actions found in "proposed" or "approved_for_apply" state to discard for this input.',
                results: [],
            });
        }
        else {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
    }
    async confirmAllActions(inputId, response) {
        const results = await this.aiActionBatchService.confirmAllAppliedActionsForInput(inputId);
        if (results.length === 0) {
            response.status(common_1.HttpStatus.OK).json({
                message: 'No actions found in "applied_pending_review" state to confirm for this input.',
                results: [],
            });
        }
        else {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
    }
    async revertAllActions(inputId, response) {
        const results = await this.aiActionBatchService.revertAllAppliedActionsForInput(inputId);
        if (results.length === 0) {
            response.status(common_1.HttpStatus.OK).json({
                message: 'No actions in "applied_pending_review" state to revert for this input.',
                results: [],
            });
            return;
        }
        const allRevertedSuccessfully = results.every((r) => r.status === ai_actions_service_1.AIActionStatus.CONFIRMED_REVERTED);
        const someRevertsFailed = results.some((r) => r.status === ai_actions_service_1.AIActionStatus.REVERT_FAILED);
        if (allRevertedSuccessfully) {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
        else if (someRevertsFailed) {
            response.status(207).json({ results });
        }
        else {
            response.status(common_1.HttpStatus.OK).json({ results });
        }
    }
};
exports.AIActionsController = AIActionsController;
__decorate([
    (0, common_1.Get)('inputs/:inputId/actions'),
    (0, swagger_1.ApiOperation)({ summary: 'List all AI actions for an input' }),
    (0, swagger_1.ApiParam)({ name: 'inputId', description: 'Input UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'List of AI actions',
        type: [core_entities_1.AIAction],
    }),
    __param(0, (0, common_1.Param)('inputId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "findAllByInputId", null);
__decorate([
    (0, common_1.Get)('actions/:actionId'),
    (0, swagger_1.ApiOperation)({ summary: 'Get a single AI action by ID' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Action found', type: core_entities_1.AIAction }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "findOne", null);
__decorate([
    (0, common_1.Post)('actions/:actionId/approve'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({ summary: 'Mark action as approved for execution' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Action approved', type: core_entities_1.AIAction }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "approveAction", null);
__decorate([
    (0, common_1.Post)('actions/:actionId/discard'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({ summary: 'Discard a proposed action' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Action discarded', type: core_entities_1.AIAction }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "discardAction", null);
__decorate([
    (0, common_1.Post)('actions/:actionId/confirm-keep'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({ summary: 'Confirm to keep an applied action (permanent)' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Action confirmed as kept',
        type: core_entities_1.AIAction,
    }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "confirmKeepAction", null);
__decorate([
    (0, common_1.Post)('actions/:actionId/revert'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({ summary: 'Revert an applied action to original state' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Action reverted', type: core_entities_1.AIAction }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "revertAction", null);
__decorate([
    (0, common_1.Post)('actions/:actionId/execute'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({ summary: 'Execute/re-execute an action' }),
    (0, swagger_1.ApiParam)({ name: 'actionId', description: 'Action UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Action executed', type: core_entities_1.AIAction }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Action not found' }),
    __param(0, (0, common_1.Param)('actionId', common_1.ParseUUIDPipe)),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "executeAction", null);
__decorate([
    (0, common_1.Post)('inputs/:inputId/apply-approved-actions'),
    (0, swagger_1.ApiOperation)({ summary: 'Execute all approved actions for input' }),
    (0, swagger_1.ApiParam)({ name: 'inputId', description: 'Input UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'All actions applied successfully' }),
    (0, swagger_1.ApiResponse)({
        status: 207,
        description: 'Multi-Status: some actions failed',
    }),
    __param(0, (0, common_1.Param)('inputId', common_1.ParseUUIDPipe)),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "applyApprovedActions", null);
__decorate([
    (0, common_1.Post)('inputs/:inputId/discard-actions'),
    (0, swagger_1.ApiOperation)({ summary: 'Discard all pending actions for input' }),
    (0, swagger_1.ApiParam)({ name: 'inputId', description: 'Input UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Actions discarded' }),
    __param(0, (0, common_1.Param)('inputId', common_1.ParseUUIDPipe)),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "discardAllActions", null);
__decorate([
    (0, common_1.Post)('inputs/:inputId/confirm-all-actions'),
    (0, swagger_1.ApiOperation)({ summary: 'Confirm all applied actions as permanent' }),
    (0, swagger_1.ApiParam)({ name: 'inputId', description: 'Input UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Actions confirmed' }),
    __param(0, (0, common_1.Param)('inputId', common_1.ParseUUIDPipe)),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "confirmAllActions", null);
__decorate([
    (0, common_1.Post)('inputs/:inputId/revert-all-actions'),
    (0, swagger_1.ApiOperation)({ summary: 'Revert all applied actions for input' }),
    (0, swagger_1.ApiParam)({ name: 'inputId', description: 'Input UUID', format: 'uuid' }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'All actions reverted successfully',
    }),
    (0, swagger_1.ApiResponse)({
        status: 207,
        description: 'Multi-Status: some reverts failed',
    }),
    __param(0, (0, common_1.Param)('inputId', common_1.ParseUUIDPipe)),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], AIActionsController.prototype, "revertAllActions", null);
exports.AIActionsController = AIActionsController = __decorate([
    (0, swagger_1.ApiTags)('ai-actions'),
    (0, common_1.Controller)(),
    __metadata("design:paramtypes", [ai_actions_service_1.AIActionsService,
        ai_action_batch_service_1.AIActionBatchService])
], AIActionsController);
//# sourceMappingURL=ai-actions.controller.js.map