"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var ContaboCapabilitiesService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContaboCapabilitiesService = void 0;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const cloud_provider_enum_1 = require("../../enums/cloud-provider.enum");
const credentials_entity_1 = require("../../../management/entities/credentials.entity");
const region_coordinates_1 = require("../../data/region-coordinates");
let ContaboCapabilitiesService = ContaboCapabilitiesService_1 = class ContaboCapabilitiesService {
    constructor(configService) {
        this.configService = configService;
    }
    async getAvailableRegions() {
        const make = (id, name, displayName, location, country, flagEmoji) => {
            const coords = (0, region_coordinates_1.getRegionCoordinates)(cloud_provider_enum_1.CloudProvider.CONTABO, id);
            return {
                id,
                name,
                displayName,
                location,
                available: true,
                flagEmoji,
                country,
                latitude: coords?.latitude,
                longitude: coords?.longitude,
            };
        };
        return [
            make('EU', 'Hub Europe', 'Hub Europe (Lauterbourg, FR/DE)', 'Lauterbourg, France/Germany border', 'France/Germany', '🇪🇺'),
            make('EU-1', 'Nuremberg', 'Nuremberg, Germany', 'Nuremberg, Germany', 'Germany', '🇩🇪'),
            make('EU-2', 'Munich', 'Munich, Germany', 'Munich, Germany', 'Germany', '🇩🇪'),
            make('UK', 'Portsmouth', 'Portsmouth, United Kingdom', 'Portsmouth, England', 'United Kingdom', '🇬🇧'),
        ];
    }
    async getSupportedInstanceTypes() {
        // Contabo bills monthly only — hourly rate is approximate (monthly / 730)
        return [
            {
                id: 'CLOUD-VPS-10',
                name: 'Cloud VPS 10',
                description: '4 vCPU, 8 GB RAM, 75 GB NVMe',
                cpu: 4,
                memory: 8192,
                disk: 76800,
                bandwidth: 32000,
                pricing: { hourly: 0.0062, monthly: 4.5, currency: 'EUR' },
                available: true,
            },
            {
                id: 'CLOUD-VPS-20',
                name: 'Cloud VPS 20',
                description: '6 vCPU, 12 GB RAM, 100 GB NVMe',
                cpu: 6,
                memory: 12288,
                disk: 102400,
                bandwidth: 32000,
                pricing: { hourly: 0.0096, monthly: 7, currency: 'EUR' },
                available: true,
            },
            {
                id: 'CLOUD-VPS-30',
                name: 'Cloud VPS 30',
                description: '8 vCPU, 24 GB RAM, 200 GB NVMe',
                cpu: 8,
                memory: 24576,
                disk: 204800,
                bandwidth: 32000,
                pricing: { hourly: 0.0192, monthly: 14, currency: 'EUR' },
                available: true,
            },
            {
                id: 'CLOUD-VPS-40',
                name: 'Cloud VPS 40',
                description: '12 vCPU, 48 GB RAM, 250 GB NVMe',
                cpu: 12,
                memory: 49152,
                disk: 256000,
                bandwidth: 32000,
                pricing: { hourly: 0.0342, monthly: 25, currency: 'EUR' },
                available: true,
            },
            {
                id: 'CLOUD-VPS-50',
                name: 'Cloud VPS 50',
                description: '16 vCPU, 64 GB RAM, 300 GB NVMe',
                cpu: 16,
                memory: 65536,
                disk: 307200,
                bandwidth: 32000,
                pricing: { hourly: 0.0507, monthly: 37, currency: 'EUR' },
                available: true,
            },
            {
                id: 'CLOUD-VPS-60',
                name: 'Cloud VPS 60',
                description: '18 vCPU, 96 GB RAM, 350 GB NVMe',
                cpu: 18,
                memory: 98304,
                disk: 358400,
                bandwidth: 32000,
                pricing: { hourly: 0.0671, monthly: 49, currency: 'EUR' },
                available: true,
            },
        ];
    }
    async getProviderInfo() {
        return {
            id: cloud_provider_enum_1.CloudProvider.CONTABO,
            name: 'contabo',
            displayName: 'Contabo',
            description: 'European VPS provider with competitive monthly pricing',
            logoUrl: '/api/v1/management/providers/contabo/logo',
            websiteUrl: 'https://contabo.com/',
            documentationUrl: 'https://api.contabo.com/',
            supportUrl: 'https://contabo.com/en/support/',
            accessKeyDocumentationUrl: 'https://api.contabo.com/#section/Introduction/Product-documentation',
            pricingUrl: 'https://contabo.com/en/pricing/',
            credentialFields: {
                type: credentials_entity_1.CredentialType.USER_PASSWORD,
                supportsExpiry: false,
                fields: [
                    {
                        key: 'clientId',
                        label: 'Client ID',
                        providerLabel: 'Client ID',
                        hint: 'Contabo API → OAuth2 credentials → Client ID',
                        secret: false,
                        required: true,
                    },
                    {
                        key: 'clientSecret',
                        label: 'Client Secret',
                        providerLabel: 'Client Secret',
                        hint: 'Contabo API → OAuth2 credentials → Client Secret',
                        secret: true,
                        required: true,
                    },
                    {
                        key: 'username',
                        label: 'Username',
                        providerLabel: 'Username',
                        hint: 'Your Contabo account email address',
                        secret: false,
                        required: true,
                    },
                    {
                        key: 'password',
                        label: 'Password',
                        providerLabel: 'Password',
                        hint: 'Your Contabo account password',
                        secret: true,
                        required: true,
                    },
                ],
            },
        };
    }
    async validateCredentials(credentials) {
        return {
            success: false,
            message: 'Use type: user_password with username, password, clientId and clientSecret',
        };
    }
    getLogo() {
        return fs.readFileSync(ContaboCapabilitiesService_1.LOGO_PATH);
    }
    getLogoContentType() {
        return 'image/svg+xml';
    }
    getStaticCapabilities() {
        return {
            supportedInstanceTypes: [],
            supportedRegions: [],
            credentialType: 'user_password',
            features: {
                autoScaling: false,
                loadBalancers: false,
                privateNetworking: true,
                snapshots: true,
                backups: true,
                dnsZones: false,
                nodeProvisioning: false,
            },
            pricing: {
                currency: 'EUR',
                billingCycle: 'monthly',
                minimumCost: 4.5, // Cloud VPS 10: 4 vCPU, 8 GB RAM @ €4.50/mo
            },
            vnetTopology: null, // Contabo VNet not yet implemented
        };
    }
    async getCapabilities() {
        const [regions, instanceTypes] = await Promise.all([
            this.getAvailableRegions(),
            this.getSupportedInstanceTypes(),
        ]);
        return {
            ...this.getStaticCapabilities(),
            supportedInstanceTypes: instanceTypes.map((type) => type.id),
            supportedRegions: regions,
        };
    }
};
exports.ContaboCapabilitiesService = ContaboCapabilitiesService;
ContaboCapabilitiesService.LOGO_PATH = path.resolve(process.cwd(), 'dist/modules/providers/implementations/contabo/assets/logo.svg');
exports.ContaboCapabilitiesService = ContaboCapabilitiesService = ContaboCapabilitiesService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.ConfigService])
], ContaboCapabilitiesService);
