"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const fs_1 = require("fs");
const path_1 = require("path");
async function update(tree) {
    devkit_1.logger.info('🔄 Updating GitHub workflows for v3.0.0');
    // Ensure .github/workflows directory exists
    if (!tree.exists('.github')) {
        tree.write('.github/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github directory');
    }
    if (!tree.exists('.github/workflows')) {
        tree.write('.github/workflows/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github/workflows directory');
    }
    try {
        // Update cicd.yml
        const cicdWorkflowPath = '.github/workflows/cicd.yml';
        const cicdTemplatePath = (0, path_1.join)(__dirname, 'templates', 'cicd.template');
        const cicdTemplateContent = (0, fs_1.readFileSync)(cicdTemplatePath, 'utf-8');
        const cicdExists = tree.exists(cicdWorkflowPath);
        tree.write(cicdWorkflowPath, cicdTemplateContent);
        if (cicdExists) {
            devkit_1.logger.info(`✅ Updated ${cicdWorkflowPath}`);
        }
        else {
            devkit_1.logger.info(`✅ Created ${cicdWorkflowPath}`);
        }
        // Update _publish-artifacts.yml
        const publishArtifactsWorkflowPath = '.github/workflows/_publish-artifacts.yml';
        const publishArtifactsTemplatePath = (0, path_1.join)(__dirname, 'templates', '_publish-artifacts.template');
        const publishArtifactsTemplateContent = (0, fs_1.readFileSync)(publishArtifactsTemplatePath, 'utf-8');
        const publishArtifactsExists = tree.exists(publishArtifactsWorkflowPath);
        tree.write(publishArtifactsWorkflowPath, publishArtifactsTemplateContent);
        if (publishArtifactsExists) {
            devkit_1.logger.info(`✅ Updated ${publishArtifactsWorkflowPath}`);
        }
        else {
            devkit_1.logger.info(`✅ Created ${publishArtifactsWorkflowPath}`);
        }
    }
    catch (error) {
        devkit_1.logger.error(`❌ Failed to update GitHub workflows: ${error?.message || 'Unknown error'}`);
        throw error;
    }
}
