/**
 * Packages this Laravel app's source into a deployable zip for production.
 *
 * Deliberately excluded (uploaded/managed separately on the server, see the
 * project's deployment notes): node_modules/, vendor/, public/, storage/,
 * upload/ (the tenant media disk - config/filesystems.php "uploads" disk).
 *
 * Usage: npm run build:dist  →  dist/crmfic-<version>.zip
 */
const pad = (n) => String(n).padStart(2, '0');
const now = new Date();
const version = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}`;

const projectConfig = Object.freeze({
    name: 'crmfic',
    distDir: `dist/crmfic/`,
    version,
});

module.exports = (grunt) => {
    'use strict';

    grunt.initConfig({
        clean: {
            options: { force: true },
            dist: [
                projectConfig.distDir + '**',
                'dist/' + projectConfig.name + '-*.zip',
            ],
        },

        copy: {
            dist: {
                files: [
                    {
                        expand: true,
                        src: [
                            '**',

                            // Installed separately on the server / built locally
                            // and uploaded as their own artifact - see deployment
                            // notes. Never bundle these into the app-code zip.
                            '!node_modules/**',
                            '!vendor/**',
                            '!public/**',
                            '!storage/**',
                            '!upload/**',

                            // Build tool / VCS / editor metadata
                            '!dist/**',
                            '!.git/**',
                            '!.github/**',
                            '!.claude/**',
                            '!.agents/**',
                            '!.vscode/**',
                            '!.idea/**',
                            '!Gruntfile.cjs',

                            // Secrets - never ship a real .env
                            '!.env',
                            '!.env.*',

                            // Local scratch / working files, not app code
                            '!*.zip',
                            '!*.sql',
                            '!*.xlsx',
                            '!crm-features-list.md',
                            '!scratch_user.php',

                            // Framework caches - regenerated by artisan on deploy
                            '!bootstrap/cache/*.php',
                        ],
                        dest: projectConfig.distDir,
                    },
                ],
            },
        },

        compress: {
            dist: {
                options: {
                    force: true,
                    mode: 'zip',
                    archive: `dist/${projectConfig.name}-${projectConfig.version}.zip`,
                },
                expand: true,
                cwd: projectConfig.distDir,
                src: ['**'],
                dest: projectConfig.name,
            },
        },

        screen: {
            begin: `
Automatic Build Tool
# Project   : ${projectConfig.name}
# Dist      : ${projectConfig.distDir}
# Version   : ${projectConfig.version}
# Excluded  : node_modules/, vendor/, public/, storage/, upload/`.cyan,

            finish: `
╭─────────────────────────────────────────────────────────────────╮
│                                                                 │
│                      All tasks completed.                       │
│   dist/${projectConfig.name}/           - app source, ready to upload    │
│   dist/${projectConfig.name}-${projectConfig.version}.zip - same thing, zipped   │
│                                                                 │
│   Still needed on the server / uploaded separately:              │
│     composer install --no-dev --optimize-autoloader              │
│     npm ci && npm run build   (produces public/build + assets)   │
│     vendor/, public/, storage/, upload/ folders                  │
│                    ~ Automatic Build Tool ~                     │
│                                                                 │
╰─────────────────────────────────────────────────────────────────╯`.green,
        },
    });

    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-compress');

    grunt.registerMultiTask('screen', function () {
        grunt.log.writeln(this.data);
    });

    grunt.registerTask('build', [
        'screen:begin',
        'clean',
        'copy',
        'compress',
        'screen:finish',
    ]);
};
