#!/usr/bin/env node

const fs = require("fs");
const path = require("path");

// Helper functions
const log = (message) => console.log(`[Setup]: ${message}`);

const createDirIfNotExists = (dir) => {
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
        log(`Created directory: ${dir}`);
    }
};

const copyTemplateFile = (sourcePath, targetPath, overwrite) => {
    if (overwrite || !fs.existsSync(targetPath)) {
        const content = fs.readFileSync(sourcePath, "utf8"); // Read content from the template file
        fs.writeFileSync(targetPath, content, "utf8"); // Write content to the target location
        log(`Created file: ${targetPath}`);
    } else {
        log(`File already exists: ${targetPath}`);
    }
};

// Main function to set up files and directories
const run = () => {
    log("Starting project setup...");

    // Define directories to create
    const dirs = ["src", "src/styles", "src/pages","src/pages/admin","src/pages/customer","src/pages/invoices","src/components", "src/config", "src/plugins", "src/styles", "src/router","src/stores"];

    // Define template files to copy and their destinations
    const templatesDir = path.join(__dirname, "templates"); // Template directory (relative to the CLI file)
    const files = [
        { source: path.join(templatesDir, "config/ucsd.config.ts"), target: path.join(process.cwd(), "src/config/ucsd.config.ts") },
        { source: path.join(templatesDir, "styles/settings.scss"), target: path.join(process.cwd(), "src/styles/settings.scss") },
        { source: path.join(templatesDir, "plugins/index.ts"), target: path.join(process.cwd(), "src/plugins/index.ts") },
        { source: path.join(templatesDir, "plugins/vuetify.ts"), target: path.join(process.cwd(), "src/plugins/vuetify.ts") },
        { source: path.join(templatesDir, "router/index.ts"), target: path.join(process.cwd(), "src/router/index.ts") },
        { source: path.join(templatesDir, "router/routes.ts"), target: path.join(process.cwd(), "src/router/routes.ts") },
        { source: path.join(templatesDir, "stores/app.ts"), target: path.join(process.cwd(), "src/stores/app.ts") },
        { source: path.join(templatesDir, "stores/index.ts"), target: path.join(process.cwd(), "src/stores/index.ts") },
        { source: path.join(templatesDir, "pages/Home.vue"), target: path.join(process.cwd(), "src/pages/Home.vue") },
        { source: path.join(templatesDir, "pages/DashBoard.vue"), target: path.join(process.cwd(), "src/pages/DashBoard.vue") },
        { source: path.join(templatesDir, "pages/admin/AdminPage.vue"), target: path.join(process.cwd(), "src/pages/admin/AdminPage.vue") },
        { source: path.join(templatesDir, "pages/customer/CustomerHome.vue"), target: path.join(process.cwd(), "src/pages/customer/CustomerHome.vue") },
        { source: path.join(templatesDir, "pages/customer/CustomerSearch.vue"), target: path.join(process.cwd(), "src/pages/customer/CustomerSearch.vue") },
        { source: path.join(templatesDir, "pages/customer/SimpleForm.vue"), target: path.join(process.cwd(), "src/pages/customer/SimpleForm.vue") },
        { source: path.join(templatesDir, "pages/invoices/InvoiceHome.vue"), target: path.join(process.cwd(), "src/pages/invoices/InvoiceHome.vue") },
        { source: path.join(templatesDir, ".env"), target: path.join(process.cwd(),"/.env") }
    ];

    const overWriteFiles = [
        { source: path.join(templatesDir, "main.ts"), target: path.join(process.cwd(), "src/main.ts") },
        { source: path.join(templatesDir, "App.vue"), target: path.join(process.cwd(), "src/App.vue") },
        { source: path.join(templatesDir, "index.html"), target: path.join(process.cwd(),"/index.html") },
        { source: path.join(templatesDir, "vite.config.ts"), target: path.join(process.cwd(),"/vite.config.ts") }
    ];


    // Create directories
    dirs.forEach((dir) => createDirIfNotExists(path.join(process.cwd(), dir)));

    // Copy template files
    files.forEach(({ source, target }) => copyTemplateFile(source, target, false ));
    overWriteFiles.forEach(({ source, target }) => copyTemplateFile(source, target, true));

    log("Setup completed successfully!");
};

run();

