"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 ScalewayBootstrapSeeder_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScalewayBootstrapSeeder = void 0;
const common_1 = require("@nestjs/common");
const cloud_provider_enum_1 = require("../../enums/cloud-provider.enum");
const credentials_entity_1 = require("../../../management/entities/credentials.entity");
let ScalewayBootstrapSeeder = ScalewayBootstrapSeeder_1 = class ScalewayBootstrapSeeder {
    constructor() {
        this.provider = cloud_provider_enum_1.CloudProvider.SCALEWAY;
        this.logger = new common_1.Logger(ScalewayBootstrapSeeder_1.name);
    }
    buildCredentials(env) {
        const accessKey = env.PROVIDER_SCALEWAY_ACCESS_KEY;
        const secretKey = env.PROVIDER_SCALEWAY_SECRET_KEY;
        if (!accessKey || !secretKey)
            return null;
        return {
            credentialType: credentials_entity_1.CredentialType.ACCESS_KEY_SECRET,
            token: secretKey,
            accessKey,
            label: 'Scaleway API Key Pair (bootstrap)',
        };
    }
    async resolveProviderResourceId(args) {
        const { instanceId, instanceName, env } = args;
        if (instanceId && /^(instance|baremetal):/.test(instanceId)) {
            return instanceId;
        }
        const token = env.PROVIDER_SCALEWAY_SECRET_KEY;
        const region = env.CLUSTER_REGION || 'fr-par';
        const zone = /^[a-z]{2}-[a-z]{3}$/.test(region) ? `${region}-1` : region;
        if (!token || !instanceName)
            return instanceId;
        try {
            const res = await fetch(`https://api.scaleway.com/instance/v1/zones/${zone}/servers?name=${encodeURIComponent(instanceName)}`, { headers: { 'X-Auth-Token': token } });
            if (!res.ok) {
                this.logger.warn(`Scaleway lookup for ${instanceName} returned ${res.status}; keeping placeholder`);
                return instanceId;
            }
            const data = (await res.json());
            const match = data.servers?.find((s) => s.name === instanceName);
            if (match?.id) {
                const canonical = `instance:${zone}:${match.id}`;
                this.logger.log(`Resolved Scaleway resource ID for ${instanceName}: ${canonical}`);
                return canonical;
            }
            this.logger.warn(`Scaleway lookup for ${instanceName} in zone ${zone} returned no match; keeping placeholder`);
        }
        catch (err) {
            this.logger.warn(`Scaleway lookup failed for ${instanceName}: ${err.message}; keeping placeholder`);
        }
        return instanceId;
    }
};
exports.ScalewayBootstrapSeeder = ScalewayBootstrapSeeder;
exports.ScalewayBootstrapSeeder = ScalewayBootstrapSeeder = ScalewayBootstrapSeeder_1 = __decorate([
    (0, common_1.Injectable)()
], ScalewayBootstrapSeeder);
