"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.ClusterValidationService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const cluster_entity_1 = require("../entities/cluster.entity");
const management_service_1 = require("../../../management/services/management.service");
/**
 * Service responsible for cluster validation logic
 */
let ClusterValidationService = class ClusterValidationService {
    constructor(clusterRepository, managementService) {
        this.clusterRepository = clusterRepository;
        this.managementService = managementService;
    }
    /**
     * Validate cluster creation request
     */
    async validateCreateClusterRequest(dto) {
        // Check for duplicate cluster name (exclude deleted clusters)
        const existingCluster = await this.clusterRepository.findOne({
            where: {
                name: dto.name,
                status: (0, typeorm_2.Not)(cluster_entity_1.ClusterStatus.DELETED),
            },
        });
        if (existingCluster) {
            throw new common_1.ConflictException(`Cluster with name '${dto.name}' already exists`);
        }
        // Validate node size
        await this.validateNodeSize(dto.provider, dto.region, dto.nodeSize);
        // Validate autoscaling configuration
        if (dto.autoscalingEnabled) {
            this.validateAutoscalingConfig(dto);
        }
        // Validate VNet configuration if provided
        if (dto.vnetConfig) {
            this.validateVNetConfig(dto);
        }
    }
    /**
     * Validate node size exists for provider/region
     * Accepts both name (e.g., 'cx22') and ID (e.g., '115')
     */
    async validateNodeSize(provider, region, nodeSize) {
        const nodeSizes = await this.managementService.getNodeSizes(provider, region);
        // Accept both name and ID for flexibility
        const validSize = nodeSizes.find((size) => size.name === nodeSize || size.id === nodeSize);
        if (!validSize) {
            throw new common_1.BadRequestException(`Node size '${nodeSize}' is not available for provider '${provider}' in region '${region}'`);
        }
    }
    /**
     * Validate autoscaling configuration
     */
    validateAutoscalingConfig(dto) {
        if (!dto.minNodes || !dto.maxNodes) {
            throw new common_1.BadRequestException('Autoscaling enabled but minNodes or maxNodes not provided');
        }
        if (dto.minNodes > dto.maxNodes) {
            throw new common_1.BadRequestException('minNodes cannot be greater than maxNodes');
        }
        if (dto.minNodes < 1) {
            throw new common_1.BadRequestException('minNodes must be at least 1');
        }
        if (dto.maxNodes > 20) {
            throw new common_1.BadRequestException('maxNodes cannot exceed 20');
        }
    }
    /**
     * Validate VNet configuration
     */
    validateVNetConfig(dto) {
        if (!dto.vnetConfig.vnetId) {
            throw new common_1.BadRequestException('vnetConfig.vnetId is required');
        }
        // Additional VNet validations can be added here
        // For example, verify VNet exists and is in the same provider/region
    }
};
exports.ClusterValidationService = ClusterValidationService;
exports.ClusterValidationService = ClusterValidationService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(cluster_entity_1.ClusterEntity)),
    __metadata("design:paramtypes", [typeorm_2.Repository,
        management_service_1.ManagementService])
], ClusterValidationService);
